Skip to content

Commit

Permalink
EditDialog: show parent ways for nodes (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Jan 11, 2025
1 parent 608d427 commit 32a17aa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getApiId, getShortId } from '../../../../services/helpers';
import { FeatureRow } from './FeatureRow';
import { t } from '../../../../services/intl';
import { useGetHandleClick } from './helpers';
import { fetchNodesWaysFeatures } from '../../../../services/osm/fetchNodesWaysFeatures';

export const ParentsEditor = () => {
const { shortId } = useFeatureEditData();
Expand All @@ -30,7 +31,11 @@ export const ParentsEditor = () => {
return;
}

setParents(await fetchParentFeatures(getApiId(shortId)));
const [parentFeatures, waysFeatures] = await Promise.all([
fetchParentFeatures(getApiId(shortId)),
fetchNodesWaysFeatures(getApiId(shortId)),
]);
setParents([...parentFeatures, ...waysFeatures]);
})();
}, [shortId]);

Expand Down
18 changes: 18 additions & 0 deletions src/services/osm/fetchNodesWaysFeatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { OsmId } from '../types';
import { fetchJson } from '../fetch';
import { getOsmNodesWaysUrl, isNodeOsmId, NodeOsmId } from './urls';
import { addSchemaToFeature } from '../tagging/idTaggingScheme';
import { osmToFeature } from './osmToFeature';
import { OsmResponse } from './types';

const getOsmNodesWaysPromise = async (apiId: NodeOsmId) =>
fetchJson<OsmResponse<'way'>>(getOsmNodesWaysUrl(apiId));

export const fetchNodesWaysFeatures = async (apiId: OsmId) => {
if (isNodeOsmId(apiId)) {
const { elements } = await getOsmNodesWaysPromise(apiId);
return elements.map((element) => addSchemaToFeature(osmToFeature(element)));
}

return [];
};
9 changes: 4 additions & 5 deletions src/services/osm/fetchParentFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { OsmId } from '../types';
import { Feature, OsmId } from '../types';
import { fetchJson } from '../fetch';
import { getOsmParentUrl } from './urls';
import { addSchemaToFeature } from '../tagging/idTaggingScheme';
import { osmToFeature } from './osmToFeature';
import { OsmResponse } from './types';

const getOsmParentPromise = async (apiId: OsmId) => {
const { elements } = await fetchJson(getOsmParentUrl(apiId));
return { elements };
};
const getOsmParentPromise = async (apiId: OsmId) =>
fetchJson<OsmResponse>(getOsmParentUrl(apiId));

export const fetchParentFeatures = async (apiId: OsmId) => {
const { elements } = await getOsmParentPromise(apiId);
Expand Down
1 change: 1 addition & 0 deletions src/services/osm/osmApiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export const saveChanges = async (
const changesetId = await putChangeset(changesetXml);

// TODO refactor below
// or even better use osmChange xml https://wiki.openstreetmap.org/wiki/API_v0.6#Diff_upload:_POST_/api/0.6/changeset/#id/upload
const changesNodes = changes.filter(({ shortId }) => shortId[0] === 'n');
const savedNodesIds = await Promise.all(
changesNodes.map((change) => saveChange(changesetId, change)),
Expand Down
4 changes: 2 additions & 2 deletions src/services/osm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export type OsmElement<T extends OsmType = 'node' | 'way' | 'relation'> = {
members?: RelationMember[];
};

export type OsmResponse = {
elements: OsmElement[];
export type OsmResponse<T extends OsmType = 'node' | 'way' | 'relation'> = {
elements: OsmElement<T>[];
};
7 changes: 7 additions & 0 deletions src/services/osm/urls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { OsmId } from '../types';
import { API_SERVER } from './consts';

export type NodeOsmId = { type: 'node'; id: number };
export const isNodeOsmId = (apiId: OsmId): apiId is NodeOsmId =>
apiId.type === 'node';

export const getOsmUrl = ({ type, id }: OsmId) =>
`${API_SERVER}/api/0.6/${type}/${id}.json`;

Expand All @@ -10,6 +14,9 @@ export const getOsmFullUrl = ({ type, id }: OsmId) =>
export const getOsmParentUrl = ({ type, id }: OsmId) =>
`${API_SERVER}/api/0.6/${type}/${id}/relations.json`;

export const getOsmNodesWaysUrl = ({ id }: NodeOsmId) =>
`${API_SERVER}/api/0.6/node/${id}/ways.json`;

export const getOsmHistoryUrl = ({ type, id }: OsmId) =>
`${API_SERVER}/api/0.6/${type}/${id}/history.json`;

Expand Down
2 changes: 1 addition & 1 deletion src/services/tagging/idTaggingScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const getSchemaForFeature = (feature: Feature) => {
};
};

export const addSchemaToFeature = (feature: Feature) => {
export const addSchemaToFeature = (feature: Feature): Feature => {
let schema;
try {
schema = getSchemaForFeature(feature); // TODO forward lang here ?? maybe full intl?
Expand Down

0 comments on commit 32a17aa

Please sign in to comment.