hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Exam, Homework, MealSlot, SchoolSettings } from "@/api/client"; |
| 2 | import type { PatchSettingsBody } from "@/api/settings"; |
| 3 | import type { MessageKey } from "@/i18n/messages"; |
| 4 | |
| 5 | type T = (key: MessageKey) => string; |
| 6 | |
| 7 | // Meal slots and dietary tags are school-defined keys; these are the ones a |
| 8 | // school is seeded with. A key outside the map is shown as the school typed it. |
| 9 | const MEAL_SLOT_LABELS: Record<string, MessageKey> = { |
| 10 | breakfast: "meals.slotName.breakfast", |
| 11 | lunch: "meals.slotName.lunch", |
| 12 | dinner: "meals.slotName.dinner", |
| 13 | snack: "meals.slotName.snack", |
| 14 | }; |
| 15 | |
| 16 | const DIETARY_TAG_LABELS: Record<string, MessageKey> = { |
| 17 | vegetarian: "meals.tagName.vegetarian", |
| 18 | vegan: "meals.tagName.vegan", |
| 19 | gluten_free: "meals.tagName.gluten_free", |
| 20 | lactose_free: "meals.tagName.lactose_free", |
| 21 | nut_allergy: "meals.tagName.nut_allergy", |
| 22 | }; |
| 23 | |
| 24 | function lookup(map: Record<string, MessageKey>, key: string, t: T): string { |
| 25 | return Object.hasOwn(map, key) ? t(map[key]) : key; |
| 26 | } |
| 27 | |
| 28 | export function mealSlotLabel(slot: string, t: T): string { |
| 29 | return lookup(MEAL_SLOT_LABELS, slot, t); |
| 30 | } |
| 31 | |
| 32 | export function dietaryTagLabel(tag: string, t: T): string { |
| 33 | return lookup(DIETARY_TAG_LABELS, tag, t); |
| 34 | } |
| 35 | |
| 36 | const BOOKING_STATE_LABELS: Record<string, MessageKey> = { |
| 37 | booked: "meals.bookingState.booked", |
| 38 | cancelled: "meals.bookingState.cancelled", |
| 39 | }; |
| 40 | |
| 41 | const SERVICE_STATUS_LABELS: Record<string, MessageKey> = { |
| 42 | served: "meals.served", |
| 43 | missed: "meals.missed", |
| 44 | }; |
| 45 | |
| 46 | const LEDGER_KIND_LABELS: Record<string, MessageKey> = { |
| 47 | charge: "meals.ledger.charge", |
| 48 | credit: "meals.ledger.credit", |
| 49 | reversal: "meals.ledger.reversal", |
| 50 | }; |
| 51 | |
| 52 | export function mealBookingStateLabel(status: string, t: T): string { |
| 53 | return lookup(BOOKING_STATE_LABELS, status, t); |
| 54 | } |
| 55 | |
| 56 | export function mealServiceStatusLabel(status: string, t: T): string { |
| 57 | return lookup(SERVICE_STATUS_LABELS, status, t); |
| 58 | } |
| 59 | |
| 60 | export function mealLedgerKindLabel(kind: string, t: T): string { |
| 61 | return lookup(LEDGER_KIND_LABELS, kind, t); |
| 62 | } |
| 63 | |
| 64 | export function formatTry(minor: number, locale: string): string { |
| 65 | return new Intl.NumberFormat(locale, { style: "currency", currency: "TRY" }).format(minor / 100); |
| 66 | } |
| 67 | |
| 68 | export function minuteToUtcTime(minute: number | null): string { |
| 69 | if (minute == null) return ""; |
| 70 | return `${String(Math.floor(minute / 60)).padStart(2, "0")}:${String(minute % 60).padStart(2, "0")}`; |
| 71 | } |
| 72 | |
| 73 | export function utcTimeToMinute(value: string): number | null { |
| 74 | const match = /^([01]\d|2[0-3]):([0-5]\d)$/.exec(value); |
| 75 | return match ? Number(match[1]) * 60 + Number(match[2]) : null; |
| 76 | } |
| 77 | |
| 78 | const DAY_MS = 86_400_000; |
| 79 | const isoToUtcMs = (iso: string) => Date.parse(`${iso}T00:00:00Z`); |
| 80 | const utcMsToIso = (ms: number) => new Date(ms).toISOString().slice(0, 10); |
| 81 | |
| 82 | /** The viewer's own calendar day as yyyy-mm-dd (menus are plain calendar dates). */ |
| 83 | export function localIsoDate(now: Date = new Date()): string { |
| 84 | const pad = (value: number) => String(value).padStart(2, "0"); |
| 85 | return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`; |
| 86 | } |
| 87 | |
| 88 | export function addDaysIso(iso: string, days: number): string { |
| 89 | return utcMsToIso(isoToUtcMs(iso) + days * DAY_MS); |
| 90 | } |
| 91 | |
| 92 | /** Monday of the week holding `iso`. */ |
| 93 | export function weekStartIso(iso: string): string { |
| 94 | const weekday = new Date(isoToUtcMs(iso)).getUTCDay(); |
| 95 | return addDaysIso(iso, -((weekday + 6) % 7)); |
| 96 | } |
| 97 | |
| 98 | /** Menus grouped per date (ascending), each day's slots in the school's slot order. */ |
| 99 | export function groupMenusByDay<M extends { date: string; slot: string }>(menus: M[], slotOrder: string[]): { date: string; menus: M[] }[] { |
| 100 | const rank = (slot: string) => { |
| 101 | const index = slotOrder.indexOf(slot); |
| 102 | return index === -1 ? slotOrder.length : index; |
| 103 | }; |
| 104 | const days = new Map<string, M[]>(); |
| 105 | for (const menu of menus) days.set(menu.date, [...(days.get(menu.date) ?? []), menu]); |
| 106 | return [...days.entries()] |
| 107 | .sort(([a], [b]) => a.localeCompare(b)) |
| 108 | .map(([date, items]) => ({ date, menus: [...items].sort((a, b) => rank(a.slot) - rank(b.slot) || a.slot.localeCompare(b.slot)) })); |
| 109 | } |
| 110 | |
| 111 | /** "Çarşamba, 21 Ekim" — a menu's calendar date, never shifted by the viewer's zone. */ |
| 112 | export function formatMealDay(iso: string, locale: string, withYear = false): string { |
| 113 | return new Intl.DateTimeFormat(locale, { weekday: "long", day: "numeric", month: "long", ...(withYear ? { year: "numeric" } : {}), timeZone: "UTC" }) |
| 114 | .format(new Date(isoToUtcMs(iso))); |
| 115 | } |
| 116 | |
| 117 | /** "22 – 28 Eylül 2026" for the Monday-to-Sunday week starting at `startIso`. */ |
| 118 | export function formatMealWeek(startIso: string, locale: string): string { |
| 119 | return new Intl.DateTimeFormat(locale, { day: "numeric", month: "long", year: "numeric", timeZone: "UTC" }) |
| 120 | .formatRange(new Date(isoToUtcMs(startIso)), new Date(isoToUtcMs(addDaysIso(startIso, 6)))); |
| 121 | } |
| 122 | |
| 123 | export function mealCutoffAt(date: string, slot: MealSlot | undefined, minutes: number | null): number | null { |
| 124 | if (minutes == null) return null; |
| 125 | const servingMinute = slot?.serving_minute ?? 0; |
| 126 | return Date.parse(`${date}T00:00:00Z`) + servingMinute * 60_000 - minutes * 60_000; |
| 127 | } |
| 128 | |
| 129 | export function nextCourseWork(instanceId: string, exams: Exam[], homework: Homework[]) { |
| 130 | return [ |
| 131 | ...exams.filter((item) => item.class_course === instanceId).map((item) => ({ kind: "exam" as const, title: item.title, at: item.starts_at ?? item.ends_at })), |
| 132 | ...homework.filter((item) => item.class_course === instanceId).map((item) => ({ kind: "homework" as const, title: item.title, at: item.due_at })), |
| 133 | ] |
| 134 | .filter((item): item is { kind: "exam" | "homework"; title: string; at: number } => item.at != null) |
| 135 | .sort((a, b) => a.at - b.at)[0] ?? null; |
| 136 | } |
| 137 | |
| 138 | export function dirtySettingsPatch(before: SchoolSettings, after: SchoolSettings): PatchSettingsBody { |
| 139 | const patch: PatchSettingsBody = {}; |
| 140 | for (const key of [ |
| 141 | "exam_kinds", |
| 142 | "attendance_statuses", |
| 143 | "grade_bands", |
| 144 | "max_file_bytes", |
| 145 | "chatbot_history_turns", |
| 146 | "max_chatbot_threads", |
| 147 | "max_chatbot_message_len", |
| 148 | "meal_slots", |
| 149 | "dietary_tags", |
| 150 | "meal_cancel_cutoff_minutes", |
| 151 | "excuse_kinds", |
| 152 | "branches", |
| 153 | "max_excused_absent_days", |
| 154 | "max_unexcused_absent_days", |
| 155 | "timezone", |
| 156 | ] as const) { |
| 157 | if (JSON.stringify(before[key]) !== JSON.stringify(after[key])) patch[key] = after[key] as never; |
| 158 | } |
| 159 | return patch; |
| 160 | } |