updates
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"use client"
|
||||
|
||||
import { AlertTriangle, Plus } from "lucide-react"
|
||||
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
|
||||
import { FieldGroup } from "@/shared/components/ui/field"
|
||||
import {
|
||||
Rhform,
|
||||
RhfTextField,
|
||||
} from "@/shared/components/form"
|
||||
import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
import { useResourceForm } from "@/shared/hooks/use-resource-form"
|
||||
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
|
||||
|
||||
import { holidayYearFormSchema, type HolidayYearFormValues } from "./holiday-year.schema"
|
||||
|
||||
// ── Props ──
|
||||
|
||||
export type HolidayYearFormProps = {
|
||||
resourceId?: string | null
|
||||
initialData?: unknown
|
||||
onSuccess?: () => void
|
||||
}
|
||||
|
||||
// ── Default values ──
|
||||
|
||||
const DEFAULT_VALUES: HolidayYearFormValues = {
|
||||
year: new Date().getFullYear(),
|
||||
}
|
||||
|
||||
// ── Mapping helpers ──
|
||||
|
||||
function mapToFormValues(data: unknown): HolidayYearFormValues {
|
||||
const d = (data as any)?.data ?? data ?? {}
|
||||
return {
|
||||
year: d.year ?? new Date().getFullYear(),
|
||||
}
|
||||
}
|
||||
|
||||
// ── Component ──
|
||||
|
||||
export function HolidayYearForm({ resourceId, initialData, onSuccess }: HolidayYearFormProps) {
|
||||
const api = useAuthApi()
|
||||
|
||||
const { form } = useResourceForm<HolidayYearFormValues, any>({
|
||||
schema: holidayYearFormSchema,
|
||||
defaultValues: DEFAULT_VALUES,
|
||||
resourceId: null,
|
||||
initialData,
|
||||
mapToFormValues,
|
||||
})
|
||||
|
||||
const { mutate, error, isPending } = useFormMutation(form, {
|
||||
mutationFn: (values: HolidayYearFormValues) => {
|
||||
const promise = api.holidayYears.create({ year: values.year })
|
||||
toast.promise(promise, {
|
||||
loading: "Creating holiday year...",
|
||||
success: "Holiday year created successfully",
|
||||
error: "Failed to create holiday year",
|
||||
})
|
||||
return promise
|
||||
},
|
||||
onSuccess: () => {
|
||||
form.reset()
|
||||
onSuccess?.()
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<Rhform form={form} onSubmit={(values) => mutate(values)}>
|
||||
{error && (
|
||||
<Alert variant="destructive" className="mb-4">
|
||||
<AlertTriangle className="me-2 h-4 w-4" />
|
||||
<AlertTitle>Failed to create holiday year</AlertTitle>
|
||||
{error.message}
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<FieldGroup>
|
||||
<RhfTextField
|
||||
name="year"
|
||||
label="Year"
|
||||
placeholder="e.g. 2026"
|
||||
type="number"
|
||||
required
|
||||
/>
|
||||
|
||||
<Button type="submit" variant="default" disabled={isPending}>
|
||||
<Plus />
|
||||
{isPending ? "Creating..." : "Create Holiday Year"}
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</Rhform>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const holidayYearFormSchema = z.object({
|
||||
year: z.coerce.number().min(1900, "Year is required").max(2100, "Invalid year"),
|
||||
})
|
||||
|
||||
export type HolidayYearFormValues = z.infer<typeof holidayYearFormSchema>
|
||||
@@ -0,0 +1,139 @@
|
||||
"use client"
|
||||
|
||||
import { AlertTriangle, Plus, Save } from "lucide-react"
|
||||
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
|
||||
import { FieldGroup } from "@/shared/components/ui/field"
|
||||
import {
|
||||
Rhform,
|
||||
RhfTextField,
|
||||
RhfTextareaField,
|
||||
RhfCheckboxField,
|
||||
} from "@/shared/components/form"
|
||||
import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
import { useResourceForm } from "@/shared/hooks/use-resource-form"
|
||||
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
|
||||
|
||||
import { taxFormSchema, type TaxFormValues } from "./tax.schema"
|
||||
import { TAX_ROUTES } from "@garage/api"
|
||||
|
||||
// ── Props ──
|
||||
|
||||
export type TaxFormProps = {
|
||||
resourceId?: string | null
|
||||
initialData?: unknown
|
||||
onSuccess?: () => void
|
||||
}
|
||||
|
||||
// ── Default values ──
|
||||
|
||||
const DEFAULT_VALUES: TaxFormValues = {
|
||||
title: "",
|
||||
note: "",
|
||||
rate: 0,
|
||||
is_default: false,
|
||||
}
|
||||
|
||||
// ── Mapping helpers ──
|
||||
|
||||
function mapToFormValues(data: unknown): TaxFormValues {
|
||||
const d = (data as any)?.data ?? data ?? {}
|
||||
return {
|
||||
title: d.title ?? "",
|
||||
note: d.note ?? "",
|
||||
rate: d.rate ? Number(d.rate) : 0,
|
||||
is_default: d.is_default ?? false,
|
||||
}
|
||||
}
|
||||
|
||||
function mapFormToPayload(values: TaxFormValues) {
|
||||
return {
|
||||
title: values.title,
|
||||
note: values.note || undefined,
|
||||
rate: values.rate,
|
||||
is_default: values.is_default,
|
||||
}
|
||||
}
|
||||
|
||||
// ── Component ──
|
||||
|
||||
export function TaxForm({ resourceId, initialData, onSuccess }: TaxFormProps) {
|
||||
const api = useAuthApi()
|
||||
|
||||
const { form, isEditing } = useResourceForm<TaxFormValues, any>({
|
||||
schema: taxFormSchema,
|
||||
defaultValues: DEFAULT_VALUES,
|
||||
resourceId,
|
||||
initialData,
|
||||
queryKey: [TAX_ROUTES.BY_ID, resourceId],
|
||||
mapToFormValues,
|
||||
})
|
||||
|
||||
const { mutate, error, isPending } = useFormMutation(form, {
|
||||
mutationFn: (values: TaxFormValues) => {
|
||||
const payload = mapFormToPayload(values)
|
||||
const promise = isEditing && resourceId
|
||||
? api.taxes.update(resourceId, payload)
|
||||
: api.taxes.create(payload)
|
||||
toast.promise(promise, {
|
||||
loading: isEditing ? "Updating tax..." : "Creating tax...",
|
||||
success: isEditing ? "Tax updated successfully" : "Tax created successfully",
|
||||
error: isEditing ? "Failed to update tax" : "Failed to create tax",
|
||||
})
|
||||
return promise
|
||||
},
|
||||
onSuccess: () => {
|
||||
form.reset()
|
||||
onSuccess?.()
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<Rhform form={form} onSubmit={(values) => mutate(values)}>
|
||||
{error && (
|
||||
<Alert variant="destructive" className="mb-4">
|
||||
<AlertTriangle className="me-2 h-4 w-4" />
|
||||
<AlertTitle>
|
||||
{isEditing ? "Failed to update tax" : "Failed to create tax"}
|
||||
</AlertTitle>
|
||||
{error.message}
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<FieldGroup>
|
||||
<RhfTextField
|
||||
name="title"
|
||||
label="Title"
|
||||
placeholder="e.g. VAT"
|
||||
required
|
||||
/>
|
||||
<RhfTextField
|
||||
name="rate"
|
||||
label="Rate (%)"
|
||||
placeholder="e.g. 18"
|
||||
type="number"
|
||||
required
|
||||
/>
|
||||
<RhfTextareaField
|
||||
name="note"
|
||||
label="Note"
|
||||
placeholder="Optional description"
|
||||
rows={3}
|
||||
/>
|
||||
<RhfCheckboxField
|
||||
name="is_default"
|
||||
label="Set as default"
|
||||
/>
|
||||
|
||||
<Button type="submit" variant="default" disabled={isPending}>
|
||||
{isEditing ? <Save /> : <Plus />}
|
||||
{isPending
|
||||
? (isEditing ? "Updating..." : "Creating...")
|
||||
: (isEditing ? "Update Tax" : "Create Tax")}
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</Rhform>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const taxFormSchema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
note: z.string().optional(),
|
||||
rate: z.coerce.number().min(0, "Rate must be 0 or more"),
|
||||
is_default: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export type TaxFormValues = z.infer<typeof taxFormSchema>
|
||||
Reference in New Issue
Block a user