Add extras and options CRUD, add sidebar, add kanban board, udpate packages
This commit is contained in:
122
src/components/commissions/extras/ExtraDialog.tsx
Normal file
122
src/components/commissions/extras/ExtraDialog.tsx
Normal file
@ -0,0 +1,122 @@
|
||||
"use client";
|
||||
|
||||
import { createCommissionExtra, updateCommissionExtra } from "@/actions/commissions/types/extras";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { type CommissionExtraValues, commissionExtraSchema } from "@/schemas/commissionType";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
|
||||
type Initial = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
trigger: React.ReactNode;
|
||||
mode: "create" | "edit";
|
||||
initial?: Initial;
|
||||
};
|
||||
|
||||
export function ExtraDialog({ trigger, mode, initial }: Props) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
// ✅ key remounts the form per item / create mode, no useEffect needed
|
||||
const formKey = mode === "create" ? "new" : initial?.id ?? "missing";
|
||||
|
||||
const form = useForm<CommissionExtraValues>({
|
||||
resolver: zodResolver(commissionExtraSchema),
|
||||
defaultValues: {
|
||||
name: initial?.name ?? "",
|
||||
description: initial?.description ?? ""
|
||||
},
|
||||
});
|
||||
|
||||
async function onSubmit(values: CommissionExtraValues) {
|
||||
try {
|
||||
if (mode === "create") {
|
||||
await createCommissionExtra(values);
|
||||
toast.success("Extra created.");
|
||||
} else {
|
||||
if (!initial?.id) throw new Error("Missing extra id");
|
||||
await updateCommissionExtra(initial.id, values);
|
||||
toast.success("Extra updated.");
|
||||
}
|
||||
setOpen(false);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
toast.error("Failed to save extra.");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>{trigger}</DialogTrigger>
|
||||
|
||||
<DialogContent className="sm:max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{mode === "create" ? "New extra" : "Edit extra"}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div key={formKey}>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-5">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl><Input {...field} /></FormControl>
|
||||
<FormDescription>Shown to customers.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Description</FormLabel>
|
||||
<FormControl><Input {...field} /></FormControl>
|
||||
<FormDescription>Optional helper text.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit">Save</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
101
src/components/commissions/extras/ExtraListClient.tsx
Normal file
101
src/components/commissions/extras/ExtraListClient.tsx
Normal file
@ -0,0 +1,101 @@
|
||||
"use client";
|
||||
|
||||
import { deleteCommissionExtra } from "@/actions/commissions/types/extras";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Pencil, Plus, Trash2 } from "lucide-react";
|
||||
import * as React from "react";
|
||||
import { toast } from "sonner";
|
||||
import { ExtraDialog } from "./ExtraDialog";
|
||||
|
||||
type Item = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
};
|
||||
|
||||
export function ExtraListClient({ extras }: { extras: Item[] }) {
|
||||
const [busyId, setBusyId] = React.useState<string | null>(null);
|
||||
|
||||
async function onDelete(id: string) {
|
||||
try {
|
||||
setBusyId(id);
|
||||
await deleteCommissionExtra(id);
|
||||
toast.success("Extra deleted.");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
toast.error("Failed to delete extra.");
|
||||
} finally {
|
||||
setBusyId(null);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<h1 className="text-2xl font-bold">Extras</h1>
|
||||
|
||||
<ExtraDialog
|
||||
mode="create"
|
||||
trigger={
|
||||
<Button className="gap-2">
|
||||
<Plus className="h-4 w-4" />
|
||||
New extra
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">All extras</CardTitle>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="p-0">
|
||||
{extras.length === 0 ? (
|
||||
<div className="p-6 text-sm text-muted-foreground italic">No extras yet.</div>
|
||||
) : (
|
||||
<ul className="divide-y">
|
||||
{extras.map((o) => (
|
||||
<li key={o.id} className="flex items-center justify-between gap-4 px-6 py-4">
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="font-medium truncate">{o.name}</div>
|
||||
</div>
|
||||
{o.description ? (
|
||||
<div className="text-sm text-muted-foreground truncate">{o.description}</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<ExtraDialog
|
||||
mode="edit"
|
||||
initial={o}
|
||||
trigger={
|
||||
<Button variant="secondary" size="sm" className="gap-2">
|
||||
<Pencil className="h-4 w-4" />
|
||||
Edit
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
disabled={busyId === o.id}
|
||||
onClick={() => onDelete(o.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
Delete
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user