Lists: use separate add/remove functions instead of toggle
This commit is contained in:
@@ -156,16 +156,19 @@ export interface PlacesClient {
|
||||
delete(id: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Add or remove a place from a list.
|
||||
* Add a place to a list.
|
||||
* @param listId - The slug ID of the list.
|
||||
* @param placeId - The ID of the place.
|
||||
* @param geohash - The geohash of the place (needed for reference).
|
||||
* @param geohash - The geohash of the place.
|
||||
*/
|
||||
togglePlace(
|
||||
listId: string,
|
||||
placeId: string,
|
||||
geohash: string
|
||||
): Promise<List>;
|
||||
addPlace(listId: string, placeId: string, geohash: string): Promise<List>;
|
||||
|
||||
/**
|
||||
* Remove a place from a list.
|
||||
* @param listId - The slug ID of the list.
|
||||
* @param placeId - The ID of the place.
|
||||
*/
|
||||
removePlace(listId: string, placeId: string): Promise<List>;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -263,7 +266,7 @@ const Places = function (
|
||||
await privateClient.remove(`_lists/${id}`);
|
||||
},
|
||||
|
||||
async togglePlace(
|
||||
async addPlace(
|
||||
listId: string,
|
||||
placeId: string,
|
||||
geohash: string
|
||||
@@ -277,16 +280,33 @@ const Places = function (
|
||||
|
||||
const index = list.placeRefs.findIndex((ref: any) => ref.id === placeId);
|
||||
|
||||
if (index !== -1) {
|
||||
// Remove
|
||||
list.placeRefs.splice(index, 1);
|
||||
} else {
|
||||
// Add
|
||||
if (index === -1) {
|
||||
// Add only if not present
|
||||
list.placeRefs.push({ id: placeId, geohash });
|
||||
list.updatedAt = new Date().toISOString();
|
||||
await privateClient.storeObject('list', path, list);
|
||||
}
|
||||
|
||||
return list;
|
||||
},
|
||||
|
||||
async removePlace(listId: string, placeId: string): Promise<List> {
|
||||
const path = `_lists/${listId}`;
|
||||
const list = (await privateClient.getObject(path)) as List;
|
||||
|
||||
if (!list) {
|
||||
throw new Error(`List not found: ${listId}`);
|
||||
}
|
||||
|
||||
const index = list.placeRefs.findIndex((ref: any) => ref.id === placeId);
|
||||
|
||||
if (index !== -1) {
|
||||
// Remove only if present
|
||||
list.placeRefs.splice(index, 1);
|
||||
list.updatedAt = new Date().toISOString();
|
||||
await privateClient.storeObject('list', path, list);
|
||||
}
|
||||
|
||||
list.updatedAt = new Date().toISOString();
|
||||
await privateClient.storeObject('list', path, list);
|
||||
return list;
|
||||
},
|
||||
|
||||
@@ -327,6 +347,20 @@ const Places = function (
|
||||
if (!id || !geohash) {
|
||||
throw new Error('Both id and geohash are required to remove a place');
|
||||
}
|
||||
|
||||
// Cleanup: Remove this place from all lists
|
||||
const allLists = await lists.getAll();
|
||||
await Promise.all(
|
||||
allLists.map(async (list) => {
|
||||
const index = list.placeRefs.findIndex((ref: any) => ref.id === id);
|
||||
if (index !== -1) {
|
||||
list.placeRefs.splice(index, 1);
|
||||
list.updatedAt = new Date().toISOString();
|
||||
await privateClient.storeObject('list', `_lists/${list.id}`, list);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const path = getPath(geohash, id);
|
||||
return privateClient.remove(path);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user