diff --git a/src/components/exams/exam-form.tsx b/src/components/exams/exam-form.tsx
index f4ec6f3..af775d0 100644
--- a/src/components/exams/exam-form.tsx
+++ b/src/components/exams/exam-form.tsx
@@ -5,33 +5,53 @@ import type { Exam } from "@/api/types";
import { EXAM_KINDS, EXAM_MODES } from "@/api/types";
import { Button } from "@/components/ui/button";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
+import { DatePicker } from "@/components/ui/date-picker";
import { IconSave } from "@/components/ui/icons";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select } from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { examKindLabel } from "@/lib/exam-labels";
-import { dateTimeTextToMs, msToDateTimeText } from "@/lib/format";
import { useT } from "@/stores/preferences-context";
const UI_EXAM_KINDS = EXAM_KINDS.filter((kind) => kind !== "homework");
-function datePart(value: string): string {
- return value.split(" ")[0] ?? "";
+function dateInputFromMs(ms: number | null | undefined): string {
+ if (ms == null) return "";
+ const d = new Date(ms);
+ const pad = (n: number) => String(n).padStart(2, "0");
+ return `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${d.getFullYear()}`;
}
-function timePart(value: string): string {
- return value.split(" ")[1] ?? "";
+function timeInputFromMs(ms: number | null | undefined): string {
+ if (ms == null) return "";
+ const d = new Date(ms);
+ const pad = (n: number) => String(n).padStart(2, "0");
+ return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
-function withDate(value: string, date: string): string {
- const time = timePart(value);
- return time ? `${date} ${time}` : date;
-}
-
-function withTime(value: string, time: string): string {
- const date = datePart(value);
- return date ? `${date} ${time}` : time;
+function scheduleInputToMs(date: string, time: string): number | null {
+ const dateMatch = date.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
+ const timeMatch = time.trim().match(/^(\d{2}):(\d{2})$/);
+ if (!dateMatch || !timeMatch) return null;
+ const [, dayRaw, monthRaw, yearRaw] = dateMatch;
+ const [, hourRaw, minuteRaw] = timeMatch;
+ const day = Number(dayRaw);
+ const month = Number(monthRaw);
+ const year = Number(yearRaw);
+ const hour = Number(hourRaw);
+ const minute = Number(minuteRaw);
+ const d = new Date(year, month - 1, day, hour, minute, 0, 0);
+ if (
+ d.getFullYear() !== year ||
+ d.getMonth() !== month - 1 ||
+ d.getDate() !== day ||
+ d.getHours() !== hour ||
+ d.getMinutes() !== minute
+ ) {
+ return null;
+ }
+ return d.getTime();
}
export type ExamFormValues = {
@@ -57,8 +77,10 @@ export function ExamForm(props: {
const [kind, setKind] = createSignal(String(props.initial?.kind && props.initial.kind !== "homework" ? props.initial.kind : "quiz"));
const [weight, setWeight] = createSignal(String(props.initial?.weight ?? 1));
const [mode, setMode] = createSignal(String(props.initial?.mode ?? ""));
- const [startsText, setStartsText] = createSignal(msToDateTimeText(props.initial?.starts_at));
- const [endsText, setEndsText] = createSignal(msToDateTimeText(props.initial?.ends_at));
+ const [startsDate, setStartsDate] = createSignal(dateInputFromMs(props.initial?.starts_at));
+ const [startsTime, setStartsTime] = createSignal(timeInputFromMs(props.initial?.starts_at));
+ const [endsDate, setEndsDate] = createSignal(dateInputFromMs(props.initial?.ends_at));
+ const [endsTime, setEndsTime] = createSignal(timeInputFromMs(props.initial?.ends_at));
const [error, setError] = createSignal("");
const [pending, setPending] = createSignal(false);
const [confirmOpen, setConfirmOpen] = createSignal(false);
@@ -96,8 +118,10 @@ export function ExamForm(props: {
setKind("quiz");
setWeight("1");
setMode("");
- setStartsText("");
- setEndsText("");
+ setStartsDate("");
+ setStartsTime("");
+ setEndsDate("");
+ setEndsTime("");
}
} catch (err) {
setError(formatApiError(err));
@@ -108,8 +132,8 @@ export function ExamForm(props: {
const handleSubmit = async (e: SubmitEvent) => {
e.preventDefault();
- const starts_at = mode() ? dateTimeTextToMs(startsText()) : null;
- const ends_at = mode() ? dateTimeTextToMs(endsText()) : null;
+ const starts_at = mode() ? scheduleInputToMs(startsDate(), startsTime()) : null;
+ const ends_at = mode() ? scheduleInputToMs(endsDate(), endsTime()) : null;
const duration_ms = mode() === "async" && starts_at != null && ends_at != null ? ends_at - starts_at : null;
const v = validate(starts_at, ends_at, duration_ms);
if (v) {
@@ -204,46 +228,46 @@ export function ExamForm(props: {
diff --git a/src/components/ui/date-picker.tsx b/src/components/ui/date-picker.tsx
new file mode 100644
index 0000000..05c2515
--- /dev/null
+++ b/src/components/ui/date-picker.tsx
@@ -0,0 +1,142 @@
+import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
+import { Button } from "@/components/ui/button";
+import { IconCalendar, IconChevronLeft, IconChevronRight } from "@/components/ui/icons";
+import { Input } from "@/components/ui/input";
+import { cn } from "@/lib/cn";
+
+function pad(value: number): string {
+ return String(value).padStart(2, "0");
+}
+
+function formatDate(date: Date): string {
+ return `${pad(date.getDate())}/${pad(date.getMonth() + 1)}/${date.getFullYear()}`;
+}
+
+function parseDate(value: string): Date | null {
+ const match = value.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
+ if (!match) return null;
+ const [, dayRaw, monthRaw, yearRaw] = match;
+ const day = Number(dayRaw);
+ const month = Number(monthRaw);
+ const year = Number(yearRaw);
+ const date = new Date(year, month - 1, day);
+ if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) return null;
+ return date;
+}
+
+function sameDay(a: Date | null, b: Date): boolean {
+ return !!a && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
+}
+
+function monthDays(month: Date): Array {
+ const year = month.getFullYear();
+ const monthIndex = month.getMonth();
+ const first = new Date(year, monthIndex, 1);
+ const days = new Date(year, monthIndex + 1, 0).getDate();
+ const blanks = (first.getDay() + 6) % 7;
+ const cells: Array = Array.from({ length: blanks }, () => null);
+ for (let day = 1; day <= days; day += 1) cells.push(new Date(year, monthIndex, day));
+ return cells;
+}
+
+export function DatePicker(props: {
+ id: string;
+ value: string;
+ placeholder: string;
+ required?: boolean;
+ class?: string;
+ onChange: (value: string) => void;
+}) {
+ let root: HTMLDivElement | undefined;
+ const [open, setOpen] = createSignal(false);
+ const selected = createMemo(() => parseDate(props.value));
+ const [month, setMonth] = createSignal(selected() ?? new Date());
+ const days = createMemo(() => monthDays(month()));
+ const monthLabel = createMemo(() =>
+ new Intl.DateTimeFormat(undefined, { month: "long", year: "numeric" }).format(month()),
+ );
+
+ createEffect(() => {
+ const date = selected();
+ if (date) setMonth(date);
+ });
+
+ createEffect(() => {
+ if (!open()) return;
+ const closeOnOutside = (event: MouseEvent) => {
+ if (root && !root.contains(event.target as Node)) setOpen(false);
+ };
+ document.addEventListener("mousedown", closeOnOutside);
+ onCleanup(() => document.removeEventListener("mousedown", closeOnOutside));
+ });
+
+ const moveMonth = (delta: number) => {
+ setMonth((current) => new Date(current.getFullYear(), current.getMonth() + delta, 1));
+ };
+
+ const pick = (date: Date) => {
+ props.onChange(formatDate(date));
+ setOpen(false);
+ };
+
+ return (
+
+
+ setOpen(true)}
+ onClick={() => setOpen(true)}
+ onInput={(e) => props.onChange(e.currentTarget.value)}
+ />
+
+
+
+
+
+
+
{monthLabel()}
+
+
+
+ Pzt
+ Sal
+ Çar
+ Per
+ Cum
+ Cmt
+ Paz
+
+
+
+ {(date) => (
+ }>
+ {(day) => (
+
+ )}
+
+ )}
+
+
+
+
+
+ );
+}
diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts
index 38d860c..192ec08 100644
--- a/src/i18n/messages.ts
+++ b/src/i18n/messages.ts
@@ -249,6 +249,7 @@ export type MessageKey =
| "form.year"
| "form.month"
| "form.day"
+ | "form.datePlaceholder"
| "form.mark"
| "form.studentId"
| "form.selectStudent"
@@ -613,6 +614,7 @@ const en: Dict = {
"form.year": "Year",
"form.month": "Month",
"form.day": "Day",
+ "form.datePlaceholder": "DD/MM/YYYY",
"form.mark": "Mark",
"form.studentId": "Student user id",
"form.selectStudent": "Select student",
@@ -976,6 +978,7 @@ const tr: Dict = {
"form.year": "Yıl",
"form.month": "Ay",
"form.day": "Gün",
+ "form.datePlaceholder": "GG/AA/YYYY",
"form.mark": "Not",
"form.studentId": "Öğrenci kullanıcı id",
"form.selectStudent": "Öğrenci seç",
diff --git a/src/lib/format.ts b/src/lib/format.ts
index 8506ee8..1080f28 100644
--- a/src/lib/format.ts
+++ b/src/lib/format.ts
@@ -23,64 +23,6 @@ export function msToLocalInput(ms: number | null | undefined): string {
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
-export function msToDateTimeText(ms: number | null | undefined): string {
- return msToLocalInput(ms).replace("T", " ");
-}
-
-export function msToLocalDate(ms: number | null | undefined): string {
- return msToLocalInput(ms).slice(0, 10);
-}
-
-export function msToLocalHour(ms: number | null | undefined): string {
- return msToLocalInput(ms).slice(11, 13);
-}
-
-export function msToLocalMinute(ms: number | null | undefined): string {
- return msToLocalInput(ms).slice(14, 16);
-}
-
-export function localPartsToMs(date: string, hour: string, minute: string): number | null {
- if (!date || !hour || !minute) return null;
- const [year, month, day] = date.split("-").map(Number);
- const h = Number(hour);
- const m = Number(minute);
- if (![year, month, day, h, m].every(Number.isInteger)) return null;
- const d = new Date(year, month - 1, day, h, m, 0, 0);
- if (
- d.getFullYear() !== year ||
- d.getMonth() !== month - 1 ||
- d.getDate() !== day ||
- d.getHours() !== h ||
- d.getMinutes() !== m
- ) {
- return null;
- }
- return d.getTime();
-}
-
-export function dateTimeTextToMs(value: string): number | null {
- const match = value.trim().match(/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2})$/);
- if (!match) return null;
- const [, yearRaw, monthRaw, dayRaw, hourRaw, minuteRaw] = match;
- const year = Number(yearRaw);
- const month = Number(monthRaw);
- const day = Number(dayRaw);
- const hour = Number(hourRaw);
- const minute = Number(minuteRaw);
- if (![year, month, day, hour, minute].every(Number.isInteger)) return null;
- const d = new Date(year, month - 1, day, hour, minute, 0, 0);
- if (
- d.getFullYear() !== year ||
- d.getMonth() !== month - 1 ||
- d.getDate() !== day ||
- d.getHours() !== hour ||
- d.getMinutes() !== minute
- ) {
- return null;
- }
- return d.getTime();
-}
-
export function formatDurationMinutes(ms: number | null | undefined, locale: Locale = "en"): string {
if (ms == null) return "—";
return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;