1- import { React , useState , useEffect } from "react" ;
1+ import { React , useState , useEffect , useContext } from "react" ;
22import PropTypes from "prop-types" ;
33import { useTranslation } from "react-i18next" ;
44import MultiSelectComponent from "../../../util/forms/multiselect-dropdown/multi-dropdown" ;
55import { displayError } from "../../../util/list/toast-component/display-toast" ;
6-
6+ import userContext from "../../../../context/user-context" ;
77/**
88 * A multiselect and table for groups.
99 *
@@ -23,13 +23,15 @@ function StationSelector({
2323 const { t } = useTranslation ( "common" , { keyPrefix : "station-selector" } ) ;
2424 const [ data , setData ] = useState ( [ ] ) ;
2525 const [ searchText , setSearchText ] = useState ( "" ) ;
26+ const { config } = useContext ( userContext ) ;
27+
2628 /**
2729 * Adds group to list of groups.
2830 *
2931 * @param {object } props - The props.
3032 * @param {object } props.target - The target.
3133 */
32- const handleAdd = ( { target } ) => {
34+ const handleSelect = ( { target } ) => {
3335 const { value, id : localId } = target ;
3436 onChange ( {
3537 target : { id : localId , value } ,
@@ -44,15 +46,32 @@ function StationSelector({
4446 const onFilter = ( filter ) => {
4547 setSearchText ( filter ) ;
4648 } ;
49+ /**
50+ * Map the data recieved from the midttrafik api.
51+ *
52+ * @param {object } locationData
53+ * @returns {object } The mapped data.
54+ */
55+ const mapLocationData = ( locationData ) => {
56+ return locationData . map ( ( location ) => ( {
57+ id : location . StopLocation . extId ,
58+ name : location . StopLocation . name ,
59+ } ) ) ;
60+ } ;
4761
4862 useEffect ( ( ) => {
63+ const baseUrl = "https://www.rejseplanen.dk/api/location.name" ;
4964 fetch (
50- `https://xmlopen.rejseplanen.dk/bin/rest.exe/location?input=user%20i${ searchText } ?&format=json`
65+ `${ baseUrl } ?${ new URLSearchParams ( {
66+ accessId : config . rejseplanenApiKey || "" ,
67+ format : "json" ,
68+ input : searchText ,
69+ } ) } `
5170 )
5271 . then ( ( response ) => response . json ( ) )
5372 . then ( ( rpData ) => {
54- if ( rpData ?. LocationList ?. StopLocation ) {
55- setData ( rpData . LocationList . StopLocation ) ;
73+ if ( rpData ?. stopLocationOrCoordLocation ) {
74+ setData ( mapLocationData ( rpData . stopLocationOrCoordLocation ) ) ;
5675 }
5776 } )
5877 . catch ( ( er ) => {
@@ -66,8 +85,7 @@ function StationSelector({
6685 < >
6786 < MultiSelectComponent
6887 options = { data }
69- singleSelect
70- handleSelection = { handleAdd }
88+ handleSelection = { handleSelect }
7189 name = { name }
7290 selected = { inputValue || [ ] }
7391 filterCallback = { onFilter }
0 commit comments