"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"; 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 router = useRouter(); const MySwal = withReactContent(Swal); const form = useForm({ defaultValues: { fullName: "", position: "", phone: "", roles: [], profileImage: null, }, }); // ================================ // 🔥 FETCH API & SET DEFAULT VALUES // ================================ useEffect(() => { async function fetchData() { try { const res = await getAgentById(id); if (!res || !res.data) { console.error("DATA AGENT TIDAK DITEMUKAN"); return; } setAgentData(res.data); // set form default values 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.profile_picture_path || null); } catch (err) { console.error("ERROR FETCH DATA AGENT:", err); } finally { setLoading(false); } } fetchData(); }, [id, form]); const handleImageChange = (file?: File) => { if (!file) return; form.setValue("profileImage", file); const preview = URL.createObjectURL(file); setPreviewImg(preview); }; const onSubmit = async (data: AgentFormValues) => { const payload = { name: data.fullName, job_title: data.position, phone: data.phone, agent_type: data.roles, profile_picture_path: data.profileImage ? data.profileImage.name : agentData.profile_picture_path, }; try { await updateAgent(id, payload); 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

{/* INPUT GRID */}
{/* Nama */} ( 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
{previewImg && ( )} handleImageChange(e.target.files?.[0])} />
{/* BUTTON */}
); }