/* eslint-disable react-hooks/exhaustive-deps */ 'use client'; import apbdes from '@/app/admin/(dashboard)/_state/landing-page/apbdes'; import colors from '@/con/colors'; import ApiFetch from '@/lib/api-fetch'; import { Box, Button, Group, Image, Paper, Stack, Text, TextInput, Title, Tooltip, } from '@mantine/core'; import { Dropzone } from '@mantine/dropzone'; import { IconArrowBack, IconFile, IconPhoto, IconUpload, IconX } from '@tabler/icons-react'; import { useParams, useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; function EditAPBDes() { const apbdesState = useProxy(apbdes); const router = useRouter(); const params = useParams(); const [previewImage, setPreviewImage] = useState(null); const [previewDoc, setPreviewDoc] = useState(null); const [imageFile, setImageFile] = useState(null); const [docFile, setDocFile] = useState(null); const [formData, setFormData] = useState({ name: apbdesState.edit.form.name || '', jumlah: apbdesState.edit.form.jumlah || '', imageId: apbdesState.edit.form.imageId || '', fileId: apbdesState.edit.form.fileId || '' }); // Load APBDes data by id useEffect(() => { const loadAPBDes = async () => { const id = params?.id as string; if (!id) return; try { const data = await apbdesState.edit.load(id); if (data) { setFormData({ name: data.name || '', jumlah: data.jumlah || '', imageId: data.imageId || '', fileId: data.fileId || '' }); if (data.image?.link) setPreviewImage(data.image.link); if (data.file?.link) setPreviewDoc(data.file.link); } } catch (error) { console.error('Error loading APBDes:', error); toast.error('Gagal memuat data APBDes'); } }; loadAPBDes(); }, [params?.id]); const handleSubmit = async () => { try { // Update global state with form data apbdesState.edit.form = { ...apbdesState.edit.form, ...formData, }; // Upload new image if exists if (imageFile) { const res = await ApiFetch.api.fileStorage.create.post({ file: imageFile, name: imageFile.name }); const uploaded = res.data?.data; if (!uploaded?.id) { return toast.error('Gagal upload gambar'); } apbdesState.edit.form.imageId = uploaded.id; } // Upload new document if exists if (docFile) { const res = await ApiFetch.api.fileStorage.create.post({ file: docFile, name: docFile.name }); const uploaded = res.data?.data; if (!uploaded?.id) { return toast.error('Gagal upload dokumen'); } apbdesState.edit.form.fileId = uploaded.id; } await apbdesState.edit.update(); toast.success('APBDes berhasil diperbarui!'); router.push('/admin/landing-page/apbdes'); } catch (error) { console.error('Error updating APBDes:', error); toast.error('Terjadi kesalahan saat memperbarui APBDes'); } }; return ( Edit APBDes setFormData({ ...formData, name: e.target.value })} required /> setFormData({ ...formData, jumlah: e.target.value })} required /> Gambar APBDes { const selectedFile = files[0]; if (selectedFile) { setImageFile(selectedFile); setPreviewImage(URL.createObjectURL(selectedFile)); } }} onReject={() => toast.error('File tidak valid, gunakan format gambar')} maxSize={5 * 1024 ** 2} accept={{ 'image/*': [] }} radius="md" p="xl" > Seret gambar atau klik untuk memilih file Maksimal 5MB, format gambar wajib {previewImage && ( Preview Gambar )} Dokumen APBDes { const selectedFile = files[0]; if (selectedFile) { setDocFile(selectedFile); setPreviewDoc(URL.createObjectURL(selectedFile)); } }} onReject={() => toast.error('File tidak valid, gunakan format dokumen')} maxSize={10 * 1024 ** 2} // 10MB accept={{ 'application/pdf': ['.pdf'], 'application/msword': ['.doc'], 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx'], 'application/vnd.ms-excel': ['.xls'], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'], }} radius="md" p="xl" > Seret dokumen atau klik untuk memilih file Maksimal 10MB, format PDF/DOC/DOCX/XLS/XLSX {previewDoc && ( Dokumen terpilih: {docFile?.name || 'Dokumen'} )} ); } export default EditAPBDes;