import apiFetch from "@/lib/apiFetch"; import { ActionIcon, Button, Divider, Flex, Group, Input, Modal, Select, Stack, Table, Text, Title, Tooltip } from "@mantine/core"; import { useDisclosure, useShallowEffect } from "@mantine/hooks"; import { IconEdit, IconPlus, IconTrash } from "@tabler/icons-react"; import { useState } from "react"; import useSWR from "swr"; import notification from "./notificationGlobal"; export default function UserSetting() { const [btnDisable, setBtnDisable] = useState(true); const [btnLoading, setBtnLoading] = useState(false); const [opened, { open, close }] = useDisclosure(false); const [openedDelete, { open: openDelete, close: closeDelete }] = useDisclosure(false); const [dataDelete, setDataDelete] = useState("") const { data: dataRole, mutate: mutateRole, isLoading: isLoadingRole } = useSWR("user-role", () => apiFetch.api.user.role.get(), ); const [openedTambah, { open: openTambah, close: closeTambah }] = useDisclosure(false); const { data, mutate, isLoading } = useSWR("user-list", () => apiFetch.api.user.list.get(), ); const list = data?.data || []; const listRole = dataRole?.data || []; const [dataEdit, setDataEdit] = useState({ id: "", name: "", phone: "", email: "", roleId: "", }); const [dataTambah, setDataTambah] = useState({ name: "", email: "", roleId: "", password: "", phone: "", }) const [error, setError] = useState({ name: false, email: false, roleId: false, password: false, phone: false, }) useShallowEffect(() => { mutate(); }, []); async function handleCreate() { try { setBtnLoading(true); const res = await apiFetch.api.user.create.post(dataTambah); if (res.status === 200) { mutate(); closeTambah(); setDataTambah({ name: "", email: "", roleId: "", password: "", phone: "", }); notification({ title: "Success", message: "Your user have been saved", type: "success", }) } else { notification({ title: "Error", message: "Failed to create user ", type: "error", }) } } catch (error) { console.error(error); notification({ title: "Error", message: "Failed to create user", 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.error(error); notification({ title: "Error", message: "Failed to edit category", type: "error", }) } finally { setBtnLoading(false); } } async function handleDelete() { try { setBtnLoading(true); const res = await apiFetch.api.user.delete.post({ id: dataDelete }); if (res.status === 200) { mutate(); closeDelete(); notification({ title: "Success", message: "Your user have been deleted", type: "success", }) } else { notification({ title: "Error", message: "Failed to delete user", type: "error", }) } } catch (error) { console.error(error); notification({ title: "Error", message: "Failed to delete user", type: "error", }) } finally { setBtnLoading(false); } } function chooseEdit({ data }: { data: { id: string, name: string, phone: string, email: string, roleId: string } }) { setDataEdit(data); open(); } function onValidation({ kat, value, aksi }: { kat: 'name' | 'email' | 'roleId' | 'password' | 'phone', value: string | null, aksi: 'edit' | 'tambah' }) { if (value == null || value.length < 1) { setBtnDisable(true); setError({ ...error, [kat]: true }); } else { setBtnDisable(false); setError({ ...error, [kat]: false }); } if (aksi === 'edit') { setDataEdit({ ...dataEdit, [kat]: value }); } else { setDataTambah({ ...dataTambah, [kat]: value }); } } useShallowEffect(() => { if (dataEdit.name.length > 0) { setBtnDisable(false); } }, [dataEdit.id]); return ( <> {/* Modal Edit */} onValidation({ kat: 'name', value: e.target.value, aksi: 'edit' })} /> {/* Modal Tambah */} onValidation({ kat: 'name', value: e.target.value, aksi: 'tambah' })} /> onValidation({ kat: 'phone', value: e.target.value, aksi: 'tambah' })} /> onValidation({ kat: 'email', value: e.target.value, aksi: 'tambah' })} /> onValidation({ kat: 'password', value: e.target.value, aksi: 'tambah' })} /> {/* Modal Delete */} Apakah anda yakin ingin menghapus user ini? Daftar User Nama Telepon Email Role Aksi { list.length > 0 ? ( list?.map((v: any) => ( {v.name} {v.phone} {v.email} {v.roleId} chooseEdit({ data: v })} > { setDataDelete(v.id) openDelete() }} > )) ) : ( Data User Tidak Ditemukan ) }
); }