Skip to content
Snippets Groups Projects
Commit d978d3d6 authored by Niklas Schrötler's avatar Niklas Schrötler
Browse files

MensaplanPanel: Added handling for failing requests

parent e36e901e
No related branches found
No related tags found
No related merge requests found
...@@ -29,85 +29,92 @@ const MensaplanPanel = (props: {definition: MensaPanelDefinition}) => { ...@@ -29,85 +29,92 @@ const MensaplanPanel = (props: {definition: MensaPanelDefinition}) => {
useEffect(() => { useEffect(() => {
const update = async () => { const update = async () => {
// Determine day to fetch for try {
const now = new Date(); // Determine day to fetch for
let fetchFor: string; const now = new Date();
let fetchFor: string;
if(
now.getHours() > props.definition.closingTime.hours || ( if (
now.getHours() === props.definition.closingTime.hours && now.getHours() > props.definition.closingTime.hours || (
now.getMinutes() > props.definition.closingTime.minutes now.getHours() === props.definition.closingTime.hours &&
) now.getMinutes() > props.definition.closingTime.minutes
) { )
// After closing, fetch for next day ) {
setRelativeDay("morgen"); // After closing, fetch for next day
const tomorrow = new Date(now.setTime(now.getTime() + 24 * 60 * 60 * 1000)); setRelativeDay("morgen");
fetchFor = toYYYYMMDD(tomorrow); const tomorrow = new Date(now.setTime(now.getTime() + 24 * 60 * 60 * 1000));
} else { fetchFor = toYYYYMMDD(tomorrow);
// otherwise, fetch for today } else {
setRelativeDay("heute"); // otherwise, fetch for today
fetchFor = toYYYYMMDD(now); setRelativeDay("heute");
fetchFor = toYYYYMMDD(now);
}
// Request the API
const request = await fetch(`https://infoscreen.oh14.de/canteen-menu/v3/canteens/${props.definition.canteenId}/${fetchFor}`);
if (!(request.status === 200)) {
menus.current = [];
specials.current = [];
return;
}
const data = await request.json() as CanteenAPIResponse;
console.log(data);
const old_menus_count = menus.current.length;
const old_specials_count = menus.current.length;
// ToDo: This needs to be cleaned up!
menus.current = data
.filter(d => d.counter !== "Beilagen")
.filter(d => d.counter !== "Aktionsteller")
.sort((a, b) => {
return a.position - b.position
})
.map(d => ({
name: (d.title.de
.split(" | ")
.at(0) ?? "Name nicht bekannt")
.replace(" nach Wahl", ""),
details: d.title.de
.split(" | ")
.slice(1, -1)
.join(", ")
.replace(" nach Wahl", ""),
typeIcons: d.type.map(typeToIcon).filter(i => i !== null) as unknown as React.FC<any>[]
}))
specials.current = data
.filter(d => d.counter === "Aktionsteller")
.sort((a, b) => {
return a.position - b.position
})
.map(d => ({
name: (d.title.de
.split(" | ")
.at(0) ?? "Name nicht bekannt")
.replace(" nach Wahl", ""),
details: d.title.de
.split(" | ")
.slice(1, -1)
.join(", ")
.replace(" nach Wahl", ""),
typeIcons: d.type.map(typeToIcon).filter(i => i !== null) as unknown as React.FC<any>[]
}))
// If the count of menus and specials changed, reset the cycler
if (menus.current.length !== old_menus_count || specials.current.length !== old_specials_count) {
setDishes(menus.current);
cycle.current = 0;
setGroupName("Menüs")
}
} }
catch (e) {
// Request the API console.warn("MensaPlan not showing data because", e);
const request = await fetch(`https://infoscreen.oh14.de/canteen-menu/v3/canteens/${props.definition.canteenId}/${fetchFor}`);
if(!(request.status === 200)) {
menus.current = []; menus.current = [];
specials.current = []; specials.current = [];
return;
}
const data = await request.json() as CanteenAPIResponse;
console.log(data);
const old_menus_count = menus.current.length;
const old_specials_count = menus.current.length;
// ToDo: This needs to be cleaned up!
menus.current = data
.filter(d => d.counter !== "Beilagen")
.filter(d => d.counter !== "Aktionsteller")
.sort((a, b) => {
return a.position - b.position
})
.map(d => ({
name: (d.title.de
.split(" | ")
.at(0) ?? "Name nicht bekannt")
.replace(" nach Wahl", ""),
details: d.title.de
.split(" | ")
.slice(1,-1)
.join(", ")
.replace(" nach Wahl", ""),
typeIcons: d.type.map(typeToIcon).filter(i => i !== null) as unknown as React.FC<any>[]
}))
specials.current = data
.filter(d => d.counter === "Aktionsteller")
.sort((a, b) => {
return a.position - b.position
})
.map(d => ({
name: (d.title.de
.split(" | ")
.at(0) ?? "Name nicht bekannt")
.replace(" nach Wahl", ""),
details: d.title.de
.split(" | ")
.slice(1,-1)
.join(", ")
.replace(" nach Wahl", ""),
typeIcons: d.type.map(typeToIcon).filter(i => i !== null) as unknown as React.FC<any>[]
}))
// If the count of menus and specials changed, reset the cycler
if(menus.current.length !== old_menus_count || specials.current.length !== old_specials_count) {
setDishes(menus.current);
cycle.current = 0;
setGroupName("Menüs")
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment