From 3eaf9cff9b8dc0ab1300721af382c0447f18dba2 Mon Sep 17 00:00:00 2001 From: stianjsu Date: Wed, 5 Jul 2023 11:53:01 +0200 Subject: [PATCH] feat(edit): adds platform dropdown component --- .../components/PlatformDropdown.tsx | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 next-tavla/src/Admin/scenarios/TilesSettings/components/PlatformDropdown.tsx diff --git a/next-tavla/src/Admin/scenarios/TilesSettings/components/PlatformDropdown.tsx b/next-tavla/src/Admin/scenarios/TilesSettings/components/PlatformDropdown.tsx new file mode 100644 index 0000000000..5998f3648c --- /dev/null +++ b/next-tavla/src/Admin/scenarios/TilesSettings/components/PlatformDropdown.tsx @@ -0,0 +1,62 @@ +import { Dropdown } from '@entur/dropdown' +import { TAnonTiles } from 'Admin/types' +import { QuaysSearchQuery, TQuaysSearchQuery } from 'graphql/index' +import { fetchQuery } from 'graphql/utils' +import { useEffect, useState } from 'react' +import { isNotNullOrUndefined } from 'utils/typeguards' + +const stopPlaceOption = { value: 'stopPlace', label: 'Vis Alle' } as const + +function PlatformDropdown({ + setTile, + stopPlaceId, +}: { + setTile: (tile: TAnonTiles) => void + stopPlaceId: string +}) { + const [data, setData] = useState(undefined) + + useEffect(() => { + if (!stopPlaceId) return + fetchQuery(QuaysSearchQuery, { stopPlaceId }).then(setData) + }, [stopPlaceId]) + + const quays = + data?.stopPlace?.quays + ?.filter(isNotNullOrUndefined) + .map((quay, index) => ({ + value: quay.id, + label: + 'Platform ' + + [quay.publicCode ?? index + 1, quay.description].join(' '), + })) || [] + + const dropDownOptions = quays.length + ? [{ ...stopPlaceOption }, ...quays] + : [] + + return ( + dropDownOptions} + label="Velg plattform" + disabled={!stopPlaceId} + onChange={(e) => { + if (e?.value) { + if (e.value == 'stopPlace') + setTile({ + type: 'stop_place', + placeId: stopPlaceId, + }) + else + setTile({ + type: 'quay', + stopPlaceId, + placeId: e.value, + }) + } + }} + /> + ) +} + +export { PlatformDropdown }