diff --git a/frontend/src/businessData.tsx b/frontend/src/businessData.tsx index 81d7e4c..149aa44 100644 --- a/frontend/src/businessData.tsx +++ b/frontend/src/businessData.tsx @@ -21,7 +21,7 @@ export interface BusinessDataStore { getStop: (stopId: number) => Stop | undefined; searchStopByName: (name: string) => Promise; - getStopDestinations: (stopId: number) => Promise; + getStopDestinations: (stopId: number) => Promise; getStopShape: (stopId: number) => Promise; }; @@ -44,11 +44,22 @@ export function BusinessDataProvider(props: { children: JSX.Element }) { let line = store.lines[lineId]; if (line === undefined) { console.log(`${lineId} not found... fetch it from backend.`); - const data = await fetch(`${serverUrl()}/line/${lineId}`, { + + const response = await fetch(`${serverUrl()}/line/${lineId}`, { headers: { 'Content-Type': 'application/json' } }); - line = await data.json(); - setStore('lines', lineId, line); + + const json = await response.json(); + + if (response.ok) { + setStore('lines', lineId, json); + line = json; + } + else { + console.warn(`No line found for ${lineId} line id (${json}).`); + } + + } return line; } @@ -91,12 +102,19 @@ export function BusinessDataProvider(props: { children: JSX.Element }) { } const refreshPassages = async (stopId: number): Promise => { - const httpOptions = { headers: { "Content-Type": "application/json" } }; console.log(`Fetching data for ${stopId}`); - const data = await fetch(`${serverUrl()}/stop/${stopId}/nextPassages`, httpOptions); - const response = await data.json(); - _cleanupPassages(response.passages); - addPassages(response.passages); + const httpOptions = { headers: { "Content-Type": "application/json" } }; + const response = await fetch(`${serverUrl()}/stop/${stopId}/nextPassages`, httpOptions); + + const json = await response.json(); + + if (response.ok) { + _cleanupPassages(json.passages); + addPassages(json.passages); + } + else { + console.warn(`No passage found for ${stopId} stop (${json}).`); + } } const addPassages = (passages: Passages): void => { @@ -155,39 +173,58 @@ ${linePassagesDestination.length} here... refresh all them.`); } const searchStopByName = async (name: string): Promise => { - const data = await fetch(`${serverUrl()}/stop/?name=${name}`, { + const byIdStops: Stops = {}; + + const response = await fetch(`${serverUrl()}/stop/?name=${name}`, { headers: { 'Content-Type': 'application/json' } }); - const stops = await data.json(); - const byIdStops: Stops = {}; - for (const stop of stops) { - byIdStops[stop.id] = stop; - setStore('stops', stop.id, stop); - for (const innerStop of stop.stops) { - setStore('stops', innerStop.id, innerStop); + const json = await response.json(); + + if (response.ok) { + for (const stop of json) { + byIdStops[stop.id] = stop; + setStore('stops', stop.id, stop); + + if (stop.stops !== undefined) { + for (const innerStop of stop.stops) { + setStore('stops', innerStop.id, innerStop); + } + } } } + else { + console.warn(`No stop found for '${name}' (${json}).`); + } + return byIdStops; } - const getStopDestinations = async (stopId: number): Promise => { - const data = await fetch(`${serverUrl()}/stop/${stopId}/destinations`, { + const getStopDestinations = async (stopId: number): Promise => { + const response = await fetch(`${serverUrl()}/stop/${stopId}/destinations`, { headers: { 'Content-Type': 'application/json' } }); - const response = await data.json(); - return response; + const destinations = response.ok ? await response.json() : undefined; + return destinations; } 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/${stopId}/shape`, { + const response = await fetch(`${serverUrl()}/stop/${stopId}/shape`, { headers: { 'Content-Type': 'application/json' } }); - shape = await data.json(); - setStore('stopShapes', stopId, shape); + + const json = await response.json(); + + if (response.ok) { + setStore('stopShapes', stopId, json); + shape = json; + } + else { + console.warn(`No shape found for ${stopId} stop (${json}).`); + } } return shape; }