Add StopDestinations to the frontend business data

This commit is contained in:
2023-04-14 11:33:29 +02:00
parent 1ffd3cbe94
commit 1b713dbc0e

View File

@@ -4,6 +4,8 @@ import { createStore } from 'solid-js/store';
import { Line, Lines, Passage, Passages, Stop, StopShape, StopShapes, Stops } from './types'; import { Line, Lines, Passage, Passages, Stop, StopShape, StopShapes, Stops } from './types';
export type StopDestinations = Record<string, string[]>;
export interface BusinessDataStore { export interface BusinessDataStore {
getLine: (lineId: string) => Promise<Line>; getLine: (lineId: string) => Promise<Line>;
getLinePassages: (lineId: string) => Record<string, Passage[]>; getLinePassages: (lineId: string) => Record<string, Passage[]>;
@@ -19,6 +21,7 @@ export interface BusinessDataStore {
getStop: (stopId: number) => Stop | undefined; getStop: (stopId: number) => Stop | undefined;
searchStopByName: (name: string) => Promise<Stops>; searchStopByName: (name: string) => Promise<Stops>;
getStopDestinations: (stopId: number) => Promise<StopDestinations>;
getStopShape: (stopId: number) => Promise<StopShape | undefined>; getStopShape: (stopId: number) => Promise<StopShape | undefined>;
}; };
@@ -58,6 +61,7 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
return Object.keys(store.passages[lineId]); return Object.keys(store.passages[lineId]);
} }
// TODO: Remove this method: it's based on the next passages and return nothing until the refreshPassages is called.
const getDestinationPassages = (lineId: string, destination: string): Passage[] => { const getDestinationPassages = (lineId: string, destination: string): Passage[] => {
return store.passages[lineId][destination]; return store.passages[lineId][destination];
} }
@@ -164,6 +168,14 @@ ${linePassagesDestination.length} here... refresh all them.`);
return byIdStops; return byIdStops;
} }
const getStopDestinations = async (stopId: number): Promise<StopDestinations> => {
const data = await fetch(`${serverUrl()}/stop/${stopId}/destinations`, {
headers: { 'Content-Type': 'application/json' }
});
const response = await data.json();
return response;
}
const getStopShape = async (stopId: number): Promise<StopShape | undefined> => { const getStopShape = async (stopId: number): Promise<StopShape | undefined> => {
let shape = store.stopShapes[stopId]; let shape = store.stopShapes[stopId];
if (shape === undefined) { if (shape === undefined) {
@@ -180,7 +192,8 @@ ${linePassagesDestination.length} here... refresh all them.`);
return ( return (
<BusinessDataContext.Provider value={{ <BusinessDataContext.Provider value={{
getLine, getLinePassages, getLineDestinations, getDestinationPassages, passages, getPassagesLineIds, getLine, getLinePassages, getLineDestinations, getDestinationPassages, passages, getPassagesLineIds,
refreshPassages, addPassages, clearPassages, getStop, searchStopByName refreshPassages, addPassages, clearPassages,
getStop, getStopDestinations, getStopShape, searchStopByName
}}> }}>
{props.children} {props.children}
</BusinessDataContext.Provider> </BusinessDataContext.Provider>