trainvel/trainvel-front/src/AutocompleteStop.jsx

64 lines
1.6 KiB
React
Raw Normal View History

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}
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} />
</>
}
function getOptionGroup(option) {
switch (option.gtfs_feed) {
case "FR-SNCF-TGV":
case "FR-SNCF-IC":
case "FR-SNCF-TER":
return "TGV/TER/Intercités"
case "FR-IDF-TN":
return "Transilien"
case "FR-EUROSTAR":
return "Eurostar"
case "IT-FRA-TI":
return "Trenitalia France"
case "ES-RENFE":
return "RENFE"
case "AT-OBB":
return "ÖBB"
default:
return option.gtfs_feed
}
}
2024-02-02 23:52:52 +00:00
export default AutocompleteStop;