-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(edit): adds platform dropdown component
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
next-tavla/src/Admin/scenarios/TileSettings/components/PlatformDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TQuaysSearchQuery | undefined>(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 ( | ||
<Dropdown | ||
items={() => 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 } |