import { ChangeEvent, useEffect, useMemo, useState } from "react"; import { useMutation } from "react-query"; import { useRegisterStore } from "../stores/useRegisterStore"; import { RegisterProps } from "~/types/auth"; import { registerUser } from "~/services/auth"; import { useRouter } from "next/router"; import { Button, Checkbox, UseToastOptions, color, useToast } from "@chakra-ui/react"; import Link from "next/link"; import getFileBase64 from '@/core/utils/getFileBase64' import { Controller, useForm } from 'react-hook-form' import HookFormSelect from '@/core/components/elements/Select/HookFormSelect' import odooApi from "~/libs/odooApi"; import { toast } from 'react-hot-toast'; import { EyeIcon } from '@heroicons/react/24/outline'; import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; import Image from 'next/image' import useDevice from '@/core/hooks/useDevice' interface FormProps { type: string; required: boolean; isPKP: boolean; } interface industry_id { label: string; value: string; category: string; } interface companyType { value: string; label: string; } const form: React.FC = ({ type, required, isPKP }) => { const { form, errors, updateForm, validate } = useRegisterStore() const { control, watch, setValue } = useForm(); const [selectedCategory, setSelectedCategory] = useState(''); const [isChekBox, setIsChekBox] = useState(false); const [isExample, setIsExample] = useState(false); const { isDesktop, isMobile } = useDevice() // Inside your component const [formattedNpwp, setFormattedNpwp] = useState(""); // State for formatted NPWP const [unformattedNpwp, setUnformattedNpwp] = useState(""); // State for unformatted NPWP const [industries, setIndustries] = useState([]); const [companyTypes, setCompanyTypes] = useState([]); const router = useRouter(); const toast = useToast(); useEffect(() => { const loadCompanyTypes = async () => { const dataCompanyTypes = await odooApi('GET', '/api/v1/partner/company_type'); setCompanyTypes(dataCompanyTypes?.map((o: { id: any; name: any; }) => ({ value: o.id, label: o.name }))); }; loadCompanyTypes(); }, []); useEffect(() => { const selectedCompanyType = companyTypes.find(company => company.value === watch('companyType')); if (selectedCompanyType) { updateForm("company_type_id", `${selectedCompanyType?.value}`); validate(); } }, [watch('companyType'), companyTypes]); useEffect(() => { const selectedIndustryType = industries.find(industry => industry.value === watch('industry_id')); if (selectedIndustryType) { updateForm("industry_id", `${selectedIndustryType?.value}`); setSelectedCategory(selectedIndustryType.category); validate(); } }, [watch('industry_id'), industries]); useEffect(() => { const loadIndustries = async () => { const dataIndustries = await odooApi('GET', '/api/v1/partner/industry'); setIndustries(dataIndustries?.map((o: { id: any; name: any; category: any; }) => ({ value: o.id, label: o.name, category: o.category }))); }; loadIndustries(); }, []); const handleInputChange = (event: ChangeEvent) => { const { name, value } = event.target; updateForm('type_acc',`business`) updateForm('is_pkp',`${isPKP}`) updateForm(name, value); validate(); }; const handleInputChangeNpwp = (event: ChangeEvent) => { const { name, value } = event.target; updateForm('type_acc',`business`) updateForm('is_pkp',`${isPKP}`) updateForm('npwp', value); validate(); }; const handleChange = (event: ChangeEvent) => { setIsChekBox(!isChekBox) }; const formatNpwp = (value: string) => { try { const cleaned = ("" + value).replace(/\D/g, ""); let match if(cleaned.length <= 15){ match = cleaned.match(/(\d{0,2})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/); }else{ match = cleaned.match(/(\d{0,3})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/); } if (match) { return [ match[1], match[2] ? "." : "", match[2], match[3] ? "." : "", match[3], match[4] ? "." : "", match[4], match[5] ? "-" : "", match[5], match[6] ? "." : "", match[6], ].join(""); } // If match is null, return the original cleaned string or handle as needed return cleaned; } catch (error) { // Handle error or return a default value console.error("Error formatting NPWP:", error); return value; } }; useEffect(() => { if (isChekBox) { updateForm("isChekBox", 'true'); validate(); } else { updateForm("isChekBox", 'false'); validate(); } }, [isChekBox,]); const handleFileChange = async (event: ChangeEvent) => { const toastProps: UseToastOptions = { duration: 5000, isClosable: true }; let fileBase64 = ''; const { name} = event.target; const file = event.target.files?.[0]; if (file) { if (typeof file !== 'undefined') { if (file.size > 5000000) { toast({ ...toastProps, title: 'Maksimal ukuran file adalah 5MB', status: 'warning' }); return; } fileBase64 = await getFileBase64(file); } updateForm(name, fileBase64); validate(); } }; const mutation = useMutation({ mutationFn: (data: RegisterProps) => registerUser(data) }); const handleSubmit = async (e: ChangeEvent) => { e.preventDefault(); const response = await mutation.mutateAsync(form); if (response?.register === true) { const urlParams = new URLSearchParams({ activation: 'otp', email: form.email, redirect: (router.query?.next || '/') as string }); router.push(`${router.route}?${urlParams}`); } const toastProps: UseToastOptions = { duration: 5000, isClosable: true }; switch (response?.reason) { case 'EMAIL_USED': toast({ ...toastProps, title: 'Email sudah digunakan', status: 'warning' }); break; case 'NOT_ACTIVE': const activationUrl = `${router.route}?activation=email`; toast({ ...toastProps, title: 'Akun belum aktif', description: <>Akun sudah terdaftar namun belum aktif. Klik untuk aktivasi akun, status: 'warning' }); break; } }; return ( <> setIsExample(false)} >
Contoh SPPKP
{!required && isPKP && !!errors.email_partner && {errors.email_partner}}
} /> {!required && !!errors.company_type_id && {errors.company_type_id}}
{ !!errors.business_name && {errors.business_name}}
} /> {selectedCategory && Kategori : {selectedCategory} } {!required && !!errors.industry_id && {errors.industry_id}}
{!required && !!errors.alamat_bisnis && {errors.alamat_bisnis}}
{isPKP && !required && !!errors.nama_wajib_pajak && {errors.nama_wajib_pajak}}
{isPKP && !required && !!errors.alamat_wajib_pajak && {errors.alamat_wajib_pajak}}
{ if (!required) { const unformatted = e.target.value.replace(/\D/g, ""); // Remove all non-digit characters const formattedValue = formatNpwp(unformatted); // Format the value setUnformattedNpwp(unformatted); // Store unformatted value setFormattedNpwp(formattedValue); // Store formatted value handleInputChangeNpwp({ ...e, target: { ...e.target, value: unformatted } }); // Update form state with unformatted value } }} aria-invalid={!required && !!errors.npwp} /> {!required && !!errors.npwp && {errors.npwp}}
{!required && !!errors.sppkp && {errors.sppkp}}
{isPKP && !required && !!errors.npwp_document && {errors.npwp_document}}
{isPKP && !required && !!errors.sppkp_document && {errors.sppkp_document}}
) } export default form;