From 2d4e0cea97f605a265ac41cbaf61ca23e4b828c5 Mon Sep 17 00:00:00 2001 From: lukman Date: Tue, 6 Aug 2024 10:28:25 +0800 Subject: [PATCH] feat : update member --- .../user/member/component/edit_member.tsx | 218 +++++++++++++++++- .../component/ui/drawer_detail_member.tsx | 38 ++- .../component/ui/navbar_detail_member.tsx | 121 ++++++++-- .../member/component/ui/tab_list_member.tsx | 1 + .../user/member/view/view_detail_member.tsx | 32 +-- .../user/member/view/view_edit_member.tsx | 5 +- 6 files changed, 335 insertions(+), 80 deletions(-) diff --git a/src/module/user/member/component/edit_member.tsx b/src/module/user/member/component/edit_member.tsx index bd583d6..f899959 100644 --- a/src/module/user/member/component/edit_member.tsx +++ b/src/module/user/member/component/edit_member.tsx @@ -1,20 +1,150 @@ 'use client' -import { WARNA } from "@/module/_global"; +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 { useShallowEffect } from "@mantine/hooks"; +import { useRouter } from "next/navigation"; import { useState } from "react"; import toast from "react-hot-toast"; import { HiUser } from "react-icons/hi2"; -export default function EditMember() { - const [isModal, setModal] = useState(false) +type dataMember = { + id: string; + nik: string; + name: string; + phone: string; + email: string; + gender: string; + idGroup: string; + idPosition: string; + idUserRole: string; +} - function onTrue(val: boolean) { - if (val) { - toast.success("Sukses! Data tersimpan"); +type dataGroup = { + id: string; + name: string; +}; +type dataPosition = { + id: string; + name: string; +}; + +type dataROleUser = { + id: string; + name: string; +} + +export default function EditMember({ id }: { id: string | undefined }) { + const [isModal, setModal] = useState(false) + const router = useRouter() + const [listGroup, setListGorup] = useState([]) + const [listPosition, setListPosition] = useState([]) + const [listUserRole, setListUserRole] = useState([]) + const [data, setData] = useState({ + id: "", + nik: "", + name: "", + phone: "", + email: "", + gender: "", + idGroup: "", + idPosition: "", + idUserRole: "", + }) + const [listData, setListData] = useState() + + 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) } - setModal(false) } + + async function getOneData() { + try { + const res = await fetch(`${API_ADDRESS.apiGetOneUser}&userID=${id}`) + const data = await res.json() + setData(data) + getAllPosition(data?.idGroup) + } 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) { + setListPosition([]) + setData({ + ...data, + idGroup: val, + idPosition: "" + }) + getAllPosition(val) + } + + useShallowEffect(() => { + getAllGroup() + getOneData() + getAllUserRole() + }, []) + + + async function onSubmit(val: boolean) { + try { + const res = await fetch(API_ADDRESS.apiUpdateUser, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + id: data.id, + nik: data.nik, + name: data.name, + phone: data.phone, + email: data.email, + gender: data.gender, + idGroup: data.idGroup, + idPosition: data.idPosition, + idUserRole: data.idUserRole + }), + }) + + const respon = await res.json() + + if (res.status == 200) { + toast.success(respon.message) + } else { + toast.error(respon.message) + } + router.push('/member') + } catch (error) { + toast.error('Error'); + } + } + return ( ({ + value: data.id, + label: data.name, + })) + : [] + } + onChange={(val: any) => { + changeGrup(val) + }} + value={data?.idGroup} /> ({ + value: data.id, + label: data.name, + })) + : [] + } + onChange={(val: any) => setData({ ...data, idUserRole: val })} + value={data?.idUserRole} /> setData({ ...data, nik: e.target.value })} + value={data.nik} /> setData({ ...data, name: e.target.value })} + value={data.name} /> setData({ ...data, email: e.target.value })} + value={data.email} /> setData({ ...data, phone: e.target.value })} + value={data.phone} />