"use client"; import React from "react" import { useEffect, useState, useMemo } from "react"; import useSWR from "swr"; 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 { Checkbox } from "@/components/ui/checkbox"; import type { Component, Category, Customer, ComponentSpecs } from "@/lib/types"; const fetcher = (url: string) => fetch(url).then((r) => r.json()); // Category-specific specification fields const CATEGORY_SPECS: Record = { resistors: [ { key: "value", label: "Value" }, { key: "power", label: "Power" }, { key: "voltage", label: "Voltage" }, { key: "tolerance", label: "Tolerance" }, ], capacitors: [ { key: "value", label: "Value" }, { key: "power", label: "Power" }, { key: "voltage", label: "Voltage" }, { key: "tolerance", label: "Tolerance" }, ], inductors: [ { key: "value", label: "Value" }, { key: "tolerance", label: "Tolerance" }, ], leds: [ { key: "color", label: "Color" }, ], connectors: [ { key: "no_of_pins", label: "No. of Pins" }, { key: "no_of_rows", label: "No. of Rows" }, { key: "connector_type", label: "Type", type: "select", options: ["Female", "Male"] }, ], }; function getCategorySpecKey(categoryName: string | undefined): string | null { if (!categoryName) return null; const lower = categoryName.toLowerCase().trim(); // Match plural or singular forms if (lower.includes("resistor")) return "resistors"; if (lower.includes("capacitor")) return "capacitors"; if (lower.includes("inductor")) return "inductors"; if (lower.includes("led")) return "leds"; if (lower.includes("connector")) return "connectors"; return null; } interface ComponentDialogProps { open: boolean; onOpenChange: (open: boolean) => void; component: Component | null; categories: Category[]; onSave: (data: Partial) => Promise; } const emptySpecs: ComponentSpecs = {}; export function ComponentDialog({ open, onOpenChange, component, categories, onSave, }: ComponentDialogProps) { const [loading, setLoading] = useState(false); const { data: customers = [] } = useSWR("/api/customers", fetcher); const [form, setForm] = useState({ part_number: "", name: "", description: "", category_id: "", manufacturer: "", package_type: "", unit_price: "", stock_quantity: "", min_stock_level: "", datasheet_url: "", supplier: "", supplier_part_number: "", shelf_location: "", is_internal: false, customer_supplied: false, customer_id: "", }); const [specs, setSpecs] = useState({}); // Determine the selected category name for spec fields const selectedCategoryName = useMemo(() => { if (!form.category_id) return undefined; const cat = categories.find((c) => String(c.id) === form.category_id); return cat?.name; }, [form.category_id, categories]); const specKey = getCategorySpecKey(selectedCategoryName); const specFields = specKey ? CATEGORY_SPECS[specKey] : null; useEffect(() => { if (component) { setForm({ part_number: component.part_number || "", name: component.name || "", description: component.description || "", category_id: component.category_id ? String(component.category_id) : "", manufacturer: component.manufacturer || "", package_type: component.package_type || "", unit_price: String(component.unit_price || ""), stock_quantity: String(component.stock_quantity || ""), min_stock_level: String(component.min_stock_level || ""), datasheet_url: component.datasheet_url || "", supplier: component.supplier || "", supplier_part_number: component.supplier_part_number || "", shelf_location: component.shelf_location || "", is_internal: component.is_internal ?? false, customer_supplied: component.customer_supplied ?? false, customer_id: component.customer_id ? String(component.customer_id) : "", }); const rawSpecs = component.specs; setSpecs(typeof rawSpecs === "string" ? JSON.parse(rawSpecs) : rawSpecs || {}); } else { setForm({ part_number: "", name: "", description: "", category_id: "", manufacturer: "", package_type: "", unit_price: "", stock_quantity: "", min_stock_level: "", datasheet_url: "", supplier: "", supplier_part_number: "", shelf_location: "", is_internal: false, customer_supplied: false, customer_id: "", }); setSpecs({}); } }, [component, open]); function updateSpec(key: keyof ComponentSpecs, value: string) { setSpecs((prev) => ({ ...prev, [key]: value })); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); try { await onSave({ ...form, category_id: form.category_id ? Number(form.category_id) : null, unit_price: Number(form.unit_price) || 0, stock_quantity: Number(form.stock_quantity) || 0, min_stock_level: Number(form.min_stock_level) || 0, is_internal: form.is_internal, customer_supplied: form.customer_supplied, customer_id: form.customer_supplied && form.customer_id ? Number(form.customer_id) : null, specs, } as unknown as Partial); onOpenChange(false); } finally { setLoading(false); } } return ( {component ? "Edit Component" : "Add New Component"}
{/* 1. Part Number and Name */}
setForm({ ...form, part_number: e.target.value })} placeholder="e.g. RC0805-10K" className="font-mono" required />
setForm({ ...form, name: e.target.value })} placeholder="e.g. 10K Ohm Resistor" required />
{/* 2. Description */}