/* eslint-disable react-hooks/exhaustive-deps */ 'use client' import CreateEditor from '@/app/admin/(dashboard)/_com/createEditor'; import layananonlineDesa from '@/app/admin/(dashboard)/_state/inovasi/layanan-online-desa'; import colors from '@/con/colors'; import ApiFetch from '@/lib/api-fetch'; import { Box, Button, Center, Group, Image, Modal, Paper, Select, Stack, Text, TextInput, Title } from '@mantine/core'; import { Dropzone } from '@mantine/dropzone'; import { useDisclosure } from '@mantine/hooks'; import { IconImageInPicture, IconMessageCircleQuestion, IconPhoto, IconUpload, IconX } from '@tabler/icons-react'; import { motion } from 'framer-motion'; import { useState, useEffect } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; function PengaduanMasyarakat() { const [opened, { open, close }] = useDisclosure(false); const state = useProxy(layananonlineDesa); const [previewImage, setPreviewImage] = useState(null); const [file, setFile] = useState(null); useEffect(() => { // ✅ Panggil load data jenis layanan dari backend if (!state.jenisPengaduan.findMany.data) { state.jenisPengaduan.findMany.load(); } }, []); const resetForm = () => { state.pengaduanMasyarakat.create.form = { name: '', email: '', nomorTelepon: '', nik: '', judulPengaduan: '', lokasiKejadian: '', deskripsiPengaduan: '', jenisPengaduanId: '', imageId: '', }; }; const resetAll = () => { resetForm(); setFile(null); setPreviewImage(null); }; const handleSubmit = async () => { if (!file) { return toast.error("Silahkan pilih file gambar terlebih dahulu") } try { // Upload the image first const uploadRes = await ApiFetch.api.fileStorage.create.post({ file: file, name: file.name }) const uploaded = uploadRes.data?.data if (!uploaded?.id) { return toast.error("Gagal upload gambar") } // Set the image ID in the form state.pengaduanMasyarakat.create.form.imageId = uploaded.id // Submit the form const success = await state.pengaduanMasyarakat.create.create() if (success) { resetAll() close() } } catch (error) { console.error("Error in handleSubmit:", error) toast.error("Terjadi kesalahan saat menyimpan data") } }; return ( Pengaduan Masyarakat Sampaikan keluhan dan aspirasi Anda melalui platform digital kami Ajukan Pengaduan Masyarakat Nama} placeholder="Masukkan nama" onChange={(val) => (state.pengaduanMasyarakat.create.form.name = val.target.value)} /> Email} placeholder="Masukkan email" onChange={(val) => (state.pengaduanMasyarakat.create.form.email = val.target.value)} /> Nomor Telepon} placeholder="Masukkan nomor telepon" onChange={(val) => (state.pengaduanMasyarakat.create.form.nomorTelepon = val.target.value)} /> NIK} placeholder="Masukkan nik" onChange={(val) => (state.pengaduanMasyarakat.create.form.nik = val.target.value)} /> Judul Pengaduan} placeholder="Masukkan judul pengaduan" onChange={(val) => (state.pengaduanMasyarakat.create.form.judulPengaduan = val.target.value)} /> Lokasi Kejadian} placeholder="Masukkan lokasi kejadian" onChange={(val) => (state.pengaduanMasyarakat.create.form.lokasiKejadian = val.target.value)} /> Deskripsi { state.pengaduanMasyarakat.create.form.deskripsiPengaduan = htmlContent; }} />