Add StopShape to the frontend business data

This commit is contained in:
2023-04-14 11:29:25 +02:00
parent 42817f7b0c
commit 1ffd3cbe94
2 changed files with 36 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
import { batch, createContext, createSignal, JSX } from 'solid-js';
import { createStore } from 'solid-js/store';
import { Line, Lines, Passage, Passages, Stop, Stops } from './types';
import { Line, Lines, Passage, Passages, Stop, StopShape, StopShapes, Stops } from './types';
export interface BusinessDataStore {
@@ -18,6 +18,8 @@ export interface BusinessDataStore {
getStop: (stopId: number) => Stop | undefined;
searchStopByName: (name: string) => Promise<Stops>;
getStopShape: (stopId: number) => Promise<StopShape | undefined>;
};
export const BusinessDataContext = createContext<BusinessDataStore>();
@@ -30,9 +32,10 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
lines: Lines;
passages: Passages;
stops: Stops;
stopShapes: StopShapes;
};
const [store, setStore] = createStore<Store>({ lines: {}, passages: {}, stops: {} });
const [store, setStore] = createStore<Store>({ lines: {}, passages: {}, stops: {}, stopShapes: {} });
const getLine = async (lineId: string): Promise<Line> => {
let line = store.lines[lineId];
@@ -161,6 +164,19 @@ ${linePassagesDestination.length} here... refresh all them.`);
return byIdStops;
}
const getStopShape = async (stopId: number): Promise<StopShape | undefined> => {
let shape = store.stopShapes[stopId];
if (shape === undefined) {
console.log(`No shape found for ${stopId} stop... fetch it from backend.`);
const data = await fetch(`${serverUrl()}/stop_shape/${stopId}`, {
headers: { 'Content-Type': 'application/json' }
});
shape = await data.json();
setStore('stopShapes', stopId, shape);
}
return shape;
}
return (
<BusinessDataContext.Provider value={{
getLine, getLinePassages, getLineDestinations, getDestinationPassages, passages, getPassagesLineIds,

View File

@@ -63,6 +63,24 @@ export class Stop {
export type Stops = Record<number, Stop>;
export type Points = [number, number][];
export class StopShape {
stop_id: number;
type_: number;
bounding_box: number[];
points: Points;
constructor(stop_id: number, type_: number, bounding_box: number[], points: Points) {
this.stop_id = stop_id;
this.type_ = type_;
this.bounding_box = bounding_box;
this.points = points;
}
};
export type StopShapes = Record<number, StopShape>;
export class Line {
id: string;
shortName: string;