feat(shop-timings): day-of-week select and closed toggle in form

Add day_of_week dropdown and is_closed checkbox to the shop timing form.
In/out times become optional and disabled when the day is marked closed;
required otherwise via schema superRefine.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Najjar\NajjarV02 2026-06-05 14:39:55 +04:00
parent baa716b8ff
commit 16fd7dd423
2 changed files with 66 additions and 16 deletions

View File

@ -8,6 +8,7 @@ import { FieldGroup } from "@/shared/components/ui/field"
import { import {
Rhform, Rhform,
RhfTextField, RhfTextField,
RhfSelectField,
RhfCheckboxField, RhfCheckboxField,
} from "@/shared/components/form" } from "@/shared/components/form"
import { toast } from "sonner" import { toast } from "sonner"
@ -17,6 +18,7 @@ import { useFormMutation } from "@/shared/hooks/use-form-mutation"
import { import {
shopTimingFormSchema, shopTimingFormSchema,
DAY_OF_WEEK_VALUES,
type ShopTimingFormValues, type ShopTimingFormValues,
} from "./shop-timing.schema" } from "./shop-timing.schema"
import { SHOP_TIMING_ROUTES } from "@garage/api" import { SHOP_TIMING_ROUTES } from "@garage/api"
@ -29,10 +31,19 @@ export type ShopTimingFormProps = {
onSuccess?: () => void onSuccess?: () => void
} }
// ── Options ──
const DAY_OPTIONS = DAY_OF_WEEK_VALUES.map((day) => ({
value: day,
label: day.charAt(0).toUpperCase() + day.slice(1),
}))
// ── Default values ── // ── Default values ──
const DEFAULT_VALUES: ShopTimingFormValues = { const DEFAULT_VALUES: ShopTimingFormValues = {
title: "", title: "",
day_of_week: "",
is_closed: false,
in_time: "", in_time: "",
out_time: "", out_time: "",
full_day_hours: "", full_day_hours: "",
@ -51,6 +62,8 @@ function mapToFormValues(data: unknown): ShopTimingFormValues {
return { return {
title: d.title ?? "", title: d.title ?? "",
day_of_week: d.day_of_week ?? "",
is_closed: d.is_closed ?? false,
in_time: d.in_time ?? "", in_time: d.in_time ?? "",
out_time: d.out_time ?? "", out_time: d.out_time ?? "",
full_day_hours: d.full_day_hours ?? "", full_day_hours: d.full_day_hours ?? "",
@ -64,10 +77,15 @@ function mapToFormValues(data: unknown): ShopTimingFormValues {
} }
function mapFormToPayload(values: ShopTimingFormValues) { function mapFormToPayload(values: ShopTimingFormValues) {
const closed = values.is_closed
return { return {
title: values.title, title: values.title,
in_time: values.in_time, day_of_week: values.day_of_week || undefined,
out_time: values.out_time, is_closed: closed,
// Closed days carry no opening hours.
in_time: closed ? undefined : values.in_time,
out_time: closed ? undefined : values.out_time,
full_day_hours: values.full_day_hours || undefined, full_day_hours: values.full_day_hours || undefined,
half_day_hours: values.half_day_hours || undefined, half_day_hours: values.half_day_hours || undefined,
punch_in: values.punch_in || undefined, punch_in: values.punch_in || undefined,
@ -93,6 +111,8 @@ export function ShopTimingForm({ resourceId, initialData, onSuccess }: ShopTimin
mapToFormValues: mapToFormValues, mapToFormValues: mapToFormValues,
}) })
const isClosed = form.watch("is_closed")
const { mutate, error, isPending } = useFormMutation(form, { const { mutate, error, isPending } = useFormMutation(form, {
mutationFn: (values: ShopTimingFormValues) => { mutationFn: (values: ShopTimingFormValues) => {
const payload = mapFormToPayload(values) const payload = mapFormToPayload(values)
@ -128,8 +148,13 @@ export function ShopTimingForm({ resourceId, initialData, onSuccess }: ShopTimin
<RhfTextField name="title" label="Title" placeholder="Enter title" required /> <RhfTextField name="title" label="Title" placeholder="Enter title" required />
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2"> <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfTextField name="in_time" label="In Time" placeholder="HH:MM:SS" required /> <RhfSelectField name="day_of_week" label="Day" placeholder="Select day" options={DAY_OPTIONS} />
<RhfTextField name="out_time" label="Out Time" placeholder="HH:MM:SS" required /> <RhfCheckboxField name="is_closed" label="Closed (shop shut this day)" />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfTextField name="in_time" label="In Time" placeholder="HH:MM:SS" required={!isClosed} disabled={isClosed} />
<RhfTextField name="out_time" label="Out Time" placeholder="HH:MM:SS" required={!isClosed} disabled={isClosed} />
</div> </div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2"> <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">

View File

@ -6,10 +6,24 @@ const optionalTimeFormat = z
.optional() .optional()
.refine((v) => !v || timeRegex.test(v), "Time must be in HH:MM:SS format") .refine((v) => !v || timeRegex.test(v), "Time must be in HH:MM:SS format")
const shopTimingFormSchema = z.object({ export const DAY_OF_WEEK_VALUES = [
"saturday",
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
] as const
const shopTimingFormSchema = z
.object({
title: z.string().min(1, "Title is required").max(255, "Title cannot exceed 255 characters"), title: z.string().min(1, "Title is required").max(255, "Title cannot exceed 255 characters"),
in_time: z.string().min(1, "In time is required").regex(timeRegex, "In time must be in HH:MM:SS format"), day_of_week: z.string().optional(),
out_time: z.string().min(1, "Out time is required").regex(timeRegex, "Out time must be in HH:MM:SS format"), is_closed: z.boolean().default(false),
// Optional at the field level; required below only when the day is open.
in_time: optionalTimeFormat,
out_time: optionalTimeFormat,
full_day_hours: optionalTimeFormat, full_day_hours: optionalTimeFormat,
half_day_hours: optionalTimeFormat, half_day_hours: optionalTimeFormat,
punch_in: optionalTimeFormat, punch_in: optionalTimeFormat,
@ -17,7 +31,18 @@ const shopTimingFormSchema = z.object({
before_time: optionalTimeFormat, before_time: optionalTimeFormat,
after_time: optionalTimeFormat, after_time: optionalTimeFormat,
is_default: z.boolean().default(false), is_default: z.boolean().default(false),
}) })
.superRefine((values, ctx) => {
// When the shop is open, in/out times are mandatory.
if (!values.is_closed) {
if (!values.in_time) {
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["in_time"], message: "In time is required" })
}
if (!values.out_time) {
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["out_time"], message: "Out time is required" })
}
}
})
type ShopTimingFormValues = z.infer<typeof shopTimingFormSchema> type ShopTimingFormValues = z.infer<typeof shopTimingFormSchema>