'use client' import colors from '@/con/colors'; import ApiFetch from '@/lib/api-fetch'; import { Box, Button, Group, Image, Paper, Stack, Text, TextInput, Title } from '@mantine/core'; import { IconArrowBack, IconPhoto, IconUpload, IconX } from '@tabler/icons-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; import CreateEditor from '../../../_com/createEditor'; import desaDigitalState from '../../../_state/inovasi/desa-digital'; import { Dropzone } from '@mantine/dropzone'; function CreateDesaDigital() { const stateDesaDigital = useProxy(desaDigitalState) const [previewImage, setPreviewImage] = useState(null); const [file, setFile] = useState(null); const router = useRouter() const resetForm = () => { stateDesaDigital.create.form = { name: "", deskripsi: "", imageId: "", } setPreviewImage(null) setFile(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 stateDesaDigital.create.form.imageId = uploaded.id // Submit the form const success = await stateDesaDigital.create.create() if (success) { resetForm() router.push("/admin/inovasi/desa-digital-smart-village") } } catch (error) { console.error("Error in handleSubmit:", error) toast.error("Terjadi kesalahan saat menyimpan data") } } return ( Create Desa Digital Smart Village { stateDesaDigital.create.form.name = val.target.value; }} label={Nama Desa Digital Smart Village} placeholder="masukkan nama desa digital smart village" /> Deskripsi { stateDesaDigital.create.form.deskripsi = htmlContent; }} /> 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/*': [] }} >
Drag gambar ke sini atau klik untuk pilih file Maksimal 5MB dan harus format gambar
{/* Tampilkan preview kalau ada */} {previewImage && ( Preview )}
); } export default CreateDesaDigital;