diff --git a/client/src/App.tsx b/client/src/App.tsx index 5cb3454..6725bc5 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -15,7 +15,7 @@ import { useSettings } from './context/settings'; import Footer from './components/Footer'; import { faChainBroken, faChevronLeft, faChevronRight, faGear, faSatelliteDish, faSearch } from '@fortawesome/free-solid-svg-icons'; import Loader from './components/Loader'; -import { getHumanDateTime, isInTheFuture } from './Utils'; +import { getDayOfWeekIndex, getHumanDate, getHumanDateTime, getIsWeekend, isInTheFuture } from './Utils'; import NoteModal from './components/modals/NoteModal'; import { useEasterEgg } from './context/eggs'; import { ClientData, Food, PizzaOrder, DepartureTime, PizzaDayState, Restaurant, RestaurantDayMenu, RestaurantDayMenuMap, LunchChoice, UserLunchChoice, PizzaVariant, getData, getEasterEggImage, addPizza, removePizza, updatePizzaDayNote, createPizzaDay, deletePizzaDay, lockPizzaDay, unlockPizzaDay, finishOrder, finishDelivery, addChoice, jdemeObed, removeChoices, removeChoice, updateNote, changeDepartureTime } from '../../types'; @@ -71,7 +71,10 @@ function App() { const departureChoiceRef = useRef(null); const pizzaPoznamkaRef = useRef(null); const [failure, setFailure] = useState(false); - const [dayIndex, setDayIndex] = useState(); + const [dayIndex, setDayIndex] = useState(); // Index zobrazovaného dne + // TODO berka zde je nutné dořešit mocking pro testování + const [todayDayIndex, setTodayDayIndex] = useState(getDayOfWeekIndex(new Date())); // Index dnešního dne + const [isTodayWeekend, setIsTodayWeekend] = useState(getIsWeekend(new Date())); const [loadingPizzaDay, setLoadingPizzaDay] = useState(false); const [noteModalOpen, setNoteModalOpen] = useState(false); const [eggImage, setEggImage] = useState(); @@ -89,8 +92,9 @@ function App() { const data = response.data if (data) { setData(data); - setDayIndex(data.dayIndex); - dayIndexRef.current = data.dayIndex; + const dayIndex = getDayOfWeekIndex(new Date(data.date)); + setDayIndex(dayIndex); + dayIndexRef.current = dayIndex; setFood(data.menus); } }).catch(e => { @@ -103,6 +107,8 @@ function App() { if (!auth?.login) { return } + setTodayDayIndex(getDayOfWeekIndex(new Date())); + setIsTodayWeekend(getIsWeekend(new Date())); getData({ query: { dayIndex: dayIndex } }).then(response => { const data = response.data; setData(data); @@ -125,7 +131,7 @@ function App() { socket.on(EVENT_MESSAGE, (newData: ClientData) => { // console.log("Přijata nová data ze socketu", newData); // Aktualizujeme pouze, pokud jsme dostali data pro den, který máme aktuálně zobrazený - if (dayIndexRef.current == null || newData.dayIndex === dayIndexRef.current) { + if (dayIndexRef.current == null || getDayOfWeekIndex(new Date(newData.date)) === dayIndexRef.current) { setData(newData); } }); @@ -439,7 +445,7 @@ function App() { } const noOrders = data?.pizzaDay?.orders?.length === 0; - const canChangeChoice = dayIndex == null || data.todayDayIndex == null || dayIndex >= data.todayDayIndex; + const canChangeChoice = dayIndex == null || dayIndex >= todayDayIndex; const { path, url, startOffset, endOffset, duration, ...style } = easterEgg || {}; @@ -448,7 +454,7 @@ function App() { {easterEgg && eggImage && }
- {data.isWeekend ?

Užívejte víkend :)

: <> + {isTodayWeekend ?

Užívejte víkend :)

: <> {/* */} @@ -464,7 +470,7 @@ function App() { 0 ? "initial" : "hidden" }} onClick={() => handleDayChange(dayIndex - 1)} /> -

{data.date}

+

{getHumanDate(new Date(data.date))}

handleDayChange(dayIndex + 1)} /> @@ -480,7 +486,7 @@ function App() {
{canChangeChoice && <> -

{`Jak to ${dayIndex == null || dayIndex === data.todayDayIndex ? 'dnes' : 'tento den'} vidíš s obědem?`}

+

{`Jak to ${dayIndex == null || dayIndex === todayDayIndex ? 'dnes' : 'tento den'} vidíš s obědem?`}

{Object.entries(LunchChoice) @@ -589,7 +595,7 @@ function App() { :
Zatím nikdo nehlasoval...
}
- {dayIndex === data.todayDayIndex && + {dayIndex === todayDayIndex &&
{!data.pizzaDay &&
diff --git a/client/src/Utils.tsx b/client/src/Utils.tsx index 4d081dc..f35dfb6 100644 --- a/client/src/Utils.tsx +++ b/client/src/Utils.tsx @@ -73,6 +73,12 @@ export const getDayOfWeekIndex = (date: Date) => { return (((date.getDay() - 1) % 7) + 7) % 7; } +/** Vrátí true, pokud je předané datum o víkendu. */ +export function getIsWeekend(date: Date) { + const index = getDayOfWeekIndex(date); + return index == 5 || index == 6; +} + /** Vrátí první pracovní den v týdnu předaného data. */ export function getFirstWorkDayOfWeek(date: Date) { const firstDay = new Date(date.getTime()); diff --git a/server/src/service.ts b/server/src/service.ts index f084932..99cedb8 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -1,4 +1,4 @@ -import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils"; +import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getIsWeekend, getWeekNumber } from "./utils"; import getStorage from "./storage"; import { getMenuSladovnicka, getMenuTechTower, getMenuZastavkaUmichala, getMenuSenkSerikova } from "./restaurants"; import { getTodayMock } from "./mock"; @@ -31,10 +31,7 @@ export const getDateForWeekIndex = (index: number) => { function getEmptyData(date?: Date): ClientData { const usedDate = date || getToday(); return { - todayDayIndex: getDayOfWeekIndex(getToday()), - date: getHumanDate(usedDate), - isWeekend: getIsWeekend(usedDate), - dayIndex: getDayOfWeekIndex(usedDate), + date: usedDate.toISOString().split('T')[0], choices: {}, }; } @@ -486,9 +483,5 @@ export async function updateDepartureTime(login: string, time?: string, date?: D export async function getClientData(date?: Date): Promise { const targetDate = date ?? getToday(); const dateString = formatDate(targetDate); - const clientData = await storage.getData(dateString) || getEmptyData(date); - return { - ...clientData, - todayDayIndex: getDayOfWeekIndex(getToday()), - } + return await storage.getData(dateString) || getEmptyData(date); } \ No newline at end of file diff --git a/types/schemas/_index.yml b/types/schemas/_index.yml index b4be218..d74f723 100644 --- a/types/schemas/_index.yml +++ b/types/schemas/_index.yml @@ -21,23 +21,13 @@ ClientData: type: object additionalProperties: false required: - - todayDayIndex - date - - isWeekend - choices properties: - todayDayIndex: - description: Index dnešního dne v týdnu - $ref: "#/DayIndex" date: - description: Human-readable datum dne + description: Datum konkrétního dne type: string - isWeekend: - description: Příznak, zda je tento den víkend - type: boolean - dayIndex: - description: Index dne v týdnu, ke kterému se vztahují tato data - $ref: "#/DayIndex" + format: date choices: $ref: "#/LunchChoices" menus: