filters , order inspections , inventory items , credit notes
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
"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,
|
||||
RhfSelectField,
|
||||
RhfAsyncSelectField,
|
||||
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 { toRelation, toId } from "@/shared/lib/utils"
|
||||
|
||||
import { expenseItemFormSchema, type ExpenseItemFormValues } from "./expense-item.schema"
|
||||
import { EXPENSE_ITEM_ROUTES, INVENTORY_CATEGORY_ROUTES } from "@garage/api"
|
||||
import { InventoryCategoryCrudDialog } from "./inventory-category-crud-dialog"
|
||||
|
||||
// ── Constants ──
|
||||
|
||||
const ITEM_TYPE_OPTIONS = [
|
||||
{ value: "Expense", label: "Expense" },
|
||||
]
|
||||
|
||||
const mapLookupOption = (item: any) => ({
|
||||
value: String(item.id),
|
||||
label: item.title ?? item.name ?? String(item.id),
|
||||
})
|
||||
|
||||
const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label }
|
||||
|
||||
// ── Props ──
|
||||
|
||||
export type ExpenseItemFormProps = {
|
||||
resourceId?: string | null
|
||||
initialData?: unknown
|
||||
onSuccess?: () => void
|
||||
}
|
||||
|
||||
// ── Default values ──
|
||||
|
||||
const DEFAULT_VALUES: ExpenseItemFormValues = {
|
||||
item_type: "Expense",
|
||||
item_name: "",
|
||||
category: null,
|
||||
purchase_price: undefined,
|
||||
purchase_chart_of_account: "",
|
||||
purchase_information: true,
|
||||
is_active: true,
|
||||
}
|
||||
|
||||
// ── Mapping helpers ──
|
||||
|
||||
function mapToFormValues(data: unknown): ExpenseItemFormValues {
|
||||
const d = (data as any)?.data ?? data ?? {}
|
||||
|
||||
return {
|
||||
item_type: d.item_type || "Expense",
|
||||
item_name: d.item_name || "",
|
||||
category: toRelation(d.category_id, d.category_title ?? d.category_name),
|
||||
purchase_price: d.purchase_price ?? undefined,
|
||||
purchase_chart_of_account: d.purchase_chart_of_account || "",
|
||||
purchase_information: d.purchase_information ?? true,
|
||||
is_active: d.is_active ?? true,
|
||||
}
|
||||
}
|
||||
|
||||
function mapFormToPayload(values: ExpenseItemFormValues) {
|
||||
return {
|
||||
item_type: values.item_type,
|
||||
item_name: values.item_name,
|
||||
category_id: toId(values.category),
|
||||
purchase_price: values.purchase_price,
|
||||
purchase_chart_of_account: values.purchase_chart_of_account || undefined,
|
||||
purchase_information: values.purchase_information,
|
||||
is_active: values.is_active,
|
||||
}
|
||||
}
|
||||
|
||||
// ── Component ──
|
||||
|
||||
export function ExpenseItemForm({ resourceId, initialData, onSuccess }: ExpenseItemFormProps) {
|
||||
const api = useAuthApi()
|
||||
|
||||
const { form, isEditing } = useResourceForm<ExpenseItemFormValues, any>({
|
||||
schema: expenseItemFormSchema,
|
||||
defaultValues: DEFAULT_VALUES,
|
||||
resourceId,
|
||||
initialData,
|
||||
initialize: (id) => api.expenseItems.show(id),
|
||||
queryKey: [EXPENSE_ITEM_ROUTES.BY_ID, resourceId],
|
||||
mapToFormValues,
|
||||
})
|
||||
|
||||
const { mutate, error, isPending } = useFormMutation(form, {
|
||||
mutationFn: (values: ExpenseItemFormValues) => {
|
||||
const promise = isEditing && resourceId
|
||||
? api.expenseItems.update(resourceId, mapFormToPayload(values))
|
||||
: api.expenseItems.create(mapFormToPayload(values))
|
||||
toast.promise(promise, {
|
||||
loading: isEditing ? "Updating expense item..." : "Creating expense item...",
|
||||
success: isEditing ? "Expense item updated successfully" : "Expense item created successfully",
|
||||
error: isEditing ? "Failed to update expense item" : "Failed to create expense item",
|
||||
})
|
||||
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 expense item" : "Failed to create expense item"}
|
||||
</AlertTitle>
|
||||
{error.message}
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<FieldGroup>
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<RhfSelectField
|
||||
name="item_type"
|
||||
label="Item Type"
|
||||
options={ITEM_TYPE_OPTIONS}
|
||||
required
|
||||
/>
|
||||
<RhfTextField
|
||||
name="item_name"
|
||||
label="Item Name"
|
||||
placeholder="e.g. Office Supplies"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div className="mb-1 flex items-center justify-between">
|
||||
<span className="text-sm font-medium">Category</span>
|
||||
<InventoryCategoryCrudDialog />
|
||||
</div>
|
||||
<RhfAsyncSelectField
|
||||
name="category"
|
||||
label=""
|
||||
placeholder="Select category"
|
||||
queryKey={[INVENTORY_CATEGORY_ROUTES.INDEX]}
|
||||
listFn={() => api.inventoryCategories.list()}
|
||||
mapOption={mapLookupOption}
|
||||
{...STORE_OBJECT}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<RhfTextField
|
||||
name="purchase_price"
|
||||
label="Purchase Price"
|
||||
placeholder="0.00"
|
||||
type="number"
|
||||
/>
|
||||
<RhfTextField
|
||||
name="purchase_chart_of_account"
|
||||
label="Purchase Chart of Account"
|
||||
placeholder="e.g. Expenses"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-3">
|
||||
<RhfCheckboxField
|
||||
name="purchase_information"
|
||||
label="Purchase Information"
|
||||
/>
|
||||
<RhfCheckboxField
|
||||
name="is_active"
|
||||
label="Active"
|
||||
/>
|
||||
</div>
|
||||
</FieldGroup>
|
||||
|
||||
<div className="mt-6 flex justify-end">
|
||||
<Button type="submit" disabled={isPending}>
|
||||
{isEditing ? (
|
||||
<>
|
||||
<Save className="me-2 h-4 w-4" />
|
||||
{isPending ? "Saving..." : "Save Changes"}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Plus className="me-2 h-4 w-4" />
|
||||
{isPending ? "Creating..." : "Create Expense Item"}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</Rhform>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const relationFieldSchema = z
|
||||
.object({ value: z.string(), label: z.string() })
|
||||
.nullable()
|
||||
|
||||
export const expenseItemFormSchema = z.object({
|
||||
item_type: z.string().min(1, "Item type is required"),
|
||||
item_name: z.string().min(1, "Item name is required"),
|
||||
category: relationFieldSchema,
|
||||
purchase_price: z.coerce.number().min(0).optional(),
|
||||
purchase_chart_of_account: z.string().optional(),
|
||||
purchase_information: z.boolean().default(true),
|
||||
is_active: z.boolean().default(true),
|
||||
})
|
||||
|
||||
export type ExpenseItemFormValues = z.infer<typeof expenseItemFormSchema>
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client"
|
||||
|
||||
import { CrudDialog } from "@/shared/components/crud-dialog"
|
||||
import { ColumnHeader } from "@/shared/data-view/table-view"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
import { INVENTORY_CATEGORY_ROUTES } from "@garage/api"
|
||||
import { InventoryCategoryForm } from "./inventory-category-form"
|
||||
|
||||
export function InventoryCategoryCrudDialog() {
|
||||
const api = useAuthApi()
|
||||
|
||||
return (
|
||||
<CrudDialog
|
||||
title="Inventory Category"
|
||||
queryKey={[INVENTORY_CATEGORY_ROUTES.INDEX]}
|
||||
getClient={() => api.inventoryCategories}
|
||||
resourceLabel="inventory category"
|
||||
columns={() => [
|
||||
{
|
||||
accessorKey: "title",
|
||||
header: ({ column }) => <ColumnHeader column={column} title="Title" />,
|
||||
},
|
||||
]}
|
||||
renderForm={({ resourceId, initialData, onSuccess }) => (
|
||||
<InventoryCategoryForm
|
||||
resourceId={resourceId}
|
||||
initialData={initialData}
|
||||
onSuccess={onSuccess}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
"use client"
|
||||
|
||||
import { z } from "zod"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { Plus, Save } from "lucide-react"
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
import { FieldGroup } from "@/shared/components/ui/field"
|
||||
import { Rhform, RhfTextField } from "@/shared/components/form"
|
||||
import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
import { useEffect } from "react"
|
||||
|
||||
const inventoryCategorySchema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
})
|
||||
|
||||
type InventoryCategoryFormValues = z.infer<typeof inventoryCategorySchema>
|
||||
|
||||
type InventoryCategoryFormProps = {
|
||||
resourceId?: string | null
|
||||
initialData?: any
|
||||
onSuccess?: () => void
|
||||
}
|
||||
|
||||
export function InventoryCategoryForm({ resourceId, initialData, onSuccess }: InventoryCategoryFormProps) {
|
||||
const api = useAuthApi()
|
||||
const isEditing = !!resourceId
|
||||
|
||||
const form = useForm<InventoryCategoryFormValues>({
|
||||
resolver: zodResolver(inventoryCategorySchema),
|
||||
defaultValues: { title: "" },
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
const d = initialData?.data ?? initialData
|
||||
form.reset({ title: d.title ?? "" })
|
||||
}
|
||||
}, [initialData, form])
|
||||
|
||||
const handleSubmit = async (values: InventoryCategoryFormValues) => {
|
||||
try {
|
||||
const promise = isEditing
|
||||
? api.inventoryCategories.update(resourceId!, { title: values.title })
|
||||
: api.inventoryCategories.create({ title: values.title })
|
||||
|
||||
toast.promise(promise, {
|
||||
loading: isEditing ? "Updating..." : "Creating...",
|
||||
success: isEditing ? "Updated successfully" : "Created successfully",
|
||||
error: isEditing ? "Failed to update" : "Failed to create",
|
||||
})
|
||||
|
||||
await promise
|
||||
form.reset()
|
||||
onSuccess?.()
|
||||
} catch {
|
||||
// toast already shown
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Rhform form={form} onSubmit={handleSubmit}>
|
||||
<FieldGroup>
|
||||
<RhfTextField
|
||||
name="title"
|
||||
label="Title"
|
||||
placeholder="e.g. Engine Parts"
|
||||
required
|
||||
/>
|
||||
<Button type="submit" disabled={form.formState.isSubmitting}>
|
||||
{isEditing ? <Save className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
|
||||
{form.formState.isSubmitting
|
||||
? (isEditing ? "Updating..." : "Creating...")
|
||||
: (isEditing ? "Update" : "Create")}
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</Rhform>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user