243 lines
7.4 KiB
TypeScript
243 lines
7.4 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { toast } from "sonner";
|
|
import type { Supplier } from "@/lib/types";
|
|
|
|
interface SupplierDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
supplier?: Supplier | null;
|
|
onSaved: () => void;
|
|
}
|
|
|
|
export function SupplierDialog({
|
|
open,
|
|
onOpenChange,
|
|
supplier,
|
|
onSaved,
|
|
}: SupplierDialogProps) {
|
|
const [loading, setLoading] = useState(false);
|
|
const [form, setForm] = useState({
|
|
name: "",
|
|
code: "",
|
|
email: "",
|
|
phone: "",
|
|
address: "",
|
|
contact_person: "",
|
|
country: "",
|
|
currency: "EUR",
|
|
notes: "",
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (supplier) {
|
|
setForm({
|
|
name: supplier.name || "",
|
|
code: supplier.code || "",
|
|
email: supplier.email || "",
|
|
phone: supplier.phone || "",
|
|
address: supplier.address || "",
|
|
contact_person: supplier.contact_person || "",
|
|
country: supplier.country || "",
|
|
currency: supplier.currency || "EUR",
|
|
notes: supplier.notes || "",
|
|
});
|
|
} else {
|
|
setForm({
|
|
name: "",
|
|
code: "",
|
|
email: "",
|
|
phone: "",
|
|
address: "",
|
|
contact_person: "",
|
|
country: "",
|
|
currency: "EUR",
|
|
notes: "",
|
|
});
|
|
}
|
|
}, [supplier, open]);
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (!form.name || !form.code) {
|
|
toast.error("Name and code are required");
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
try {
|
|
const url = supplier
|
|
? `/api/suppliers/${supplier.id}`
|
|
: "/api/suppliers";
|
|
const res = await fetch(url, {
|
|
method: supplier ? "PUT" : "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(form),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
throw new Error(err.error || "Failed to save");
|
|
}
|
|
toast.success(supplier ? "Supplier updated" : "Supplier created");
|
|
onSaved();
|
|
onOpenChange(false);
|
|
} catch (err) {
|
|
toast.error(err instanceof Error ? err.message : "Failed to save supplier");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{supplier ? "Edit Supplier" : "Add Supplier"}
|
|
</DialogTitle>
|
|
</DialogHeader>
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-4 pt-2">
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-name">
|
|
Supplier Name <span className="text-destructive">*</span>
|
|
</Label>
|
|
<Input
|
|
id="supplier-name"
|
|
value={form.name}
|
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
|
placeholder="Mouser Electronics"
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-code">
|
|
Supplier Code <span className="text-destructive">*</span>
|
|
</Label>
|
|
<Input
|
|
id="supplier-code"
|
|
value={form.code}
|
|
onChange={(e) => setForm({ ...form, code: e.target.value.toUpperCase() })}
|
|
placeholder="MOUSER"
|
|
className="font-mono uppercase"
|
|
required
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-email">Email</Label>
|
|
<Input
|
|
id="supplier-email"
|
|
type="email"
|
|
value={form.email}
|
|
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
|
placeholder="sales@mouser.com"
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-phone">Phone</Label>
|
|
<Input
|
|
id="supplier-phone"
|
|
value={form.phone}
|
|
onChange={(e) => setForm({ ...form, phone: e.target.value })}
|
|
placeholder="+1 555-0123"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-contact">Contact Person</Label>
|
|
<Input
|
|
id="supplier-contact"
|
|
value={form.contact_person}
|
|
onChange={(e) => setForm({ ...form, contact_person: e.target.value })}
|
|
placeholder="Jane Smith"
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-country">Country</Label>
|
|
<Input
|
|
id="supplier-country"
|
|
value={form.country}
|
|
onChange={(e) => setForm({ ...form, country: e.target.value })}
|
|
placeholder="Germany"
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-currency">Currency</Label>
|
|
<Select
|
|
value={form.currency}
|
|
onValueChange={(val) => setForm({ ...form, currency: val })}
|
|
>
|
|
<SelectTrigger id="supplier-currency">
|
|
<SelectValue placeholder="Select currency" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="EUR">EUR</SelectItem>
|
|
<SelectItem value="USD">USD</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-address">Address</Label>
|
|
<Textarea
|
|
id="supplier-address"
|
|
value={form.address}
|
|
onChange={(e) => setForm({ ...form, address: e.target.value })}
|
|
placeholder="1000 N Main St, Mansfield, TX 76063"
|
|
rows={2}
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="supplier-notes">Notes</Label>
|
|
<Textarea
|
|
id="supplier-notes"
|
|
value={form.notes}
|
|
onChange={(e) => setForm({ ...form, notes: e.target.value })}
|
|
placeholder="Internal notes about this supplier..."
|
|
rows={2}
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-2 pt-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => onOpenChange(false)}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={loading}>
|
|
{loading ? "Saving..." : supplier ? "Update" : "Create"}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|