Skip to content

Commit

Permalink
EditDialog: connect relation to OSM API
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaclavik committed Jan 20, 2025
1 parent c4c6c57 commit a50169d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/locales/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default {
'editdialog.location_editor_to_be_added': 'Polohu zde zatím upravit nelze, můžete to provést třeba v <a href="__link__">editoru iD</a>.',
'editdialog.place_cancelled': 'Místo zrušeno (smazat)',
'editdialog.comment': 'Poznámka (nepovinné)',
'editdialog.comment_placeholder': 'odkaz na zdroj informace apod.',
'editdialog.comment_placeholder': 'Odkaz na zdroj informace apod.',
'editdialog.info_edit': `Vaše úprava bude ihned uložena do databáze OpenStreetMap. Prosíme,
vkládejte pouze informace z vlastních nebo ověřených zdrojů. Je zakázano
kopírovat data krytá autorským zákonem (např. Google Maps).
Expand Down Expand Up @@ -212,6 +212,10 @@ export default {
'editsuccess.edit.body': `Uložení proběhlo do primární databáze OSM. V řádu několika minut změnu uvidíte na mapě "OSM Carto". Zdejší mapa a různé jiné aplikace se obnovují cca 1x za měsíc.\n\nPokud se jedná o omyl, můžete hodnoty ručně vrátit zpět a znovu je uložit.`,
'editsuccess.edit.urlLabel': `Vaše změny:`,
'editsuccess.edit.textLabel': 'Poznámka ke změně',
'editdialog.preset_select.label': 'Typ',
'editdialog.preset_select.placeholder': 'Vyberte typ',
'editdialog.preset_select.search_placeholder': 'Zadejte hledaný výraz...',
'editdialog.preset_select.edit_button': 'Upravit',

'tags.name': 'Název',
'tags.website': 'Web',
Expand Down
39 changes: 32 additions & 7 deletions src/services/osm/osmApiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,20 @@ const deleteItem = (apiId: OsmId, content: string) =>
content,
});

const createItem = (content: string) =>
const createNodeItem = (content: string) =>
authFetch<string>({
method: 'PUT',
path: `/api/0.6/node/create`,
headers: { 'Content-Type': 'text/xml; charset=utf-8' },
content,
});
const createRelationItem = (content: string) =>
authFetch<string>({
method: 'PUT',
path: `/api/0.6/relation/create`,
headers: { 'Content-Type': 'text/xml; charset=utf-8' },
content,
});

const putOrDeleteItem = async (
toBeDeleted: boolean,
Expand Down Expand Up @@ -279,20 +286,38 @@ const getNewNodeXml = async (
return buildXmlString(xml);
};

const getNewRelationXml = async (
changesetId: string,
newTags: FeatureTags,
members: Members,
) => {
const xml = await parseToXml2Js('<osm><relation visible="true" /></osm>');
xml.relation.$.changeset = changesetId;
xml.relation.tag = getXmlTags(newTags);
xml.relation.member = getXmlMembers(members);
return buildXmlString(xml);
};

const saveChange = async (
changesetId: string,
{ shortId, version, tags, toBeDeleted, nodeLonLat, members }: EditDataItem,
): Promise<OsmId> => {
// TODO don't save changes if no change detected

let apiId = getApiId(shortId);
if (apiId.id < 0) {
if (apiId.type !== 'node') {
throw new Error('We can only add new nodes so far.');
if (apiId.type === 'way') {
throw new Error('We can only add new nodes and relations so far.');
}
if (apiId.type === 'node') {
const content = await getNewNodeXml(changesetId, nodeLonLat, tags);
const newNodeId = await createNodeItem(content);
return { type: 'node', id: parseInt(newNodeId, 10) };
}
if (apiId.type === 'relation') {
const content = await getNewRelationXml(changesetId, tags, members);
const newRelationId = await createRelationItem(content);
return { type: 'relation', id: parseInt(newRelationId, 10) };
}
const content = await getNewNodeXml(changesetId, nodeLonLat, tags);
const newNodeId = await createItem(content);
return { type: 'node', id: parseInt(newNodeId, 10) };
}

const freshItem = await getItem(apiId);
Expand Down

0 comments on commit a50169d

Please sign in to comment.