2024-02-02 23:52:52 +00:00
|
|
|
import {Autocomplete, TextField} from "@mui/material";
|
|
|
|
import {useRef, useState} from "react";
|
|
|
|
|
|
|
|
function AutocompleteStop(params) {
|
|
|
|
const [options, setOptions] = useState([])
|
|
|
|
const previousController = useRef()
|
|
|
|
|
|
|
|
function onInputChange(event, value) {
|
|
|
|
if (!value) {
|
|
|
|
setOptions([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousController.current)
|
|
|
|
previousController.current.abort()
|
|
|
|
|
|
|
|
const controller = new AbortController()
|
|
|
|
const signal = controller.signal
|
|
|
|
previousController.current = controller
|
|
|
|
fetch("/api/gtfs/stop/?location_type=1&search=" + value, {signal})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => data.results)
|
|
|
|
.then(setOptions)
|
|
|
|
.catch()
|
|
|
|
}
|
|
|
|
|
|
|
|
return <>
|
|
|
|
<Autocomplete
|
|
|
|
id="stop"
|
|
|
|
options={options}
|
|
|
|
onInputChange={onInputChange}
|
|
|
|
filterOptions={(x) => x}
|
|
|
|
getOptionKey={option => option.id}
|
|
|
|
getOptionLabel={option => option.name}
|
2024-02-10 16:33:36 +00:00
|
|
|
groupBy={option => getOptionGroup(option)}
|
2024-02-02 23:52:52 +00:00
|
|
|
isOptionEqualToValue={(option, value) => option.id === value.id}
|
|
|
|
renderInput={(params) => <TextField {...params} label="Arrêt" />}
|
|
|
|
{...params} />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:33:36 +00:00
|
|
|
function getOptionGroup(option) {
|
2024-05-09 17:28:19 +00:00
|
|
|
switch (option.gtfs_feed) {
|
|
|
|
case "FR-SNCF-TGV":
|
|
|
|
case "FR-SNCF-IC":
|
|
|
|
case "FR-SNCF-TER":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "TGV/TER/Intercités"
|
2024-05-09 17:28:19 +00:00
|
|
|
case "FR-IDF-TN":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "Transilien"
|
2024-05-09 17:28:19 +00:00
|
|
|
case "FR-EUROSTAR":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "Eurostar"
|
2024-05-09 17:28:19 +00:00
|
|
|
case "IT-FRA-TI":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "Trenitalia France"
|
2024-05-09 17:28:19 +00:00
|
|
|
case "ES-RENFE":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "RENFE"
|
2024-05-09 17:28:19 +00:00
|
|
|
case "AT-OBB":
|
2024-02-10 16:33:36 +00:00
|
|
|
return "ÖBB"
|
|
|
|
default:
|
2024-05-09 17:28:19 +00:00
|
|
|
return option.gtfs_feed
|
2024-02-10 16:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 23:52:52 +00:00
|
|
|
export default AutocompleteStop;
|