Skip to content

Commit

Permalink
fix add new point
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Jan 9, 2025
1 parent 8ab5efa commit 977e9cf
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/services/osmApiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,6 @@ const createItem = (content: string) =>
content,
});

const putOrDeleteItem = async (
toBeDeleted: boolean,
apiId: OsmId,
newItem: string,
) => {
if (toBeDeleted) {
await deleteItem(apiId, newItem);
} else {
await putItem(apiId, newItem);
}
};

const getItemOrLastHistoric = async (
apiId: OsmId,
): Promise<Xml2JsSingleDoc> => {
Expand Down Expand Up @@ -279,18 +267,32 @@ const getNewNodeXml = async (
return buildXmlString(xml);
};

const createNewItem = async (
apiId: OsmId,
changesetId: string,
nodeLonLat: number[],
tags: FeatureTags,
): Promise<OsmId> => {
if (apiId.type !== 'node') {
throw new Error('We can only add new nodes so far.');
}
const content = await getNewNodeXml(changesetId, nodeLonLat, tags);
const newNodeId = await createItem(content);
return { type: 'node', id: parseInt(newNodeId, 10) };
};

const saveChange = async (
changesetId: string,
{ shortId, version, tags, toBeDeleted, nodeLonLat, members }: EditDataItem,
): Promise<OsmId> => {
let apiId = getApiId(shortId);
if (apiId.id < 0) {
if (apiId.type !== 'node') {
throw new Error('We can only add new nodes so far.');
}
const content = await getNewNodeXml(changesetId, nodeLonLat, tags);
const newNodeId = await createItem(content);
return { type: 'node', id: parseInt(newNodeId, 10) };
return await createNewItem(apiId, changesetId, nodeLonLat, tags);
}

if (toBeDeleted) {
await deleteItem(apiId, newItem);
return apiId;
}

const freshItem = await getItem(apiId);
Expand All @@ -306,7 +308,7 @@ const saveChange = async (
members,
nodeLonLat,
);
await putOrDeleteItem(toBeDeleted, apiId, newItem);
await putItem(apiId, newItem);
return apiId;
};

Expand All @@ -321,8 +323,8 @@ const getCommentMulti = (
// TODO find topmost parent in changes and use its name
// eg. survey • Edited Roviště (5 items) #osmapp #climbing

if (changes.length === 1 && changes[0].nodeLonLat) {
const typeTag = Object.entries(changes[0].tags)[0]?.join('=');
if (changes.length === 1 && original.point) {
const typeTag = changes[0].tagsEntries[0]?.join('=') ?? 'node with no tags';
return join(comment, ' • ', `Added ${typeTag} #osmapp`);
}

Expand Down

0 comments on commit 977e9cf

Please sign in to comment.