Skip to content

Commit

Permalink
chore(board): transform old line ids to new (#1658)
Browse files Browse the repository at this point in the history
* chore(board): transform old line ids to new

* fix(boards): correct timestamp for switch date
  • Loading branch information
emilielr authored Sep 30, 2024
1 parent 52da424 commit b8edd48
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tavla/app/(admin)/edit/[id]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { TTile } from 'types/tile'
import { getWalkingDistance } from 'app/(admin)/components/TileSelector/utils'
import { TLocation } from 'types/meta'
import { revalidatePath } from 'next/cache'
import { makeBoardCompatible } from './compatibility'

initializeAdminApp()

export async function getBoard(bid: TBoardID) {
const board = await firestore().collection('boards').doc(bid).get()
return { id: board.id, ...board.data() } as TBoard
return makeBoardCompatible({ id: board.id, ...board.data() } as TBoard)
}

export async function addTile(bid: TBoardID, tile: TTile) {
Expand Down
99 changes: 99 additions & 0 deletions tavla/app/(admin)/edit/[id]/compatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// TODO: remove 15. december when new lines are active
// Remember to do a migration script in Firestore beforehand

import { TBoard } from 'types/settings'
import { TTile } from 'types/tile'

export const SWITCH_DATE = new Date(2024, 11, 15)

export function makeBoardCompatible(board: TBoard): TBoard {
const updatedTiles = board.tiles.map(({ whitelistedLines, ...tile }) => ({
...tile,
whitelistedLines: whitelistedLines?.flatMap(oldLineIdsToNew),
})) as TTile[]

return { ...board, tiles: updatedTiles }
}

function oldLineIdsToNew(lineId: string): string | string[] {
switch (lineId) {
case 'VYG:Line:41':
return [lineId, 'VYG:Line:F4']
case 'VYG:Line:45':
return [lineId, 'VYG:Line:R40']
case 'VYG:Line:43':
return [lineId, 'VYG:Line:L4']
case 'FLB:Line:42':
return [lineId, 'VYG:Line:R45']
case 'VYG:Line:70':
return [lineId, 'VYG:Line:F1']
case 'NSB:Line:R20':
return [lineId, 'VYG:Line:RE20']
case 'NSB:Line:RX20':
return [lineId, 'VYG:Line:RX20']
case 'NSB:Line:L21':
return [lineId, 'VYG:Line:R21']
case 'NSB:Line:L22':
return [lineId, 'VYG:Line:R22']
case 'NSB:Line:R23':
return [lineId, 'VYG:Line:R23']
case 'NSB:Line:R23x':
return [lineId, 'VYG:Line:R23x']
case 'NSB:Line:L2':
return [lineId, 'VYG:Line:L2']
case 'NSB:Line:L2x':
return [lineId, 'VYG:Line:L2x']
case 'GJB:Line:R30':
return [lineId, 'VYG:Line:RE30']
case 'GJB:Line:L3':
return [lineId, 'VYG:Line:R31']
case 'NSB:Line:R10':
return [lineId, 'VYG:Line:RE10']
case 'NSB:Line:R11':
return [lineId, 'VYG:Line:RE11']
case 'NSB:Line:RX11':
return [lineId, 'VYG:Line:RX11']
case 'NSB:Line:L12':
return [lineId, 'VYG:Line:R12']
case 'NSB:Line:L13':
return [lineId, 'VYG:Line:R13']
case 'NSB:Line:R13x':
return [lineId, 'VYG:Line:R13x']
case 'NSB:Line:L14':
return [lineId, 'VYG:Line:R14']
case 'NSB:Line:52':
return [lineId, 'VYG:Line:R55']
case 'NSB:Line:L1':
return [lineId, 'VYG:Line:L1']

default:
return lineId
}
}

export const OLD_LINE_IDS = [
'VYG:Line:41',
'VYG:Line:45',
'VYG:Line:43',
'FLB:Line:42',
'VYG:Line:70',
'NSB:Line:R20',
'NSB:Line:RX20',
'NSB:Line:L21',
'NSB:Line:L22',
'NSB:Line:R23',
'NSB:Line:R23x',
'NSB:Line:L2',
'NSB:Line:L2x',
'GJB:Line:R30',
'GJB:Line:L3',
'NSB:Line:R10',
'NSB:Line:R11',
'NSB:Line:RX11',
'NSB:Line:L12',
'NSB:Line:L13',
'NSB:Line:R13x',
'NSB:Line:L14',
'NSB:Line:52',
'NSB:Line:L1',
]
10 changes: 9 additions & 1 deletion tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
getFormFeedbackForField,
} from 'app/(admin)/utils'
import { useFormState } from 'react-dom'
import { OLD_LINE_IDS, SWITCH_DATE } from '../../compatibility'

function TileCard({
bid,
Expand Down Expand Up @@ -140,7 +141,7 @@ function TileCard({
setIsOpen(false)
}

const lines = useLines(tile)
let lines = useLines(tile)

if (!lines)
return (
Expand All @@ -149,6 +150,13 @@ function TileCard({
</div>
)

// TODO: remove 15. december when new lines are active
if (Date.now() < Date.parse(SWITCH_DATE.toString())) {
lines = lines.filter((line) => OLD_LINE_IDS.includes(line.id))
} else {
lines = lines.filter((line) => !OLD_LINE_IDS.includes(line.id))
}

const uniqLines = uniqBy(lines, 'id')

const transportModes = uniqBy(uniqLines, 'transportMode')
Expand Down

0 comments on commit b8edd48

Please sign in to comment.