diff --git a/src/app/admin/(dashboard)/landing-page/apbdes/[id]/edit/page.tsx b/src/app/admin/(dashboard)/landing-page/apbdes/[id]/edit/page.tsx index e40d354f..4d4c74f9 100644 --- a/src/app/admin/(dashboard)/landing-page/apbdes/[id]/edit/page.tsx +++ b/src/app/admin/(dashboard)/landing-page/apbdes/[id]/edit/page.tsx @@ -1,26 +1,27 @@ /* eslint-disable react-hooks/exhaustive-deps */ -"use client"; +'use client'; - -import apbdes from "@/app/admin/(dashboard)/_state/landing-page/apbdes"; -import colors from "@/con/colors"; -import ApiFetch from "@/lib/api-fetch"; +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 -} from "@mantine/core"; -import { Dropzone } from "@mantine/dropzone"; -import { IconArrowBack, IconFile, IconImageInPicture, 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"; + 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); @@ -38,14 +39,14 @@ function EditAPBDes() { fileId: apbdesState.edit.form.fileId || '' }); - // Load sdgs desa by id saat pertama kali + // Load APBDes data by id useEffect(() => { - const loadKolaborasi = async () => { + const loadAPBDes = async () => { const id = params?.id as string; if (!id) return; try { - const data = await apbdesState.edit.load(id); // akses langsung, bukan dari proxy + const data = await apbdesState.edit.load(id); if (data) { setFormData({ name: data.name || '', @@ -53,202 +54,238 @@ function EditAPBDes() { imageId: data.imageId || '', fileId: data.fileId || '' }); - if (data.image) { - if (data?.image?.link) { - setPreviewImage(data.image.link); - } - } - if (data.file) { - if (data?.file?.link) { - setPreviewDoc(data.file.link); - } - } + + 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"); + console.error('Error loading APBDes:', error); + toast.error('Gagal memuat data APBDes'); } }; - loadKolaborasi(); + loadAPBDes(); }, [params?.id]); const handleSubmit = async () => { - try { - // edit global state with form data + // Update global state with form data apbdesState.edit.form = { ...apbdesState.edit.form, - name: formData.name, - jumlah: formData.jumlah, - imageId: formData.imageId // Keep existing imageId if not changed + ...formData, }; - // Jika ada file image baru, upload + // Upload new image if exists if (imageFile) { - const res = await ApiFetch.api.fileStorage.create.post({ file: imageFile, name: imageFile.name }); + 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"); + return toast.error('Gagal upload gambar'); } - // edit imageId in global state apbdesState.edit.form.imageId = uploaded.id; } - // Jika ada file doc baru, upload + // Upload new document if exists if (docFile) { - const res = await ApiFetch.api.fileStorage.create.post({ file: docFile, name: docFile.name }); + 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 doc"); + return toast.error('Gagal upload dokumen'); } - // edit fileId in global state apbdesState.edit.form.fileId = uploaded.id; } await apbdesState.edit.update(); - toast.success("apbdes berhasil diperbarui!"); - router.push("/admin/landing-page/APBDes"); + 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"); + console.error('Error updating APBDes:', error); + toast.error('Terjadi kesalahan saat memperbarui APBDes'); } }; return ( - - - - - - - Edit APBDes + + + + + + + Edit APBDes + + + + + setFormData({ ...formData, name: e.target.value })} - label={Nama} - placeholder="masukkan nama" + required /> setFormData({ ...formData, jumlah: e.target.value })} - label={Jumlah} - placeholder="masukkan jumlah" + required /> - - File Image - - { - const selectedFile = files[0]; // Ambil file pertama - if (selectedFile) { - setImageFile(selectedFile); - setPreviewImage(URL.createObjectURL(selectedFile)); // Buat preview - } - }} - onReject={() => toast.error('File tidak valid.')} - maxSize={5 * 1024 ** 2} // Maks 5MB - accept={{ - 'application/*': ['.jpeg', '.jpg', '.png', '.gif', '.webp', '.svg'], - }} - > - - - - - - - - - - -
- - Drag file ke sini atau klik untuk pilih image - - - Maksimal 5MB dan harus format image - -
-
-
- - Image - {previewImage ? ( -