diff --git a/next-tavla/app/(admin)/edit/[id]/actions.ts b/next-tavla/app/(admin)/edit/[id]/actions.ts index ef4baedd0..8ed32bcd6 100644 --- a/next-tavla/app/(admin)/edit/[id]/actions.ts +++ b/next-tavla/app/(admin)/edit/[id]/actions.ts @@ -9,6 +9,7 @@ import { TBoard, TBoardID } from 'types/settings' import { TTile } from 'types/tile' import { getWalkingDistance } from 'app/(admin)/components/TileSelector/utils' import { TLocation } from 'types/meta' +import { revalidatePath } from 'next/cache' initializeAdminApp() @@ -47,3 +48,13 @@ export async function getWalkingDistanceTile( }, } } +export async function saveTiles(bid: TBoardID, tiles: TTile[]) { + const access = await hasBoardEditorAccess(bid) + if (!access) return redirect('/') + + await firestore().collection('boards').doc(bid).update({ + tiles: tiles, + 'meta.dateModified': Date.now(), + }) + revalidatePath(`/edit/${bid}`) +} diff --git a/next-tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx b/next-tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx index f347e231f..041ff3c34 100644 --- a/next-tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx +++ b/next-tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx @@ -1,10 +1,22 @@ 'use client' import { BaseExpand } from '@entur/expand' import { TTile } from 'types/tile' -import { Button, IconButton, SecondarySquareButton } from '@entur/button' +import { + Button, + IconButton, + NegativeButton, + SecondarySquareButton, +} from '@entur/button' import { FilterChip } from '@entur/chip' import { Switch, TextField } from '@entur/form' -import { CloseIcon, DeleteIcon, EditIcon, QuestionIcon } from '@entur/icons' +import { + CloseIcon, + DeleteIcon, + DownwardIcon, + EditIcon, + QuestionIcon, + UpwardIcon, +} from '@entur/icons' import { Modal } from '@entur/modal' import { Heading3, @@ -38,12 +50,18 @@ function TileCard({ address, demoBoard, setDemoBoard, + moveItem, + index, + totalTiles, }: { bid: TBoardID tile: TTile address?: TLocation demoBoard?: TBoard setDemoBoard?: Dispatch> + moveItem: (index: number, direction: string) => void + index: number + totalTiles: number }) { const posthog = usePostHog() const [isOpen, setIsOpen] = useState(false) @@ -109,43 +127,75 @@ function TileCard({ return (
-
-
- {tile.name} -
- {transportModes.map((tm) => ( - - ))} +
+
+
+ {tile.name} +
+ {transportModes.map((tm) => ( + + ))} +
+
+ +
+ { + if (changed) return setConfirmOpen(true) + setIsOpen(!isOpen) + }} + aria-label="Rediger stoppested" + > + {isOpen ? : } +
-
- { - bid === 'demo' - ? removeTileFromDemoBoard(tile) - : await deleteTile(bid, tile) - }} - aria-label="Slett stoppested" - > - - - { - if (changed) return setConfirmOpen(true) - setIsOpen(!isOpen) - }} - aria-label="Rediger stoppested" - > - {isOpen ? : } - +
+ {index !== 0 && ( + { + moveItem(index, 'up') + }} + aria-label="Flytt opp" + className="ml-2 *:!border-gray-300" + > + { + moveItem(index, 'up') + }} + aria-label="Flytt opp" + /> + + )} + {index !== totalTiles - 1 && ( + { + moveItem(index, 'down') + }} + aria-label="Flytt ned" + className="ml-2 *:!border-gray-300" + > + + + )}
-
+
{ @@ -297,10 +347,28 @@ function TileCard({ value={uniqLines.length.toString()} /> -
- - Lagre endringer +
+ + Lagre valg + + { + bid === 'demo' + ? removeTileFromDemoBoard(tile) + : await deleteTile(bid, tile) + }} + aria-label="Slett stoppested" + type="button" + > + + Fjern stoppested +
> +}) { + const [array, setArray] = useState(board.tiles) + + useEffect(() => { + setArray(board.tiles) + }, [board.tiles]) + + const moveItem = (index: number, direction: string) => { + const newIndex: number = direction === 'up' ? index - 1 : index + 1 + if (newIndex < 0 || newIndex >= board.tiles.length) { + return + } + + const newArray: TTile[] = [...board.tiles] + + const oldElement = newArray[newIndex] + + newArray[newIndex] = newArray[index] as TTile + newArray[index] = oldElement as TTile + + setArray(newArray) + if (bid === 'demo' && setDemoBoard) { + const newBoard: TBoard = { ...board, tiles: newArray } + setDemoBoard(newBoard ?? board) + } else { + saveTiles(board.id ?? '', newArray) + } + } + const debouncedSave = debounce(moveItem, 150) + return ( +
+ {array.map((tile, index) => ( + + ))} +
+ ) +} +export { TileList } diff --git a/next-tavla/app/(admin)/edit/[id]/page.tsx b/next-tavla/app/(admin)/edit/[id]/page.tsx index c25884ee9..71d21e09e 100644 --- a/next-tavla/app/(admin)/edit/[id]/page.tsx +++ b/next-tavla/app/(admin)/edit/[id]/page.tsx @@ -2,7 +2,6 @@ import { redirect } from 'next/navigation' import { TBoardID } from 'types/settings' import { addTile, getBoard, getWalkingDistanceTile } from './actions' import { Heading1, Heading2 } from '@entur/typography' -import { TileCard } from './components/TileCard' import { MetaSettings } from './components/MetaSettings' import { TileSelector } from 'app/(admin)/components/TileSelector' import { formDataToTile } from 'app/(admin)/components/TileSelector/utils' @@ -18,8 +17,9 @@ import { DEFAULT_BOARD_NAME } from 'app/(admin)/utils/constants' import { Preview } from './components/Preview' import { ActionsMenu } from './components/ActionsMenu' import { ThemeSelect } from './components/ThemeSelect' +import { TileList } from './components/TileList' -type TProps = { +export type TProps = { params: { id: TBoardID } } @@ -40,7 +40,6 @@ export default async function EditPage({ params }: TProps) { const access = await hasBoardEditorAccess(params.id) if (!access) return redirect('/') - return (
@@ -65,7 +64,6 @@ export default async function EditPage({ params }: TProps) { />
-
Stoppesteder i tavlen - {board.tiles.map((tile) => ( - - ))} + +
diff --git a/next-tavla/app/demo/components/DemoBoard.tsx b/next-tavla/app/demo/components/DemoBoard.tsx index 11b1978f6..7222d0cf0 100644 --- a/next-tavla/app/demo/components/DemoBoard.tsx +++ b/next-tavla/app/demo/components/DemoBoard.tsx @@ -3,10 +3,9 @@ import { Heading2 } from '@entur/typography' import { TileSelector } from 'app/(admin)/components/TileSelector' import { formDataToTile } from 'app/(admin)/components/TileSelector/utils' import { Preview } from 'app/(admin)/edit/[id]/components/Preview' -import { TileCard } from 'app/(admin)/edit/[id]/components/TileCard' import useLocalStorage from '../../(admin)/hooks/useLocalStorage' -import { TTile } from 'types/tile' import { usePostHog } from 'posthog-js/react' +import { TileList } from 'app/(admin)/edit/[id]/components/TileList' const emptyDemoBoard = { id: 'demo', @@ -31,15 +30,7 @@ function DemoBoard() { }} col={false} /> - {board.tiles?.map((tile: TTile) => ( - - ))} +
Forhåndsvisning