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 { 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"; interface FormProps { type: string; required: boolean; isPKP: boolean; } interface Industry { label: string; value: string; category: string; } interface company_type_id { value: string; label: string; } const FormBisnis: React.FC = ({ type, required, isPKP }) => { const { formBisnis, isCheckedTNC, isValidCaptcha, errors, updateFormBisnis, validateFormBisnis, } = useRegisterStore() console.log("errors bisnis",errors) const { control, watch, setValue } = useForm(); const [selectedCategory, setSelectedCategory] = useState(''); const [selectedCompanyId, setSelectedCompanyId] = useState(''); const [industries, setIndustries] = useState([]); const [companyTypes, setCompanyTypes] = useState([]); const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); 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('company_type_id')); if (selectedCompanyType) { updateFormBisnis("company_type_id", selectedCompanyType?.value); setSelectedCompanyId(selectedCompanyType?.label) validateFormBisnis(); } }, [watch('company_type_id'), companyTypes]); useEffect(() => { const selectedIndustryType = industries.find(industry => industry.value === watch('industry')); if (selectedIndustryType) { updateFormBisnis("industry_id", selectedIndustryType?.value); setSelectedCategory(selectedIndustryType.label); validateFormBisnis(); } }, [watch('industry'), 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; updateFormBisnis(name, value); updateFormBisnis('name','iman'); updateFormBisnis('email','it@fixcomart.co.id'); updateFormBisnis('password','Fixcomart378'); updateFormBisnis('phone','082339129611'); validateFormBisnis(); }; const handleFileChange = async (event: ChangeEvent) => { let fileBase64 = ''; const { name} = event.target; const file = event.target.files?.[0]; if (file) { if (typeof file !== 'undefined') { if (file.size > 5000000) { // toast.error('Maksimal ukuran file adalah 5MB') return; } fileBase64 = await getFileBase64(file); } updateFormBisnis(name, fileBase64); validateFormBisnis(); } }; const mutation = useMutation({ mutationFn: (data: RegisterProps) => registerUser(data) }); const handleSubmit = async (e: ChangeEvent) => { e.preventDefault(); const response = await mutation.mutateAsync(formBisnis); if (response?.register === true) { const urlParams = new URLSearchParams({ activation: 'otp', email: formBisnis.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 (
{!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 && {errors.industry}}
{isPKP && !required && !!errors.nama_wajib_pajak && {errors.nama_wajib_pajak}}
{isPKP && !required && !!errors.npwp && {errors.npwp}}
{isPKP && !required && !!errors.npwp_document && {errors.npwp_document}}
{isPKP && !required && !!errors.sppkp_document && {errors.sppkp_document}}
{/* */}
) } export default FormBisnis;