import apiFetch from "@/lib/apiFetch"; import { ActionIcon, Anchor, Button, Divider, FileInput, Flex, Group, Input, Modal, Stack, Table, Title, Tooltip } from "@mantine/core"; import { useDisclosure, useShallowEffect } from "@mantine/hooks"; import { IconEdit } from "@tabler/icons-react"; import { useState } from "react"; import useSWR from "swr"; import ModalFile from "./ModalFile"; import notification from "./notificationGlobal"; import _ from "lodash"; export default function DesaSetting() { const [btnDisable, setBtnDisable] = useState(false); const [btnLoading, setBtnLoading] = useState(false); const [opened, { open, close }] = useDisclosure(false); const [img, setImg] = useState() const [openedPreview, setOpenedPreview] = useState(false); const [viewImg, setViewImg] = useState(""); const { data, mutate, isLoading } = useSWR("/", () => apiFetch.api["configuration-desa"].list.get(), ); const list = data?.data || []; const [dataEdit, setDataEdit] = useState({ id: "", value: "", name: "", }); useShallowEffect(() => { mutate(); }, []); async function handleEdit() { try { setBtnLoading(true); let finalData = { ...dataEdit }; // ← buffer data terbaru if (dataEdit.name === "TTD") { const resImg = await apiFetch.api.pengaduan.upload.post({ file: img }); if (resImg.status === 200) { finalData = { ...finalData, value: resImg.data?.filename || "" }; setDataEdit(finalData); // update state } else { return notification({ title: "Error", message: "Failed to upload image", type: "error", }); } } const res = await apiFetch.api["configuration-desa"].edit.post(finalData); if (res.status === 200) { mutate(); close(); notification({ title: "Success", message: "Your settings have been saved", type: "success", }); } else { notification({ title: "Error", message: "Failed to edit configuration", type: "error", }); } } catch (error) { console.error(error); notification({ title: "Error", message: "Failed to edit configuration", type: "error", }); } finally { setBtnLoading(false); } } function chooseEdit({ data }: { data: { id: string; value: string; name: string }; }) { setDataEdit(data); open(); } function onValidation({ kat, value }: { kat: "value"; value: string }) { if (value.length < 1) { setBtnDisable(true); } else { setBtnDisable(false); } if (kat === "value") { setDataEdit({ ...dataEdit, value: value }); } } useShallowEffect(() => { if (dataEdit.value.length > 0) { setBtnDisable(false); } }, [dataEdit.id]); return ( <> { dataEdit.name == "TTD" ? ( { setImg(e) }} /> ) : ( onValidation({ kat: "value", value: e.target.value }) } /> ) } setOpenedPreview(false)} folder="syarat-dokumen" fileName={viewImg} /> Pengaturan Desa Nama Value Aksi {list?.map((v: any) => ( {v.name} { v.name == "TTD" ? { setViewImg(v.value); setOpenedPreview(true); }} underline="always"> Lihat : v.value } chooseEdit({ data: v })} > ))}
); }