import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'; import cityApi from '@/lib/address/api/cityApi'; import stateApi from '@/lib/address/api/stateApi.js'; import { yupResolver } from '@hookform/resolvers/yup'; import React, { useEffect, useRef, useState } from 'react'; import ReCAPTCHA from 'react-google-recaptcha'; import { Controller, useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; import * as Yup from 'yup'; import createLeadApi from '../api/createLeadApi'; import PageContent from '@/lib/content/components/PageContent'; import { useRouter } from 'next/router'; import useAuth from '@/core/hooks/useAuth'; const CreateMerchant = () => { const { register, handleSubmit, formState: { errors }, control, reset, watch, setValue, } = useForm({ resolver: yupResolver(validationSchema), defaultValues, }); const list_unit = [ { value: 'Manufacturing', label: 'Manufacturing', }, { value: 'Hospitality', label: 'Hospitality', }, { value: 'Automotive', label: 'Automotive', }, { value: 'Retail', label: 'Retail', }, { value: 'Maining', label: 'Maining', }, { value: 'Lain - Lain', label: 'Lain - Lain', }, ]; const [cities, setCities] = useState([]); const [state, setState] = useState([]); const [company_unit, setCompany_unit] = useState(list_unit); const recaptchaRef = useRef(null); const router = useRouter(); const auth = useAuth(); if (auth == false) { router.push(`/login?next=${encodeURIComponent('/daftar-merchant')}`); } useEffect(() => { const loadState = async () => { let dataState = await stateApi({ tempo: false }); dataState = dataState.map((state) => ({ value: state.id, label: state.name, })); setState(dataState); }; loadState(); }, []); const watchState = watch('state'); useEffect(() => { if (auth == false) { return; } const loadCities = async () => { setValue('city', ''); let dataCities = await cityApi({ stateId: watchState }); dataCities = dataCities?.map((city) => ({ value: city.id, label: city.name, })); setCities(dataCities); }; loadCities(); }, [auth, watchState, setValue]); const onSubmitHandler = async (values) => { const recaptchaValue = recaptchaRef.current.getValue(); if (!recaptchaValue) { toast.error('Catcha harus diisi'); return; } const data = { ...values, name: 'Form Merchant - ' + values.company, contact_name: values.cp, email_from: values.email, phone: values.phone, description: 'Nama Perusahaan : ' + values.company + ' \r\n Alamat : ' + values.address + ' \r\n Kota : ' + values.city + ' \r\n Unit Perusahaan : ' + values.company_unit + ' \r\n Telepon: ' + values.phone + ' \r\n Email : ' + values.email + ' \r\n Website : ' + values.website + ' \r\n No Hp : ' + values.mobile + 'Keterangan : ' + values.description, }; const create_leads = await createLeadApi({ data }); if (create_leads) { toast.success('Berhasil menambahkan data'); reset(); recaptchaRef.current.reset(); } }; if (!auth) { return; } return (

Form Merchant

Penjualan online adalah hal yang HARUS dilakukan mulai sekarang. Perubahan dalam banyak industri dan pola pembelian. Gabung dengan platform kami dan mulai penjualan langsung di ribuan perusahaan di seluruh Indonesia.{' '}

{errors.company?.message}
{errors.address?.message}
{errors.phone?.message}
( )} />
{errors.state?.message}
( )} />
{errors.city?.message}
( )} />
{errors.company_unit?.message}
{errors.website?.message}
{errors.cp?.message}
{errors.mobile?.message}
{errors.email?.message}
); }; const validationSchema = Yup.object().shape({ company: Yup.string().required('Harus di-isi'), email: Yup.string() .email('Format harus seperti contoh@email.com') .required('Harus di-isi'), phone: Yup.string().required('Harus di-isi'), cp: Yup.string().required('Harus di-isi'), state: Yup.string().required('Harus dipilih'), city: Yup.string().required('Harus di-isi'), company_unit: Yup.string().required('Harus di-isi'), address: Yup.string().required('Harus di-isi'), website: Yup.string().required('Harus di-isi'), mobile: Yup.string().required('Harus di-isi'), }); const defaultValues = { company: '', email: '', phone: '', state: '', city: '', company_unit: '', cp: '', address: '', website: '', mobile: '', }; export default CreateMerchant;