From 5472b464099400e10a8042d7f7861b3f2688a244 Mon Sep 17 00:00:00 2001 From: SelmaBergstrand <78426277+SelmaBergstrand@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:11:37 +0100 Subject: [PATCH] fix(TileCard): delete tile based on unique tile id (#1761) --- .../edit/[id]/components/TileCard/actions.ts | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts b/tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts index 037941728..f3b4b703d 100644 --- a/tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts +++ b/tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts @@ -9,7 +9,6 @@ import { initializeAdminApp, } from 'app/(admin)/utils/firebase' import { redirect } from 'next/navigation' -import { SWITCH_DATE, NEW_LINE_IDS, OLD_LINE_IDS } from '../../compatibility' initializeAdminApp() @@ -17,24 +16,15 @@ export async function deleteTile(bid: TBoardID, tile: TTile) { const access = await hasBoardOwnerAccess(bid) if (!access) return redirect('/') - // TODO: refactor 15. december when new lines are active - if (tile.whitelistedLines) { - if (Date.now() < Date.parse(SWITCH_DATE.toString())) { - tile.whitelistedLines = tile.whitelistedLines.filter( - (line) => !NEW_LINE_IDS.includes(line), - ) - } else { - tile.whitelistedLines = tile.whitelistedLines.filter( - (line) => !OLD_LINE_IDS.includes(line), - ) - } - } + const boardRef = firestore().collection('boards').doc(bid) + const board = (await boardRef.get()).data() as TBoard + const tileToDelete = board.tiles.find((t) => t.uuid === tile.uuid) await firestore() .collection('boards') .doc(bid) .update({ - tiles: firestore.FieldValue.arrayRemove(tile), + tiles: firestore.FieldValue.arrayRemove(tileToDelete), 'meta.dateModified': Date.now(), }) revalidatePath(`/edit/${bid}`) @@ -44,26 +34,26 @@ export async function saveTile(bid: TBoardID, tile: TTile) { const access = await hasBoardEditorAccess(bid) if (!access) return redirect('/') - const docRef = firestore().collection('boards').doc(bid) - const doc = (await docRef.get()).data() as TBoard - const oldTile = doc.tiles.find((t) => t.uuid === tile.uuid) - if (!oldTile) - return docRef.update({ + const boardRef = firestore().collection('boards').doc(bid) + const board = (await boardRef.get()).data() as TBoard + const existingTile = board.tiles.find((t) => t.uuid === tile.uuid) + if (!existingTile) + return boardRef.update({ tiles: firestore.FieldValue.arrayUnion(tile), 'meta.dateModified': Date.now(), }) - const index = doc.tiles.indexOf(oldTile) + const indexExistingTile = board.tiles.indexOf(existingTile) - if (tile.displayName !== undefined) { - doc.tiles[index] = { + if (tile.displayName) { + board.tiles[indexExistingTile] = { ...tile, - displayName: tile.displayName?.substring(0, 50), + displayName: tile.displayName.substring(0, 50), } } else { - doc.tiles[index] = tile + board.tiles[indexExistingTile] = tile } - docRef.update({ tiles: doc.tiles, 'meta.dateModified': Date.now() }) + boardRef.update({ tiles: board.tiles, 'meta.dateModified': Date.now() }) revalidatePath(`/edit/${bid}`) }