Podpora víkendů

This commit is contained in:
2023-06-02 21:12:38 +02:00
parent d59e439588
commit ec1cbf332b
5 changed files with 58 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
import { ClientData, Locations } from "./types";
import { db } from "./database";
import { getTodayString } from "./utils";
import { getHumanDate, getIsWeekend } from "./utils";
import { getDate } from "./utils";
// /** Jedna konkrétní pizza */
@@ -33,7 +33,7 @@ import { getDate } from "./utils";
/** Vrátí "prázdná" (implicitní) data, pokud ještě nikdo nehlasoval. */
function getEmptyData(): ClientData {
return { date: getTodayString(), choices: {} };
return { date: getHumanDate(new Date()), isWeekend: getIsWeekend(new Date()), choices: {} };
}
/**

View File

@@ -4,6 +4,7 @@ export interface Choices {
export interface ClientData {
date: string, // dnešní datum pro zobrazení
isWeekend: boolean, // příznak, zda je dnes víkend
choices: Choices, // seznam voleb
}

View File

@@ -6,12 +6,17 @@ export function getDate() {
return `${currentYear}-${currentMonth}-${currentDay}`;
}
/** Vrátí human-readable reprezentaci dnešního data pro zobrazení. */
export function getTodayString() {
const date = new Date();
/** Vrátí human-readable reprezentaci předaného data pro zobrazení. */
export function getHumanDate(date: Date) {
let currentDay = String(date.getDate()).padStart(2, '0');
let currentMonth = String(date.getMonth() + 1).padStart(2, "0");
let currentYear = date.getFullYear();
let currentDayOfWeek = date.toLocaleDateString("CZ-cs", { weekday: 'long' });
return `${currentDay}.${currentMonth}.${currentYear} (${currentDayOfWeek})`;
}
/** Vrátí true, pokud je předané datum o víkendu. */
export function getIsWeekend(date: Date) {
const dayName = date.toLocaleDateString("CZ-cs", { weekday: 'long' }).toLowerCase()
return dayName === 'sobota' || dayName === 'neděle'
}