"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogClose } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Loader2, Settings } from "lucide-react"; import { useToast } from "@/components/ui/toast-provider"; interface JenisPendaftaran { jenis_pendaftaran: string; } interface EditJenisPendaftaranProps { onUpdateSuccess?: () => void; } export default function EditJenisPendaftaran({ onUpdateSuccess }: EditJenisPendaftaranProps) { const { showSuccess, showError } = useToast(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [jenisPendaftaranList, setJenisPendaftaranList] = useState([]); const [loading, setLoading] = useState(false); const [updating, setUpdating] = useState(false); const [error, setError] = useState(null); // State for selected jenis pendaftaran and new value const [selectedJenisPendaftaran, setSelectedJenisPendaftaran] = useState(""); const [newValue, setNewValue] = useState(""); // Fetch jenis pendaftaran data when dialog opens useEffect(() => { if (isDialogOpen) { fetchJenisPendaftaran(); resetForm(); } }, [isDialogOpen]); // Update new value when selected jenis pendaftaran changes useEffect(() => { if (selectedJenisPendaftaran) { setNewValue(selectedJenisPendaftaran); } }, [selectedJenisPendaftaran]); // Reset form const resetForm = () => { setSelectedJenisPendaftaran(""); setNewValue(""); setError(null); }; // Fetch unique jenis pendaftaran values const fetchJenisPendaftaran = async () => { try { setLoading(true); setError(null); const response = await fetch("/api/keloladata/setting-jenis-pendaftaran"); if (!response.ok) { throw new Error("Failed to fetch data"); } const data = await response.json(); setJenisPendaftaranList(data); } catch (err) { setError("Error fetching data. Please try again later."); console.error("Error fetching data:", err); } finally { setLoading(false); } }; // Handle save changes const handleSaveChanges = async () => { try { if (!selectedJenisPendaftaran || !newValue) { setError("Pilih jenis pendaftaran dan masukkan nilai baru"); return; } if (selectedJenisPendaftaran === newValue) { setError("Nilai baru harus berbeda dengan nilai lama"); return; } setUpdating(true); setError(null); const response = await fetch("/api/keloladata/setting-jenis-pendaftaran", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ oldValue: selectedJenisPendaftaran, newValue: newValue }), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || "Failed to update jenis pendaftaran"); } // Reset form and notify parent component resetForm(); // Refresh the list await fetchJenisPendaftaran(); // Show success message showSuccess("Berhasil!", "Jenis pendaftaran berhasil diperbarui"); // Close dialog setIsDialogOpen(false); // Notify parent component if (onUpdateSuccess) { onUpdateSuccess(); } } catch (err) { setError((err as Error).message); console.error("Error updating jenis pendaftaran:", err); showError("Gagal!", (err as Error).message); } finally { setUpdating(false); } }; return ( <> Edit Jenis Pendaftaran {loading ? (
) : error ? (
{error}
) : (
setNewValue(e.target.value)} placeholder="Masukkan jenis pendaftaran baru" disabled={!selectedJenisPendaftaran} />
)}
); }