diff --git a/src/components/FeaturePanel/EditDialog/EditContent/ParentsEditor.tsx b/src/components/FeaturePanel/EditDialog/EditContent/ParentsEditor.tsx index 1b3a480ba..28bb6dde7 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/ParentsEditor.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/ParentsEditor.tsx @@ -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(); @@ -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]); diff --git a/src/services/osm/fetchNodesWaysFeatures.ts b/src/services/osm/fetchNodesWaysFeatures.ts new file mode 100644 index 000000000..e04f2f307 --- /dev/null +++ b/src/services/osm/fetchNodesWaysFeatures.ts @@ -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>(getOsmNodesWaysUrl(apiId)); + +export const fetchNodesWaysFeatures = async (apiId: OsmId) => { + if (isNodeOsmId(apiId)) { + const { elements } = await getOsmNodesWaysPromise(apiId); + return elements.map((element) => addSchemaToFeature(osmToFeature(element))); + } + + return []; +}; diff --git a/src/services/osm/fetchParentFeatures.ts b/src/services/osm/fetchParentFeatures.ts index c59596540..6b7cbcf86 100644 --- a/src/services/osm/fetchParentFeatures.ts +++ b/src/services/osm/fetchParentFeatures.ts @@ -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(getOsmParentUrl(apiId)); export const fetchParentFeatures = async (apiId: OsmId) => { const { elements } = await getOsmParentPromise(apiId); diff --git a/src/services/osm/osmApiAuth.ts b/src/services/osm/osmApiAuth.ts index 51186c972..3e3d43406 100644 --- a/src/services/osm/osmApiAuth.ts +++ b/src/services/osm/osmApiAuth.ts @@ -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)), diff --git a/src/services/osm/types.ts b/src/services/osm/types.ts index 4bb7e8736..dea253ade 100644 --- a/src/services/osm/types.ts +++ b/src/services/osm/types.ts @@ -14,6 +14,6 @@ export type OsmElement = { members?: RelationMember[]; }; -export type OsmResponse = { - elements: OsmElement[]; +export type OsmResponse = { + elements: OsmElement[]; }; diff --git a/src/services/osm/urls.ts b/src/services/osm/urls.ts index 8d25f7e88..d7361e50a 100644 --- a/src/services/osm/urls.ts +++ b/src/services/osm/urls.ts @@ -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`; @@ -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`; diff --git a/src/services/tagging/idTaggingScheme.ts b/src/services/tagging/idTaggingScheme.ts index 4c6b2d828..2b390209d 100644 --- a/src/services/tagging/idTaggingScheme.ts +++ b/src/services/tagging/idTaggingScheme.ts @@ -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?