diff --git a/src/components/KategoriPengaduan.tsx b/src/components/KategoriPengaduan.tsx new file mode 100644 index 0000000..ffd3f54 --- /dev/null +++ b/src/components/KategoriPengaduan.tsx @@ -0,0 +1,234 @@ +import apiFetch from "@/lib/apiFetch"; +import { + ActionIcon, + Button, + Divider, + Flex, + Group, + Input, + Modal, + Stack, + Table, + Title, + Tooltip +} from "@mantine/core"; +import { useDisclosure, useShallowEffect } from "@mantine/hooks"; +import { IconEdit, IconPlus } from "@tabler/icons-react"; +import { useState } from "react"; +import useSWR from "swr"; +import notification from "./notificationGlobal"; + +export default function KategoriPengaduan() { + const [btnDisable, setBtnDisable] = useState(true); + const [btnLoading, setBtnLoading] = useState(false); + const [opened, { open, close }] = useDisclosure(false); + const [openedTambah, { open: openTambah, close: closeTambah }] = useDisclosure(false); + const { data, mutate, isLoading } = useSWR("/", () => + apiFetch.api.pengaduan.category.get(), + ); + const list = data?.data || []; + const [dataEdit, setDataEdit] = useState({ + id: "", + name: "", + }); + const [dataTambah, setDataTambah] = useState("") + + useShallowEffect(() => { + mutate(); + }, []); + + async function handleCreate() { + try { + setBtnLoading(true); + const res = await apiFetch.api.pengaduan.category.create.post({ name: dataTambah }); + if (res.status === 200) { + mutate(); + closeTambah(); + setDataTambah(""); + notification({ + title: "Success", + message: "Your category have been saved", + type: "success", + }) + } else { + notification({ + title: "Error", + message: "Failed to create category", + type: "error", + }) + } + } catch (error) { + console.log(error); + notification({ + title: "Error", + message: "Failed to create category", + type: "error", + }) + } finally { + setBtnLoading(false); + } + } + + async function handleEdit() { + try { + setBtnLoading(true); + const res = await apiFetch.api.pengaduan.category.update.post(dataEdit); + if (res.status === 200) { + mutate(); + close(); + notification({ + title: "Success", + message: "Your category have been saved", + type: "success", + }) + } else { + notification({ + title: "Error", + message: "Failed to edit category", + type: "error", + }) + } + } catch (error) { + console.log(error); + notification({ + title: "Error", + message: "Failed to edit category", + type: "error", + }) + } finally { + setBtnLoading(false); + } + } + + function chooseEdit({ data }: { data: { id: string, value: string, name: string } }) { + setDataEdit(data); + open(); + } + + function onValidation({ kat, value, aksi }: { kat: 'name', value: string, aksi: 'edit' | 'tambah' }) { + if (value.length < 1) { + setBtnDisable(true); + } else { + setBtnDisable(false); + } + + if (kat === 'name') { + if (aksi === 'edit') { + setDataEdit({ ...dataEdit, name: value }); + } else { + setDataTambah(value); + } + } + } + + useShallowEffect(() => { + if (dataEdit.name.length > 0) { + setBtnDisable(false); + } + }, [dataEdit.id]); + + useShallowEffect(() => { + if (dataTambah.length > 0) { + setBtnDisable(false); + } + }, [dataTambah]); + + return ( + <> + {/* Modal Edit */} + + + + onValidation({ kat: 'name', value: e.target.value, aksi: 'edit' })} /> + + + + + + + + + {/* Modal Tambah */} + + + + onValidation({ kat: 'name', value: e.target.value, aksi: 'tambah' })} /> + + + + + + + + + + + + + + Kategori Pengaduan + + + + + + + + + + + Kategori + Aksi + + + + {list?.map((v: any) => ( + + {v.name} + + + chooseEdit({ data: v })} + > + + + + + + ))} + +
+
+
+ + ); +} diff --git a/src/pages/scr/dashboard/setting/detail_setting_page.tsx b/src/pages/scr/dashboard/setting/detail_setting_page.tsx index 230e5ae..767f4cb 100644 --- a/src/pages/scr/dashboard/setting/detail_setting_page.tsx +++ b/src/pages/scr/dashboard/setting/detail_setting_page.tsx @@ -1,4 +1,5 @@ import DesaSetting from "@/components/DesaSetting"; +import KategoriPengaduan from "@/components/KategoriPengaduan"; import { Button, Card, @@ -80,7 +81,7 @@ export default function DetailSettingPage() { }} > {type === "cat-pengaduan" ? ( - + ) : type === "cat-pelayanan" ? ( ) : type === "desa" ? (