Files
website-secure/components/customer-dialog.tsx
T
pm 9c14a19a87
DevSecOps Enterprise Pipeline / security-gate-and-deploy (push) Failing after 41s
chore: upload parte 1 - logica
2026-06-01 15:35:53 +01:00

203 lines
5.9 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 { toast } from "sonner";
import type { Customer } from "@/lib/types";
interface CustomerDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
customer?: Customer | null;
onSaved: () => void;
}
export function CustomerDialog({
open,
onOpenChange,
customer,
onSaved,
}: CustomerDialogProps) {
const [loading, setLoading] = useState(false);
const [form, setForm] = useState({
name: "",
code: "",
email: "",
phone: "",
address: "",
contact_person: "",
notes: "",
});
useEffect(() => {
if (customer) {
setForm({
name: customer.name || "",
code: customer.code || "",
email: customer.email || "",
phone: customer.phone || "",
address: customer.address || "",
contact_person: customer.contact_person || "",
notes: customer.notes || "",
});
} else {
setForm({
name: "",
code: "",
email: "",
phone: "",
address: "",
contact_person: "",
notes: "",
});
}
}, [customer, 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 = customer
? `/api/customers/${customer.id}`
: "/api/customers";
const res = await fetch(url, {
method: customer ? "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(customer ? "Customer updated" : "Customer created");
onSaved();
onOpenChange(false);
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to save customer");
} finally {
setLoading(false);
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>
{customer ? "Edit Customer" : "Add Customer"}
</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="name">
Company Name <span className="text-destructive">*</span>
</Label>
<Input
id="name"
value={form.name}
onChange={(e) => setForm({ ...form, name: e.target.value })}
placeholder="Acme Electronics"
required
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="code">
Customer Code <span className="text-destructive">*</span>
</Label>
<Input
id="code"
value={form.code}
onChange={(e) => setForm({ ...form, code: e.target.value.toUpperCase() })}
placeholder="ACME"
className="font-mono uppercase"
required
/>
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
value={form.email}
onChange={(e) => setForm({ ...form, email: e.target.value })}
placeholder="orders@acme.com"
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="phone">Phone</Label>
<Input
id="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="contact_person">Contact Person</Label>
<Input
id="contact_person"
value={form.contact_person}
onChange={(e) => setForm({ ...form, contact_person: e.target.value })}
placeholder="John Doe"
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="address">Address</Label>
<Textarea
id="address"
value={form.address}
onChange={(e) => setForm({ ...form, address: e.target.value })}
placeholder="123 Main St, City, State 12345"
rows={2}
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="notes">Notes</Label>
<Textarea
id="notes"
value={form.notes}
onChange={(e) => setForm({ ...form, notes: e.target.value })}
placeholder="Internal notes about this customer..."
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..." : customer ? "Update" : "Create"}
</Button>
</div>
</form>
</DialogContent>
</Dialog>
);
}