'use client' import { WARNA } from "@/module/_global"; import LayoutModal from "@/module/_global/layout/layout_modal"; import { funGetAllGroup, IDataGroup } from "@/module/group"; import { funGetAllPosition } from "@/module/position/lib/api_position"; import { Avatar, Box, Button, Indicator, rem, Select, Skeleton, Stack, Text, TextInput } from "@mantine/core"; import { useShallowEffect } from "@mantine/hooks"; import { useRouter } from "next/navigation"; import { useRef, useState } from "react"; import toast from "react-hot-toast"; import { HiUser } from "react-icons/hi2"; import { IDataPositionMember, IDataROleMember, IEditDataMember, IFormMember } from "../lib/type_member"; import { funEditMember, funGetOneMember, funGetRoleUser } from "../lib/api_member"; import _ from "lodash"; import { Dropzone } from "@mantine/dropzone"; import { FaCamera } from "react-icons/fa6"; export default function EditMember({ id }: { id: string }) { const [isModal, setModal] = useState(false) const router = useRouter() const [listGroup, setListGorup] = useState([]) const [listPosition, setListPosition] = useState([]) const [listUserRole, setListUserRole] = useState([]) const [imgForm, setImgForm] = useState() const openRef = useRef<() => void>(null) const [img, setIMG] = useState() const [loading, setLoading] = useState(true) const [touched, setTouched] = useState({ nik: false, name: false, phone: false, email: false, gender: false, idGroup: false, idPosition: false, idUserRole: false }); const [data, setData] = useState({ id: "", nik: "", name: "", phone: "", email: "", gender: "", idGroup: "", idPosition: "", idUserRole: "", }) 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"); } } async function getOneData() { try { setLoading(true) const res = await funGetOneMember(id) setData(res.data) getAllPosition(res.data?.idGroup) setIMG(`/api/file/img?cat=user&file=${res.data.img}`) setLoading(false) } catch (error) { console.error(error) } finally { setLoading(false) } } async function getAllPosition(val: any) { try { const res = await funGetAllPosition( "?active=true" + "&group=" + `${val}` ); setListPosition(res.data); } catch (error) { console.error(error) } } async function getAllUserRole() { try { const res = await funGetRoleUser(); setListUserRole(res.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 { if (_.isEmpty(data)) { return } if (val) { const fd = new FormData() fd.append("file", imgForm) fd.append("data", JSON.stringify(data)) const res = await funEditMember(id, fd) if (res.success) { toast.success(res.message) router.push(`/member?active=true`) } else { toast.error(res.message) } } } catch (error) { toast.error('Error'); } } return ( { if (!files || _.isEmpty(files)) return toast.error('Tidak ada gambar yang dipilih') setImgForm(files[0]) const buffer = URL.createObjectURL(new Blob([new Uint8Array(await files[0].arrayBuffer())])) setIMG(buffer) }} activateOnClick={false} maxSize={1 * 1024 ** 2} accept={['image/png', 'image/jpeg', 'image/heic']} onReject={(files) => { return toast.error('File yang diizinkan: .png, .jpg, dan .heic dengan ukuran maksimal 1 MB') }} > {loading ? : } size={40} onClick={() => openRef.current?.()}> } {loading ? <> : <> ({ value: data.id, label: data.name, })) : [] } onChange={(val: any) => { setData({ ...data, idPosition: val }) setTouched({ ...touched, idPosition: false }) }} value={(data?.idPosition == "") ? null : data.idPosition} onBlur={() => setTouched({ ...touched, idPosition: true })} error={ touched.idPosition && ( data.idPosition == "" ? "Jabatan Tidak Boleh Kosong" : null ) } /> { setData({ ...data, gender: val }) setTouched({ ...touched, gender: false }) }} value={data.gender} onBlur={() => setTouched({ ...touched, gender: true })} error={ touched.gender && ( data.gender == "" ? "Gender Tidak Boleh Kosong" : null ) } /> } {loading ? : } setModal(false)} description="Apakah Anda yakin ingin mengubah data?" onYes={(val) => { if (val) onSubmit(val) setModal(false) } } /> ) }