This commit is contained in:
2026-04-06 02:32:47 +03:00
parent 5f3d208158
commit 24a44481a0
152 changed files with 85913 additions and 44338 deletions
@@ -0,0 +1,24 @@
import { z } from "zod"
export const salesConfigFormSchema = z.object({
sell_rates_tax_inclusive: z.string().optional(),
give_discounts: z.string().optional(),
})
export type SalesConfigFormValues = z.infer<typeof salesConfigFormSchema>
export const purchaseConfigFormSchema = z.object({
purchase_rates_tax_inclusive: z.string().optional(),
receive_discounts: z.string().optional(),
})
export type PurchaseConfigFormValues = z.infer<typeof purchaseConfigFormSchema>
export const generalPreferencesFormSchema = z.object({
sell_rates_tax_inclusive: z.string().optional(),
give_discounts: z.string().optional(),
purchase_rates_tax_inclusive: z.string().optional(),
receive_discounts: z.string().optional(),
})
export type GeneralPreferencesFormValues = z.infer<typeof generalPreferencesFormSchema>
@@ -0,0 +1,102 @@
"use client"
import { AlertTriangle, Save } from "lucide-react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { toast } from "sonner"
import { Button } from "@/shared/components/ui/button"
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
import { FieldGroup } from "@/shared/components/ui/field"
import { Rhform, RhfSelectField } from "@/shared/components/form"
import { useAuthApi } from "@/shared/useApi"
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
import { TaxInclusive, DiscountType } from "@garage/api"
import { generalPreferencesFormSchema, type GeneralPreferencesFormValues } from "./configurations.schema"
const TAX_INCLUSIVE_OPTIONS = TaxInclusive.map((v) => ({ value: v, label: v }))
const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
value: v,
label: v === "no" ? "No Discount" : v === "line_item_level" ? "Line Item Level" : "Transaction Level",
}))
const DEFAULT_VALUES: GeneralPreferencesFormValues = {
sell_rates_tax_inclusive: "",
give_discounts: "",
purchase_rates_tax_inclusive: "",
receive_discounts: "",
}
export function GeneralPreferencesForm() {
const api = useAuthApi()
const form = useForm<GeneralPreferencesFormValues>({
resolver: zodResolver(generalPreferencesFormSchema) as any,
defaultValues: DEFAULT_VALUES,
})
const { mutate, error, isPending } = useFormMutation(form, {
mutationFn: (values: GeneralPreferencesFormValues) => {
const promise = api.configurations.updateGeneralPreferences(values)
toast.promise(promise, {
loading: "Saving general preferences...",
success: "General preferences saved",
error: "Failed to save general preferences",
})
return promise
},
})
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 save general preferences</AlertTitle>
{error.message}
</Alert>
)}
<FieldGroup>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfSelectField
name="sell_rates_tax_inclusive"
label="Sell Rates Tax"
placeholder="Select tax type"
options={TAX_INCLUSIVE_OPTIONS}
/>
<RhfSelectField
name="give_discounts"
label="Give Discounts"
placeholder="Select discount type"
options={DISCOUNT_OPTIONS}
/>
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfSelectField
name="purchase_rates_tax_inclusive"
label="Purchase Rates Tax"
placeholder="Select tax type"
options={TAX_INCLUSIVE_OPTIONS}
/>
<RhfSelectField
name="receive_discounts"
label="Receive Discounts"
placeholder="Select discount type"
options={DISCOUNT_OPTIONS}
/>
</div>
<div className="flex justify-end">
<Button type="submit" disabled={isPending}>
<Save />
{isPending ? "Saving..." : "Save"}
</Button>
</div>
</FieldGroup>
</Rhform>
)
}
@@ -0,0 +1,83 @@
"use client"
import { AlertTriangle, Save } from "lucide-react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { toast } from "sonner"
import { Button } from "@/shared/components/ui/button"
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
import { FieldGroup } from "@/shared/components/ui/field"
import { Rhform, RhfSelectField } from "@/shared/components/form"
import { useAuthApi } from "@/shared/useApi"
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
import { TaxInclusive, DiscountType } from "@garage/api"
import { purchaseConfigFormSchema, type PurchaseConfigFormValues } from "./configurations.schema"
const TAX_INCLUSIVE_OPTIONS = TaxInclusive.map((v) => ({ value: v, label: v }))
const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
value: v,
label: v === "no" ? "No Discount" : v === "line_item_level" ? "Line Item Level" : "Transaction Level",
}))
const DEFAULT_VALUES: PurchaseConfigFormValues = {
purchase_rates_tax_inclusive: "",
receive_discounts: "",
}
export function PurchaseConfigForm() {
const api = useAuthApi()
const form = useForm<PurchaseConfigFormValues>({
resolver: zodResolver(purchaseConfigFormSchema) as any,
defaultValues: DEFAULT_VALUES,
})
const { mutate, error, isPending } = useFormMutation(form, {
mutationFn: (values: PurchaseConfigFormValues) => {
const promise = api.configurations.updatePurchaseTaxDiscount(values)
toast.promise(promise, {
loading: "Saving purchase configuration...",
success: "Purchase configuration saved",
error: "Failed to save purchase configuration",
})
return promise
},
})
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 save purchase configuration</AlertTitle>
{error.message}
</Alert>
)}
<FieldGroup>
<RhfSelectField
name="purchase_rates_tax_inclusive"
label="Purchase Rates Tax"
placeholder="Select tax type"
options={TAX_INCLUSIVE_OPTIONS}
/>
<RhfSelectField
name="receive_discounts"
label="Receive Discounts"
placeholder="Select discount type"
options={DISCOUNT_OPTIONS}
/>
<div className="flex justify-end">
<Button type="submit" disabled={isPending}>
<Save />
{isPending ? "Saving..." : "Save"}
</Button>
</div>
</FieldGroup>
</Rhform>
)
}
@@ -0,0 +1,83 @@
"use client"
import { AlertTriangle, Save } from "lucide-react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { toast } from "sonner"
import { Button } from "@/shared/components/ui/button"
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
import { FieldGroup } from "@/shared/components/ui/field"
import { Rhform, RhfSelectField } from "@/shared/components/form"
import { useAuthApi } from "@/shared/useApi"
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
import { TaxInclusive, DiscountType } from "@garage/api"
import { salesConfigFormSchema, type SalesConfigFormValues } from "./configurations.schema"
const TAX_INCLUSIVE_OPTIONS = TaxInclusive.map((v) => ({ value: v, label: v }))
const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
value: v,
label: v === "no" ? "No Discount" : v === "line_item_level" ? "Line Item Level" : "Transaction Level",
}))
const DEFAULT_VALUES: SalesConfigFormValues = {
sell_rates_tax_inclusive: "",
give_discounts: "",
}
export function SalesConfigForm() {
const api = useAuthApi()
const form = useForm<SalesConfigFormValues>({
resolver: zodResolver(salesConfigFormSchema) as any,
defaultValues: DEFAULT_VALUES,
})
const { mutate, error, isPending } = useFormMutation(form, {
mutationFn: (values: SalesConfigFormValues) => {
const promise = api.configurations.updateSalesTaxDiscount(values)
toast.promise(promise, {
loading: "Saving sales configuration...",
success: "Sales configuration saved",
error: "Failed to save sales configuration",
})
return promise
},
})
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 save sales configuration</AlertTitle>
{error.message}
</Alert>
)}
<FieldGroup>
<RhfSelectField
name="sell_rates_tax_inclusive"
label="Sell Rates Tax"
placeholder="Select tax type"
options={TAX_INCLUSIVE_OPTIONS}
/>
<RhfSelectField
name="give_discounts"
label="Give Discounts"
placeholder="Select discount type"
options={DISCOUNT_OPTIONS}
/>
<div className="flex justify-end">
<Button type="submit" disabled={isPending}>
<Save />
{isPending ? "Saving..." : "Save"}
</Button>
</div>
</FieldGroup>
</Rhform>
)
}