From 2b9d817af515dab2ecf248a0ba61d168bd2bf9b7 Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Fri, 10 Nov 2023 20:22:47 +0100 Subject: [PATCH] =?UTF-8?q?Zobrazov=C3=A1n=C3=AD=20data=20u=20posledn?= =?UTF-8?q?=C3=AD=20aktualizace=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/App.tsx | 3 ++- client/src/Utils.tsx | 14 +++++++++++++- server/src/service.ts | 5 +++-- types/Types.ts | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 5bb0713..4ed696b 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -20,6 +20,7 @@ import { faChainBroken, faChevronLeft, faChevronRight, faGear, faSatelliteDish, import Loader from './components/Loader'; import { getData, errorHandler, getQrUrl } from './api/Api'; import { addChoice, removeChoices, removeChoice, changeDepartureTime, jdemeObed } from './api/FoodApi'; +import { getHumanDateTime } from './Utils'; const EVENT_CONNECT = "connect" @@ -315,7 +316,7 @@ function App() { } return

{name}

- {menu?.lastUpdate && Poslední aktualizace: {menu.lastUpdate}} + {menu?.lastUpdate && Poslední aktualizace: {getHumanDateTime(new Date(menu.lastUpdate))}} {content} } diff --git a/client/src/Utils.tsx b/client/src/Utils.tsx index bfece64..e60e9f2 100644 --- a/client/src/Utils.tsx +++ b/client/src/Utils.tsx @@ -35,4 +35,16 @@ export const getToken = (): string | null => { */ export const deleteToken = () => { localStorage.removeItem(TOKEN_KEY); -} \ No newline at end of file +} + +/** + * Vrátí human-readable reprezentaci předaného data a času pro zobrazení. + */ +export function getHumanDateTime(datetime: Date) { + let currentDay = String(datetime.getDate()).padStart(2, '0'); + let currentMonth = String(datetime.getMonth() + 1).padStart(2, "0"); + let currentYear = datetime.getFullYear(); + let currentHours = String(datetime.getHours()).padStart(2, '0'); + let currentMinutes = String(datetime.getMinutes()).padStart(2, "0"); + return `${currentDay}.${currentMonth}.${currentYear} ${currentHours}:${currentMinutes}`; +} diff --git a/server/src/service.ts b/server/src/service.ts index c33dace..7da9dbe 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -99,9 +99,10 @@ async function getMenu(date: Date): Promise { export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise { const usedDate = date ?? getToday(); const dayOfWeekIndex = getDayOfWeekIndex(usedDate); + const now = new Date().getTime(); if (getIsWeekend(usedDate)) { return { - lastUpdate: getHumanTime(new Date()), + lastUpdate: now, closed: true, food: [], }; @@ -117,7 +118,7 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P } if (menus[i][restaurant] == null) { menus[i][restaurant] = { - lastUpdate: getHumanTime(new Date()), + lastUpdate: now, closed: false, food: [], }; diff --git a/types/Types.ts b/types/Types.ts index d3c3eb1..4cc717d 100644 --- a/types/Types.ts +++ b/types/Types.ts @@ -93,7 +93,7 @@ export interface ClientData extends DayData { /** Nabídka jídel jednoho podniku pro jeden konkrétní den. */ export interface DayMenu { - lastUpdate: string, // human-readable čas poslední aktualizace menu + lastUpdate: number, // UNIX timestamp poslední aktualizace menu closed: boolean, // příznak, zda je daný podnik v tento den zavřený food: Food[], // seznam jídel v menu }