/* eslint-disable @typescript-eslint/no-explicit-any */ 'use client' import colors from '@/con/colors'; import { Alert, Box, Button, Center, Group, Image, Paper, Stack, Text, TextInput, Title, Tooltip } from '@mantine/core'; import { useEffect, useState } from 'react'; import { useProxy } from 'valtio/utils'; import stateProfilePPID from '../../../_state/ppid/profile_ppid/profile_PPID'; import ApiFetch from '@/lib/api-fetch'; import { Dropzone } from '@mantine/dropzone'; import { IconAlertCircle, IconArrowBack, IconPhoto, IconUpload, IconX } from '@tabler/icons-react'; import { useParams, useRouter } from 'next/navigation'; import { toast } from 'react-toastify'; import Biodata from './biodata/biodataForm'; import PengalamanOrganisasi from './pengalaman_organisasi/pengalamanForm'; import ProgramKerjaUnggulan from './program_kerja_unggulan/programKerjaForm'; import RiwayatKarir from './riwayat_karir/riwayatKarirForm'; function EditProfilePPID() { const allState = useProxy(stateProfilePPID); const params = useParams(); const router = useRouter(); // UI States const [previewImage, setPreviewImage] = useState(null); const [file, setFile] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); // Load data on mount useEffect(() => { const loadData = async () => { const id = params?.id as string; if (!id) { toast.error("ID tidak valid"); router.push("/admin/ppid/profile-ppid"); return; } try { const profileData = await stateProfilePPID.loadForEdit(id); if (profileData && profileData.image?.link) { setPreviewImage(profileData.image.link); } } catch (error) { console.error("Error loading profile:", error); toast.error("Gagal memuat data profile"); } }; loadData(); return () => { stateProfilePPID.editForm.reset(); // cleanup form }; }, [params?.id, router]); const handleFieldChange = (field: string, value: string) => { stateProfilePPID.editForm.updateField(field as any, value); }; const handleSubmit = async () => { if (isSubmitting || !stateProfilePPID.editForm.form.name.trim()) { toast.error("Nama wajib diisi"); return; } setIsSubmitting(true); try { // Upload file jika ada if (file) { const uploadResponse = await ApiFetch.api.fileStorage.create.post({ file, name: file.name }); const uploaded = uploadResponse.data?.data; if (!uploaded?.id) { toast.error("Gagal upload gambar"); return; } stateProfilePPID.editForm.form.imageId = uploaded.id; } // Submit form const success = await stateProfilePPID.editForm.submit(); if (success) { toast.success("Berhasil menyimpan perubahan"); router.push("/admin/ppid/profile-ppid"); } } catch (error) { console.error("Error submitting form:", error); toast.error("Gagal menyimpan profile"); } finally { setIsSubmitting(false); } }; const handleBack = () => { router.back(); }; // Loading state if (allState.profile.loading) { return (
Memuat data profile...
); } // Error state if (allState.profile.error) { return ( } color="red"> Error {allState.profile.error} ); } // No data state if (!allState.profile.data) { return ( } color="yellow"> Data tidak ditemukan Profile PPID tidak dapat ditemukan ); } return ( Edit Profil PPID Edit Profile PPID {/* Nama Field */} Nama Perbekel} placeholder="Masukkan nama perbekel" value={allState.editForm.form.name} onChange={(e) => handleFieldChange('name', e.currentTarget.value)} error={!allState.editForm.form.name && "Nama wajib diisi"} /> {/* File Upload */} Gambar { const selectedFile = files[0]; // Ambil file pertama if (selectedFile) { setFile(selectedFile); setPreviewImage(URL.createObjectURL(selectedFile)); // Buat preview } }} onReject={() => toast.error('File tidak valid.')} maxSize={5 * 1024 ** 2} // Maks 5MB accept={{ 'image/*': [] }} >
Tarik gambar ke sini atau klik untuk memilih file Maksimal 5MB dan harus format gambar
{/* Tampilkan preview kalau ada */} {previewImage && ( Preview )}
{/* Rich Components */} handleFieldChange('biodata', val)} /> handleFieldChange('riwayat', val)} /> handleFieldChange('pengalaman', val)} /> handleFieldChange('unggulan', val)} /> {/* Submit Button */}
); } export default EditProfilePPID;