import { WARNA, LayoutDrawer, API_ADDRESS } from "@/module/_global"; import { funGetAllGroup, IDataGroup } from "@/module/group"; import { Box, Stack, SimpleGrid, Flex, TextInput, Button, Text, Select } from "@mantine/core"; import { useShallowEffect } from "@mantine/hooks"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { IoAddCircle } from "react-icons/io5"; import { RiFilter2Line } from "react-icons/ri"; import { funCreatePosition } from "../lib/api_position"; export default function DrawerListPosition({ onCreated }: { onCreated: (val: boolean) => void }) { const [openDrawerGroup, setOpenDrawerGroup] = useState(false) const router = useRouter() const [listGroup, setListGorup] = useState([]) const [listData, setListData] = useState({ name: "", idGroup: "", }) async function getAllGroup() { try { const response = await funGetAllGroup('?active=true') if (response.success) { setListGorup(response.data); } else { toast.error(response.message); } } catch (error) { console.error(error) toast.error("Gagal mendapatkan grup, coba lagi nanti"); } } useShallowEffect(() => { getAllGroup() }, []) async function onSubmit() { try { // const res = await fetch(API_ADDRESS.apiCreatePosition, { // method: 'POST', // headers: { // 'Content-Type': 'application/json' // }, // body: JSON.stringify({ // name: listData.name, // idGroup: listData.idGroup // }) // }) // if (!res.ok) { // const errorData = await res.json(); // if (errorData.message === "Position sudah ada") { // toast.error('Gagal! Position sudah ada'); // } else { // toast.error('Error'); // } // } else { // setOpenDrawerGroup(false) // toast.success('Sukses! data tersimpan') // } // onCreated(true) const res = await funCreatePosition({ name: listData.name, idGroup: listData.idGroup }) if (res.success) { setOpenDrawerGroup(false) toast.success(res.message) onCreated(true) } else { toast.error(res.message) } } catch (error) { toast.error('Error') } } return ( setOpenDrawerGroup(true)} > Tambah Jabatan router.push('/position?page=filter')}> Filter setOpenDrawerGroup(false)} title={'Tambah Jabatan'} size="lg">