170 lines
4.8 KiB
TypeScript
170 lines
4.8 KiB
TypeScript
"use client";
|
|
|
|
import React from "react"
|
|
|
|
import { useEffect, useState } 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 type { Product } from "@/lib/types";
|
|
|
|
interface ProductDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
product: Product | null;
|
|
onSave: (data: Partial<Product>) => Promise<void>;
|
|
}
|
|
|
|
export function ProductDialog({
|
|
open,
|
|
onOpenChange,
|
|
product,
|
|
onSave,
|
|
}: ProductDialogProps) {
|
|
const [loading, setLoading] = useState(false);
|
|
const [form, setForm] = useState({
|
|
name: "",
|
|
sku: "",
|
|
description: "",
|
|
version: "1.0",
|
|
status: "draft",
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (product) {
|
|
setForm({
|
|
name: product.name || "",
|
|
sku: product.sku || "",
|
|
description: product.description || "",
|
|
version: product.version || "1.0",
|
|
status: product.status || "draft",
|
|
});
|
|
} else {
|
|
setForm({
|
|
name: "",
|
|
sku: "",
|
|
description: "",
|
|
version: "1.0",
|
|
status: "draft",
|
|
});
|
|
}
|
|
}, [product, open]);
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
try {
|
|
await onSave(form);
|
|
onOpenChange(false);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{product ? "Edit Product" : "Add New Product"}
|
|
</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="prod_name">Product Name *</Label>
|
|
<Input
|
|
id="prod_name"
|
|
value={form.name}
|
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
|
placeholder="e.g. Sensor Board v2"
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="sku">SKU *</Label>
|
|
<Input
|
|
id="sku"
|
|
value={form.sku}
|
|
onChange={(e) => setForm({ ...form, sku: e.target.value })}
|
|
placeholder="e.g. SB-V2-001"
|
|
className="font-mono"
|
|
required
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="prod_desc">Description</Label>
|
|
<Textarea
|
|
id="prod_desc"
|
|
value={form.description}
|
|
onChange={(e) =>
|
|
setForm({ ...form, description: e.target.value })
|
|
}
|
|
placeholder="Product description..."
|
|
rows={2}
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="version">Version</Label>
|
|
<Input
|
|
id="version"
|
|
value={form.version}
|
|
onChange={(e) => setForm({ ...form, version: e.target.value })}
|
|
placeholder="1.0"
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="status">Status</Label>
|
|
<Select
|
|
value={form.status}
|
|
onValueChange={(val) => setForm({ ...form, status: val })}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="draft">Draft</SelectItem>
|
|
<SelectItem value="active">Active</SelectItem>
|
|
<SelectItem value="deprecated">Deprecated</SelectItem>
|
|
<SelectItem value="archived">Archived</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</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..." : product ? "Update" : "Create"}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|