From 8615286c459a0f38b4770338cff57c1b4c78bb1e Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Wed, 6 Sep 2023 22:43:51 +0200 Subject: [PATCH] Opravy TypeScriptu --- client/src/App.tsx | 16 ++++++++-------- server/src/service.ts | 20 ++++++++++---------- types/Types.ts | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 2818963..9dfdc6a 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -43,7 +43,7 @@ function App() { const bank = useBank(); const [isConnected, setIsConnected] = useState(false); const [data, setData] = useState(); - const [food, setFood] = useState<{ [key in Restaurants]: Menu }>(); + const [food, setFood] = useState<{ [key in Restaurants]?: Menu }>(); const [myOrder, setMyOrder] = useState(); const [foodChoiceList, setFoodChoiceList] = useState(); const [closed, setClosed] = useState(false); @@ -144,8 +144,8 @@ function App() { const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); if (restaurantKey > -1 && food) { const restaurant = Object.values(Restaurants)[restaurantKey]; - setFoodChoiceList(food[restaurant].food); - setClosed(food[restaurant].closed); + setFoodChoiceList(food[restaurant]?.food); + setClosed(food[restaurant]?.closed ?? false); } else { setFoodChoiceList(undefined); setClosed(false); @@ -372,9 +372,9 @@ function App() { } - {renderFoodTable('Sladovnická', food[Restaurants.SLADOVNICKA])} - {renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])} - {renderFoodTable('TechTower', food[Restaurants.TECHTOWER])} + {food[Restaurants.SLADOVNICKA] && renderFoodTable('Sladovnická', food[Restaurants.SLADOVNICKA])} + {food[Restaurants.UMOTLIKU] && renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])} + {food[Restaurants.TECHTOWER] && renderFoodTable('TechTower', food[Restaurants.TECHTOWER])}
@@ -390,7 +390,7 @@ function App() { const locationsKey = Object.keys(Locations)[locationIndex]; const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); const v = Object.values(Restaurants)[restaurantKey]; - return v == null || !food[v].closed; + return v == null || !food[v]?.closed; }) .map(entry => )} @@ -444,7 +444,7 @@ function App() { const locationsKey = Object.keys(Locations)[Number(locationKey)] const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); const restaurant = Object.values(Restaurants)[restaurantKey]; - const foodName = food[restaurant].food[foodIndex].name; + const foodName = food[restaurant]?.food[foodIndex].name; return
  • {foodName} {login === auth.login && { diff --git a/server/src/service.ts b/server/src/service.ts index 38957ea..3dbdd89 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -91,13 +91,13 @@ export async function savePizzaList(pizzaList: Pizza[]): Promise { */ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise { await initIfNeeded(date); - const today = formatDate(date ?? getToday()); - const clientData: ClientData = await storage.getData(today); + const selectedDay = formatDate(date ?? getToday()); + const clientData: ClientData = await storage.getData(selectedDay); if (!clientData.menus) { clientData.menus = {}; - storage.setData(today, clientData); + storage.setData(selectedDay, clientData); } - if (!clientData?.menus?.[restaurant]) { + if (!clientData.menus[restaurant]) { clientData.menus[restaurant] = { lastUpdate: getHumanTime(new Date()), closed: false, @@ -106,22 +106,22 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P const mock = process.env.MOCK_DATA === 'true'; switch (restaurant) { case Restaurants.SLADOVNICKA: - clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock); + clientData.menus[restaurant]!.food = await getMenuSladovnicka(date, mock); break; case Restaurants.UMOTLIKU: const uMotlikuFood = await getMenuUMotliku(date, mock); - clientData.menus[restaurant].food = uMotlikuFood; + clientData.menus[restaurant]!.food = uMotlikuFood; if (uMotlikuFood.length === 1 && uMotlikuFood[0].name.toLowerCase() === 'zavřeno') { - clientData.menus[restaurant].closed = true; + clientData.menus[restaurant]!.closed = true; } break; case Restaurants.TECHTOWER: - clientData.menus[restaurant].food = await getMenuTechTower(date, mock); + clientData.menus[restaurant]!.food = await getMenuTechTower(date, mock); break; } - storage.setData(today, clientData); + storage.setData(selectedDay, clientData); } - return clientData?.menus?.[restaurant]; + return clientData.menus[restaurant]!; } /** diff --git a/types/Types.ts b/types/Types.ts index 591f634..118cb06 100644 --- a/types/Types.ts +++ b/types/Types.ts @@ -72,7 +72,7 @@ export interface ClientData { isWeekend: boolean, // příznak, zda je dnes víkend weekIndex: number, // index aktuálního dne v týdnu (0-6) choices: Choices, // seznam voleb - menus?: { [restaurant in Restaurants]: Menu }, // menu jednotlivých restaurací + menus?: { [restaurant in Restaurants]?: Menu }, // menu jednotlivých restaurací pizzaDay?: PizzaDay, // pizza day pro dnešní den, pokud existuje pizzaList?: Pizza[], // seznam dostupných pizz pro dnešní den pizzaListLastUpdate?: Date, // datum a čas poslední aktualizace pizz