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, 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; } interface Industry { label: string; value: string; category: string; } const FormBisnis: React.FC = ({ type, required }) => { const { formBisnis, isCheckedTNC, isValidCaptcha, errors, updateFormBisnis, validateFormBisnis, } = useRegisterStore() const { control, watch, setValue } = useForm(); const [selectedCategory, setSelectedCategory] = 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 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 selectedIndustry = watch('industry'); useEffect(() => { const selected = industries.find(industry => industry.value === selectedIndustry); console.log("selected",selected) if (selected) { setSelectedCategory(selected.category); } }, [selectedIndustry, industries]); const handleInputChange = (event: ChangeEvent) => { console.log("event",event) const { name, value } = event.target; updateFormBisnis(name, value); validateFormBisnis(); }; const handleFileChange = async (event: ChangeEvent) => { let fileBase64 = ''; 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("document", fileBase64); // Menyimpan file ke dalam formBisnis state 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 (
{!!errors.email && {errors.email}}
} />
} /> Kategori : {selectedCategory}
{!!errors.name && {errors.name}}
{!!errors.phone && {errors.phone}}
{/* {!!errors.document && {errors.document}} */}
{/* {!!errors.document && {errors.document}} */}
{/* */}
) } export default FormBisnis;