"use client"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { Button } from "@/components/ui/button"; import { getAgentById, updateAgent } from "@/service/agent"; import { useRouter } from "next/navigation"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { X } from "lucide-react"; type AgentFormValues = { fullName: string; position: string; phone: string; roles: string[]; profileImage: File | null; }; const agentTypes = ["After Sales", "Sales", "Spv", "Branch Manager"]; export default function UpdateAgentForm({ id }: { id: number }) { const [loading, setLoading] = useState(true); const [previewImg, setPreviewImg] = useState(null); const [agentData, setAgentData] = useState(null); const [file, setFile] = useState(null); const [preview, setPreview] = useState(null); const router = useRouter(); const MySwal = withReactContent(Swal); const form = useForm({ defaultValues: { fullName: "", position: "", phone: "", roles: [], profileImage: null, }, }); useEffect(() => { async function fetchData() { try { const res = await getAgentById(id); if (!res || !res.data) { console.error("DATA AGENT TIDAK DITEMUKAN"); return; } setAgentData(res.data); form.reset({ fullName: res?.data?.data?.name, position: res?.data?.data?.job_title, phone: res?.data?.data?.phone, roles: res?.data?.data?.agent_type || [], profileImage: null, }); console.log("name", res?.data?.data?.name); setPreviewImg(res.data.data.profile_picture_path || null); } catch (err) { console.error("ERROR FETCH DATA AGENT:", err); } finally { setLoading(false); } } fetchData(); }, [id, form]); const handleFileChange = (e: any) => { const selected = e.target.files[0]; if (selected) { setFile(selected); setPreview(URL.createObjectURL(selected)); } }; const handleRemoveFile = () => { setFile(null); setPreview(null); }; const onSubmit = async (data: AgentFormValues) => { try { const formData = new FormData(); formData.append("name", data.fullName); formData.append("job_title", data.position); formData.append("phone", data.phone); data.roles.forEach((role) => { formData.append("agent_type", role); }); if (file) { formData.append("file", file); } await updateAgent(id, formData); successSubmit("/admin/agent"); } catch (err) { console.error("ERROR UPDATE AGENT:", err); } }; function successSubmit(redirect: string) { MySwal.fire({ title: "Data Berhasil Diupdate", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((res) => { if (res.isConfirmed) router.push(redirect); }); } if (loading) { return
Loading data...
; } return (

Edit Agen

( Nama Lengkap * )} /> {/* Jabatan */} ( Jabatan * )} /> {/* Telepon */} ( No Telp * )} />
{/* ROLES */}
Pilih Jenis Agen *
{agentTypes.map((role) => ( { const selected = field.value || []; return ( { const updated = checked ? [...selected, role] : selected.filter((i) => i !== role); field.onChange(updated); }} /> {role} ); }} /> ))}
{/* FOTO */}
Foto Agen
{preview && (
Preview
)}
{/* BUTTON */}
); }