'use client' import colors from '@/con/colors'; import ApiFetch from '@/lib/api-fetch'; import { Box, Button, Center, FileInput, Image, Paper, Stack, Text, TextInput, Title } from '@mantine/core'; import { IconArrowBack, IconImageInPicture } 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 infoTeknoState from '../../../_state/inovasi/info-tekno'; function CreateInfoTeknologiTepatGuna() { const stateInfoTekno = useProxy(infoTeknoState) const [previewImage, setPreviewImage] = useState(null); const [file, setFile] = useState(null); const router = useRouter() const resetForm = () => { stateInfoTekno.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 stateInfoTekno.create.form.imageId = uploaded.id // Submit the form const success = await stateInfoTekno.create.create() if (success) { resetForm() router.push("/admin/inovasi/info-teknologi-tepat-guna") } } catch (error) { console.error("Error in handleSubmit:", error) toast.error("Terjadi kesalahan saat menyimpan data") } } return ( Create Info Teknologi Tepat Guna { stateInfoTekno.create.form.name = val.target.value; }} label={Nama Info Teknologi Tepat Guna} placeholder="masukkan nama info teknologi tepat guna" /> Deskripsi { stateInfoTekno.create.form.deskripsi = htmlContent; }} /> Upload Gambar Konten} value={file} onChange={async (e) => { if (!e) return; setFile(e); const base64 = await e.arrayBuffer().then((buf) => "data:image/png;base64," + Buffer.from(buf).toString("base64") ); setPreviewImage(base64); }} /> {previewImage ? ( ) : (
)}
); } export default CreateInfoTeknologiTepatGuna;