hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { postInstanceWeeklyPlanMaterialize } from "@/api/instances"; |
| 3 | import { formatApiError, type MaterializeReport } from "@/api/client"; |
| 4 | import { Alert } from "@/components/ui/alert"; |
| 5 | import { Button } from "@/components/ui/button"; |
| 6 | import { DatePicker } from "@/components/ui/date-picker"; |
| 7 | import { Label } from "@/components/ui/label"; |
| 8 | import { SidePanel } from "@/components/ui/side-panel"; |
| 9 | import { dateInputToEndOfDayMs, dateTimeInputToMs, isoDateToInput } from "@/lib/datetime-input"; |
| 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | |
| 12 | /** |
| 13 | * Turns a section's resolved weekly plan into dated lessons. Always previews |
| 14 | * first (a dry run writes nothing), then the same range is applied — the call |
| 15 | * is additive and idempotent, so a retry never doubles a lesson. |
| 16 | */ |
| 17 | export function MaterializePanel(props: { |
| 18 | instanceId: string; |
| 19 | open: boolean; |
| 20 | onOpenChange: (open: boolean) => void; |
| 21 | /** limits.weekly_plan.max_materialize_days */ |
| 22 | maxDays?: number; |
| 23 | onApplied: (report: MaterializeReport) => void | Promise<void>; |
| 24 | }) { |
| 25 | const t = useT(); |
| 26 | const [from, setFrom] = createSignal(""); |
| 27 | const [to, setTo] = createSignal(""); |
| 28 | const [preview, setPreview] = createSignal<MaterializeReport | null>(null); |
| 29 | const [error, setError] = createSignal(""); |
| 30 | const [pending, setPending] = createSignal(false); |
| 31 | |
| 32 | createEffect(() => { |
| 33 | if (!props.open) return; |
| 34 | setFrom(""); |
| 35 | setTo(""); |
| 36 | setPreview(null); |
| 37 | setError(""); |
| 38 | }); |
| 39 | |
| 40 | const range = () => { |
| 41 | const fromMs = dateTimeInputToMs(from(), "00:00"); |
| 42 | const toMs = dateInputToEndOfDayMs(to()); |
| 43 | if (fromMs === null || toMs === null || toMs < fromMs) return null; |
| 44 | return { from: fromMs, to: toMs }; |
| 45 | }; |
| 46 | |
| 47 | const run = async (apply: boolean) => { |
| 48 | const body = range(); |
| 49 | if (!body) { |
| 50 | setError(t("weeklyPlan.rangeInvalid")); |
| 51 | return; |
| 52 | } |
| 53 | const days = Math.round((body.to - body.from) / 86_400_000); |
| 54 | if (props.maxDays && days > props.maxDays) { |
| 55 | setError(t("weeklyPlan.rangeTooLong", { days: props.maxDays })); |
| 56 | return; |
| 57 | } |
| 58 | setError(""); |
| 59 | setPending(true); |
| 60 | try { |
| 61 | const report = await postInstanceWeeklyPlanMaterialize(props.instanceId, { ...body, apply }); |
| 62 | if (apply) { |
| 63 | props.onOpenChange(false); |
| 64 | await props.onApplied(report); |
| 65 | } else { |
| 66 | setPreview(report); |
| 67 | } |
| 68 | } catch (err) { |
| 69 | setError(formatApiError(err)); |
| 70 | } finally { |
| 71 | setPending(false); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | return ( |
| 76 | <SidePanel open={props.open} onOpenChange={props.onOpenChange} title={t("weeklyPlan.generate")} description={t("weeklyPlan.generateHelp")}> |
| 77 | <div class="space-y-4"> |
| 78 | <div class="grid grid-cols-2 gap-2"> |
| 79 | <div class="space-y-1.5"> |
| 80 | <Label for="materialize-from">{t("weeklyPlan.from")}</Label> |
| 81 | <DatePicker id="materialize-from" placeholder={t("form.datePlaceholder")} value={from()} onChange={(value) => { setFrom(value); setPreview(null); }} /> |
| 82 | </div> |
| 83 | <div class="space-y-1.5"> |
| 84 | <Label for="materialize-to">{t("weeklyPlan.to")}</Label> |
| 85 | <DatePicker id="materialize-to" placeholder={t("form.datePlaceholder")} value={to()} onChange={(value) => { setTo(value); setPreview(null); }} /> |
| 86 | </div> |
| 87 | </div> |
| 88 | |
| 89 | <Show when={preview()}> |
| 90 | {(report) => ( |
| 91 | <div class="space-y-3 rounded-xl border border-border-line bg-surface-tint p-3 text-sm"> |
| 92 | <p class="font-medium"> |
| 93 | {t("weeklyPlan.previewSummary", { |
| 94 | candidates: report().candidates, |
| 95 | existing: report().skipped_existing, |
| 96 | holiday: report().skipped_holiday, |
| 97 | })} |
| 98 | </p> |
| 99 | <Show when={report().candidates === 0}> |
| 100 | <p class="text-muted-foreground">{t("weeklyPlan.nothingToCreate")}</p> |
| 101 | </Show> |
| 102 | <Show when={report().blocked.length > 0}> |
| 103 | <div class="space-y-1"> |
| 104 | <p class="text-xs font-medium text-text-subtle">{t("weeklyPlan.blockedDays")}</p> |
| 105 | <ul class="flex flex-wrap gap-1.5 text-xs"> |
| 106 | <For each={report().blocked}> |
| 107 | {(day) => <li class="rounded-md border border-border-line bg-surface-base px-2 py-0.5 font-mono">{isoDateToInput(day.date)}</li>} |
| 108 | </For> |
| 109 | </ul> |
| 110 | </div> |
| 111 | </Show> |
| 112 | </div> |
| 113 | )} |
| 114 | </Show> |
| 115 | |
| 116 | <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> |
| 117 | |
| 118 | <div class="flex flex-wrap gap-2 border-t border-border-hairline pt-4"> |
| 119 | <Show |
| 120 | when={preview() && preview()!.candidates > 0} |
| 121 | fallback={<Button type="button" disabled={pending() || !from() || !to()} onClick={() => void run(false)}>{t("weeklyPlan.preview")}</Button>} |
| 122 | > |
| 123 | <Button type="button" disabled={pending()} onClick={() => void run(true)}> |
| 124 | {t("weeklyPlan.apply", { count: preview()!.candidates })} |
| 125 | </Button> |
| 126 | </Show> |
| 127 | <Button type="button" variant="outline" onClick={() => props.onOpenChange(false)}>{t("common.cancel")}</Button> |
| 128 | </div> |
| 129 | </div> |
| 130 | </SidePanel> |
| 131 | ); |
| 132 | } |