'use client' import { API_ADDRESS, WARNA } from "@/module/_global"; import LayoutModal from "@/module/_global/layout/layout_modal"; import { Box, Button, Select, Stack, TextInput } from "@mantine/core"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { HiUser } from "react-icons/hi2"; type dataGroup = { id: string; name: string; }; type dataPosition = { id: string; name: string; }; type dataROleUser = { id: string; name: string; } export default function CreateMember() { const router = useRouter() const [isModal, setModal] = useState(false) const [listGroup, setListGorup] = useState([]) const [listPosition, setListPosition] = useState([]) const [listUserRole, setListUserRole] = useState([]) const [listData, setListData] = useState({ nik: "", name: "", phone: "", email: "", gender: "", idGroup: "", idPosition: "", idUserRole: "", }) async function getAllGroup() { try { const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=desa1&active=true`) const data = await res.json() setListGorup(data) } catch (error) { console.error(error) } } async function getAllPosition(val: any) { try { const res = await fetch(`${API_ADDRESS.apiGetAllPosition}&groupId=${val}&active=true`) const data = await res.json() setListPosition(data) } catch (error) { console.error(error) } } async function getAllUserRole() { try { const res = await fetch(`${API_ADDRESS.apiGetRoleUser}`) const data = await res.json() setListUserRole(data) } catch (error) { console.error(error) } } async function changeGrup(val: any) { console.log(val) setListPosition([]) setListData({ ...listData, idGroup: val, idPosition: "" }) getAllPosition(val) } async function onSubmit(val: boolean) { try { const res = await fetch(API_ADDRESS.apiCreateUser, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ nik: listData.nik, name: listData.name, phone: listData.phone, email: listData.email, gender: listData.gender, idGroup: listData.idGroup, idPosition: listData.idPosition, idUserRole: listData.idUserRole }) }) toast.success("Sukses! Data tersimpan"); setModal(false) router.push('/member') } catch (error) { toast.error('Error') toast.error("Sukses! Data tersimpan"); } } useEffect(() => { getAllGroup() getAllUserRole() }, []) function onTrue(val: boolean) { if (val) { toast.success("Sukses! Data tersimpan"); } setModal(false) } return ( ({ value: data.id, label: data.name, })) : [] } onChange={(val: any) => setListData({ ...listData, idPosition: val })} value={listData.idPosition} /> setListData({ ...listData, gender: val })} /> setModal(false)} description="Apakah Anda yakin ingin menambahkan data?" onYes={(val) => { onSubmit(val) }} /> ) }