diff --git a/src/components/DesaSetting.tsx b/src/components/DesaSetting.tsx index d136937..6a5ba29 100644 --- a/src/components/DesaSetting.tsx +++ b/src/components/DesaSetting.tsx @@ -1,8 +1,10 @@ import apiFetch from "@/lib/apiFetch"; import { ActionIcon, + Anchor, Button, Divider, + FileInput, Flex, Group, Input, @@ -10,18 +12,23 @@ import { Stack, Table, Title, - Tooltip, + 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(), ); @@ -39,7 +46,31 @@ export default function DesaSetting() { async function handleEdit() { try { setBtnLoading(true); - const res = await apiFetch.api["configuration-desa"].edit.post(dataEdit); + + 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(); @@ -67,11 +98,8 @@ export default function DesaSetting() { } } - function chooseEdit({ - data, - }: { - data: { id: string; value: string; name: string }; - }) { + + function chooseEdit({ data }: { data: { id: string; value: string; name: string }; }) { setDataEdit(data); open(); } @@ -100,18 +128,35 @@ export default function DesaSetting() { opened={opened} onClose={close} title={"Edit"} - centered overlayProps={{ backgroundOpacity: 0.55, blur: 3 }} > - - - onValidation({ kat: "value", value: e.target.value }) - } - /> - + { + dataEdit.name == "TTD" + ? + ( + + { setImg(e) }} + /> + + ) + : + ( + + + onValidation({ kat: "value", value: e.target.value }) + } + /> + + ) + } +