From 1ffd3cbe94ce64d941e59d9e510e089c537b74e0 Mon Sep 17 00:00:00 2001 From: Adrien Date: Fri, 14 Apr 2023 11:29:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20StopShape=20to=20the=20fronte?= =?UTF-8?q?nd=20business=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/businessData.tsx | 20 ++++++++++++++++++-- frontend/src/types.tsx | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/frontend/src/businessData.tsx b/frontend/src/businessData.tsx index 53355f4..f1238eb 100644 --- a/frontend/src/businessData.tsx +++ b/frontend/src/businessData.tsx @@ -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; + + getStopShape: (stopId: number) => Promise; }; export const BusinessDataContext = createContext(); @@ -30,9 +32,10 @@ export function BusinessDataProvider(props: { children: JSX.Element }) { lines: Lines; passages: Passages; stops: Stops; + stopShapes: StopShapes; }; - const [store, setStore] = createStore({ lines: {}, passages: {}, stops: {} }); + const [store, setStore] = createStore({ lines: {}, passages: {}, stops: {}, stopShapes: {} }); const getLine = async (lineId: string): Promise => { let line = store.lines[lineId]; @@ -161,6 +164,19 @@ ${linePassagesDestination.length} here... refresh all them.`); return byIdStops; } + const getStopShape = async (stopId: number): Promise => { + 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 ( ; +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; + export class Line { id: string; shortName: string;