Skip to content

Commit

Permalink
fix(endpoint-posts): reading/editing geographic coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jan 17, 2024
1 parent 5a6cc36 commit f28325a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
8 changes: 3 additions & 5 deletions packages/endpoint-posts/lib/middleware/post-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const postData = {

// Only show advanced options if one of those fields has been updated
const showAdvancedOptions =
properties.category || properties.geo || properties.visibility;
properties.category || properties.location || properties.visibility;

response.locals = {
accessToken: access_token,
Expand Down Expand Up @@ -57,16 +57,14 @@ export const postData = {
throw IndiekitError.notFound(response.locals.__("NotFoundError.page"));
}

if (properties.location.geo) {
properties.geo = getGeoValue(properties.geo);
}

const geo = properties?.location && getGeoValue(properties.location);
const postType = properties["post-type"];

response.locals = {
accessToken: access_token,
action: action || "create",
draftMode: scope?.includes("draft"),
geo,
postName: getPostName(publication, properties),
postsPath: path.dirname(request.baseUrl + request.path),
postStatus: properties["post-status"],
Expand Down
10 changes: 7 additions & 3 deletions packages/endpoint-posts/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ export const getGeoProperty = (geo) => {

/**

Check warning on line 29 in packages/endpoint-posts/lib/utils.js

View workflow job for this annotation

GitHub Actions / test (20.x, 4)

JSDoc @returns declaration present but return expression not available in function
* Get comma separated geographic coordinates
* @param {object} geo - JF2 geo location property
* @param {object} location - JF2 location property
* @returns {string} Latitude and longitude, comma separated
*/
export const getGeoValue = (geo) => {
return [geo.latitude, geo.longitude].toString();
export const getGeoValue = (location) => {
if (location && location.geo) {
return [location.geo.latitude, location.geo.longitude].toString();
} else if (location && location.type === "geo") {
return [location.latitude, location.longitude].toString();
}
};

/**
Expand Down
38 changes: 28 additions & 10 deletions packages/endpoint-posts/test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,34 @@ describe("endpoint-posts/lib/utils", () => {
});
});

it("Gets comma separated geographic coordinates", () => {
assert.equal(
getGeoValue({
type: "geo",
name: "50° 49′ 30.72″ N 0° 8′ 17.88″ W",
latitude: 50.8252,
longitude: -0.1383,
}),
"50.8252,-0.1383",
);
it("Gets comma separated geographic coordinates", async (t) => {
await t.test("from address", () => {
assert.equal(
getGeoValue({
"street-address": "Jubilee Street",
locality: "Brighton",
geo: {
type: "geo",
name: "50° 49′ 30.72″ N 0° 8′ 17.88″ W",
latitude: 50.8252,
longitude: -0.1383,
},
}),
"50.8252,-0.1383",
);
});

await t.test("from geographic coordinates", () => {
assert.equal(
getGeoValue({
type: "geo",
name: "50° 49′ 30.72″ N 0° 8′ 17.88″ W",
latitude: 50.8252,
longitude: -0.1383,
}),
"50.8252,-0.1383",
);
});
});

it("Gets location property", async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-posts/views/post-form.njk
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@

{{ geoInput({
name: "geo",
value: fieldData("geo").value,
value: geo,
label: __("posts.form.geo.label"),
hint: __("posts.form.geo.hint", "50.8211, -0.1452"),
optional: true,
Expand Down

0 comments on commit f28325a

Please sign in to comment.