"use client"; import { LayoutNavbarNew, TEMA } from "@/module/_global"; import LayoutModal from "@/module/_global/layout/layout_modal"; import { useHookstate } from "@hookstate/core"; import { Avatar, Box, Button, Indicator, rem, Select, Skeleton, Stack, Text, TextInput } from "@mantine/core"; import { Dropzone } from "@mantine/dropzone"; import { useShallowEffect } from "@mantine/hooks"; import _ from "lodash"; import { useRouter } from "next/navigation"; import { useRef, useState } from "react"; import toast from "react-hot-toast"; import { FaCamera } from "react-icons/fa6"; import { funEditProfileByCookies, funGetProfileByCookies, } from "../lib/api_profile"; import { IEditDataProfile, IProfileById } from "../lib/type_profile"; import { IDataPositionMember } from "../../member/lib/type_member"; import { funGetAllPosition } from "@/module/position/lib/api_position"; export default function EditProfile() { const [isValModal, setValModal] = useState(false); const [isDataEdit, setDataEdit] = useState([]); const [listPosition, setListPosition] = useState([]) const openRef = useRef<() => void>(null); const [img, setIMG] = useState(); const [imgForm, setImgForm] = useState(); const router = useRouter(); const [loading, setLoading] = useState(true); const tema = useHookstate(TEMA); const [loadingKonfirmasi, setLoadingKonfirmasi] = useState(false); const [loadingPosition, setLoadingPosition] = useState(true) const [touched, setTouched] = useState({ nik: false, name: false, phone: false, email: false, gender: false, idPosition: false }); const [data, setData] = useState({ id: "", nik: "", name: "", phone: "", email: "", gender: "", img: "", idPosition: "" }); async function getAllPosition(val: any) { try { setLoadingPosition(true) const res = await funGetAllPosition( "?active=true" + "&group=" + `${val}` ); setListPosition(res.data); } catch (error) { console.error(error) } finally { setLoadingPosition(false) } } async function getAllProfile() { try { setLoading(true); const res = await funGetProfileByCookies(); setData(res.data); getAllPosition(res.data.idGroup); setIMG(`https://wibu-storage.wibudev.com/api/files/${res.data.img}`); setLoading(false); } catch (error) { console.error(error); } finally { setLoading(false); } } useShallowEffect(() => { getAllProfile(); }, []); async function onEditProfile(val: boolean) { try { if (val) { setLoadingKonfirmasi(true) const fd = new FormData(); fd.append("file", imgForm); fd.append("data", JSON.stringify(data)); const res = await funEditProfileByCookies(fd); if (res.success) { setValModal(false); toast.success(res.message); router.push("/profile"); } else { toast.error(res.message); } } setValModal(false); } catch (error) { console.error(error); toast.error("Gagal edit profil, coba lagi nanti"); } finally { setLoadingKonfirmasi(false) } } function onCheck() { if (Object.values(touched).some((v) => v == true)) return false setValModal(true) } function onValidation(kategori: string, val: string) { if (kategori == 'nik') { setData({ ...data, nik: val }) if (val === "" || val.length !== 16) { setTouched({ ...touched, nik: true }) } else { setTouched({ ...touched, nik: false }) } } else if (kategori == 'name') { setData({ ...data, name: val }) if (val === "") { setTouched({ ...touched, name: true }) } else { setTouched({ ...touched, name: false }) } } else if (kategori == 'phone') { setData({ ...data, phone: val }) if (val == "" || !(val.length >= 9 && val.length <= 15)) { setTouched({ ...touched, phone: true }) } else { setTouched({ ...touched, phone: false }) } } else if (kategori == 'email') { setData({ ...data, email: val }) if (val == "" || !/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(val)) { setTouched({ ...touched, email: true }) } else { setTouched({ ...touched, email: false }) } } else if (kategori == 'gender') { setData({ ...data, gender: val }) if (val == "" || String(val) == "null") { setTouched({ ...touched, gender: true }) } else { setTouched({ ...touched, gender: false }) } } else if (kategori == 'idPosition') { setData({ ...data, idPosition: val }) if (val == "" || String(val) == "null") { setTouched({ ...touched, idPosition: true }) } else { setTouched({ ...touched, idPosition: false }) } } } 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 ? [...Array(6)].map((_, index) => ( )) : ( <> { onValidation('gender', val) }} value={data.gender} error={ touched.gender && (data.gender == "" || data.gender == null ? "Jenis Kelamin Tidak Boleh Kosong" : null) } /> )} {loading ? ( ) : ( )} setValModal(false)} description="Apakah Anda yakin ingin melakukan perubahan data?" onYes={(val) => { onEditProfile(val); }} /> ); }