🐛 Check backend return codes before handling data
This commit is contained in:
@@ -21,7 +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>;
|
getStopDestinations: (stopId: number) => Promise<StopDestinations | undefined>;
|
||||||
getStopShape: (stopId: number) => Promise<StopShape | undefined>;
|
getStopShape: (stopId: number) => Promise<StopShape | undefined>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,11 +44,22 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
|
|||||||
let line = store.lines[lineId];
|
let line = store.lines[lineId];
|
||||||
if (line === undefined) {
|
if (line === undefined) {
|
||||||
console.log(`${lineId} not found... fetch it from backend.`);
|
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' }
|
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;
|
return line;
|
||||||
}
|
}
|
||||||
@@ -91,12 +102,19 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const refreshPassages = async (stopId: number): Promise<void> => {
|
const refreshPassages = async (stopId: number): Promise<void> => {
|
||||||
const httpOptions = { headers: { "Content-Type": "application/json" } };
|
|
||||||
console.log(`Fetching data for ${stopId}`);
|
console.log(`Fetching data for ${stopId}`);
|
||||||
const data = await fetch(`${serverUrl()}/stop/${stopId}/nextPassages`, httpOptions);
|
const httpOptions = { headers: { "Content-Type": "application/json" } };
|
||||||
const response = await data.json();
|
const response = await fetch(`${serverUrl()}/stop/${stopId}/nextPassages`, httpOptions);
|
||||||
_cleanupPassages(response.passages);
|
|
||||||
addPassages(response.passages);
|
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 => {
|
const addPassages = (passages: Passages): void => {
|
||||||
@@ -155,39 +173,58 @@ ${linePassagesDestination.length} here... refresh all them.`);
|
|||||||
}
|
}
|
||||||
|
|
||||||
const searchStopByName = async (name: string): Promise<Stops> => {
|
const searchStopByName = async (name: string): Promise<Stops> => {
|
||||||
const data = await fetch(`${serverUrl()}/stop/?name=${name}`, {
|
const byIdStops: Stops = {};
|
||||||
|
|
||||||
|
const response = await fetch(`${serverUrl()}/stop/?name=${name}`, {
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
const stops = await data.json();
|
|
||||||
|
|
||||||
const byIdStops: Stops = {};
|
const json = await response.json();
|
||||||
for (const stop of stops) {
|
|
||||||
byIdStops[stop.id] = stop;
|
if (response.ok) {
|
||||||
setStore('stops', stop.id, stop);
|
for (const stop of json) {
|
||||||
for (const innerStop of stop.stops) {
|
byIdStops[stop.id] = stop;
|
||||||
setStore('stops', innerStop.id, innerStop);
|
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;
|
return byIdStops;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getStopDestinations = async (stopId: number): Promise<StopDestinations> => {
|
const getStopDestinations = async (stopId: number): Promise<StopDestinations | undefined> => {
|
||||||
const data = await fetch(`${serverUrl()}/stop/${stopId}/destinations`, {
|
const response = await fetch(`${serverUrl()}/stop/${stopId}/destinations`, {
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
const response = await data.json();
|
const destinations = response.ok ? await response.json() : undefined;
|
||||||
return response;
|
return destinations;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
console.log(`No shape found for ${stopId} stop... fetch it from backend.`);
|
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' }
|
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;
|
return shape;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user