Skip to content

Commit

Permalink
fix(TileCard): delete tile based on unique tile id (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
SelmaBergstrand authored Dec 11, 2024
1 parent b6bbf5e commit 5472b46
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@ import {
initializeAdminApp,
} from 'app/(admin)/utils/firebase'
import { redirect } from 'next/navigation'
import { SWITCH_DATE, NEW_LINE_IDS, OLD_LINE_IDS } from '../../compatibility'

initializeAdminApp()

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}`)
Expand All @@ -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}`)
}
Expand Down

0 comments on commit 5472b46

Please sign in to comment.