From b3be7e6d5c320764ef3ca38df196218abf9be25b Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 10 Dec 2024 10:05:47 +0700
Subject: <iman> move formulis & add state field

---
 src/lib/form/components/KunjunganSales.jsx | 215 ++++++++++++++++++++---------
 1 file changed, 147 insertions(+), 68 deletions(-)

(limited to 'src/lib/form/components/KunjunganSales.jsx')

diff --git a/src/lib/form/components/KunjunganSales.jsx b/src/lib/form/components/KunjunganSales.jsx
index ffa8f135..3779b836 100644
--- a/src/lib/form/components/KunjunganSales.jsx
+++ b/src/lib/form/components/KunjunganSales.jsx
@@ -1,17 +1,18 @@
-import odooApi from '@/core/api/odooApi'
-import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'
-import cityApi from '@/lib/address/api/cityApi'
-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 useAuth from '@/core/hooks/useAuth'
-import { useRouter } from 'next/router'
+import odooApi from '@/core/api/odooApi';
+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 useAuth from '@/core/hooks/useAuth';
+import { useRouter } from 'next/router';
 
 const KunjunganSales = () => {
   const {
@@ -19,44 +20,72 @@ const KunjunganSales = () => {
     handleSubmit,
     formState: { errors },
     control,
-    reset
+    reset,
+    watch,
+    setValue,
   } = useForm({
     resolver: yupResolver(validationSchema),
-    defaultValues
-  })
-  const [cities, setCities] = useState([])
-  const [companyTypes, setCompanyTypes] = useState([])
-  const router = useRouter()
+    defaultValues,
+  });
+  const [cities, setCities] = useState([]);
+  const [state, setState] = useState([]);
+  const [companyTypes, setCompanyTypes] = useState([]);
+  const router = useRouter();
+
+  const auth = useAuth();
 
-  const auth = useAuth()
+  const recaptchaRef = useRef(null);
 
-  
+  if (auth == false) {
+    router.push(`/login?next=${encodeURIComponent('/kunjungan-sales')}`);
+  }
+
+  useEffect(() => {
+    const loadState = async () => {
+      let dataState = await stateApi();
+      dataState = dataState.map((state) => ({
+        value: state.id,
+        label: state.name,
+      }));
+      setState(dataState);
+    };
+    loadState();
+  }, []);
 
-  const recaptchaRef = useRef(null)
+  const watchState = watch('state');
 
   useEffect(() => {
-    if(auth == false) {
-      router.push('/login')
+    if (auth == false) {
+      return;
     }
     const loadCities = async () => {
-      let dataCities = await cityApi()
-      dataCities = dataCities.map((obj) => ({ value: obj.name, label: obj.name }))
-      setCities(dataCities)
-    }
+      setValue('city', '');
+      let dataCities = await cityApi({ stateId: watchState });
+      dataCities = dataCities?.map((obj) => ({
+        value: obj.name,
+        label: obj.name,
+      }));
+      setCities(dataCities);
+    };
     const loadCompanyTypes = async () => {
-      const dataCompanyTypes = await odooApi('GET', '/api/v1/partner/company_type')
-      setCompanyTypes(dataCompanyTypes?.map((obj) => ({ value: obj.name, label: obj.name })))
-    }
+      const dataCompanyTypes = await odooApi(
+        'GET',
+        '/api/v1/partner/company_type'
+      );
+      setCompanyTypes(
+        dataCompanyTypes?.map((obj) => ({ value: obj.name, label: obj.name }))
+      );
+    };
 
-    loadCompanyTypes()
-    loadCities()
-  }, [auth])
+    loadCompanyTypes();
+    loadCities();
+  }, [auth, watchState, setValue]);
 
   const onSubmitHandler = async (values) => {
-    const recaptchaValue = recaptchaRef.current.getValue()
+    const recaptchaValue = recaptchaRef.current.getValue();
     if (!recaptchaValue) {
-      toast.error('Recaptcha harus diisi')
-      return
+      toast.error('Recaptcha harus diisi');
+      return;
     }
 
     const data = {
@@ -71,29 +100,37 @@ const KunjunganSales = () => {
         `Unit Perusahaan: ${values.companyType}`,
         `No. Handphone: ${values.mobile}`,
         `Alamat Email: ${values.email}`,
-        `Keterangan: ${values.description}`
-      ].join('\n')
-    }
+        `Keterangan: ${values.description}`,
+      ].join('\n'),
+    };
 
-    const createLead = await createLeadApi({ data })
+    const createLead = await createLeadApi({ data });
     if (createLead) {
-      toast.success('Berhasil mengirimkan formulir kunjungan sales')
-      reset()
-      recaptchaRef.current.reset()
+      toast.success('Berhasil mengirimkan formulir kunjungan sales');
+      reset();
+      recaptchaRef.current.reset();
     }
+  };
+  if (!auth) {
+    return;
   }
   return (
     <div className='container mx-auto p-4 md:p-0 my-0 md:my-10'>
-      <h1 className='text-h-sm md:text-title-sm font-semibold mb-6'>Kunjungan Sales</h1>
+      <h1 className='text-h-sm md:text-title-sm font-semibold mb-6'>
+        Kunjungan Sales
+      </h1>
 
       <div className='w-full grid grid-cols-1 md:grid-cols-2 gap-x-2'>
-        <form onSubmit={handleSubmit(onSubmitHandler)} className='grid grid-cols-1 gap-y-6'>
+        <form
+          onSubmit={handleSubmit(onSubmitHandler)}
+          className='grid grid-cols-1 gap-y-6'
+        >
           <div
             className='flex items-center bg-blue-100 border border-blue-300 text-blue-500 font-medium px-4 py-3 rounded leading-6'
             role='alert'
           >
-            Hubungi kami untuk mendapatkan kunjungan sales kami dan dapatkan berbagai kelebihannya
-            dengan menjadi pelanggan korporat kami.
+            Hubungi kami untuk mendapatkan kunjungan sales kami dan dapatkan
+            berbagai kelebihannya dengan menjadi pelanggan korporat kami.
           </div>
           <div>
             <label className='form-label mb-2'>Nama Perusahan*</label>
@@ -104,7 +141,9 @@ const KunjunganSales = () => {
               className='form-input'
               aria-invalid={errors.company?.message}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.company?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.company?.message}
+            </div>
           </div>
 
           <div>
@@ -116,7 +155,9 @@ const KunjunganSales = () => {
               className='form-input'
               aria-invalid={errors.phone?.message}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.phone?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.phone?.message}
+            </div>
           </div>
 
           <div>
@@ -128,7 +169,21 @@ const KunjunganSales = () => {
               className='form-input'
               aria-invalid={errors.address?.message}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.address?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.address?.message}
+            </div>
+          </div>
+
+          <div>
+            <label className='form-label mb-2'>Provinsi*</label>
+            <Controller
+              name='state'
+              control={control}
+              render={(props) => <HookFormSelect {...props} options={state} />}
+            />
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.stateId?.message}
+            </div>
           </div>
 
           <div>
@@ -138,7 +193,9 @@ const KunjunganSales = () => {
               control={control}
               render={(props) => <HookFormSelect {...props} options={cities} />}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.city?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.city?.message}
+            </div>
           </div>
 
           <div>
@@ -146,9 +203,13 @@ const KunjunganSales = () => {
             <Controller
               name='companyType'
               control={control}
-              render={(props) => <HookFormSelect {...props} options={companyTypes} />}
+              render={(props) => (
+                <HookFormSelect {...props} options={companyTypes} />
+              )}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.companyType?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.companyType?.message}
+            </div>
           </div>
 
           <div>
@@ -160,7 +221,9 @@ const KunjunganSales = () => {
               className='form-input'
               aria-invalid={errors.mobile?.message}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.mobile?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.mobile?.message}
+            </div>
           </div>
 
           <div>
@@ -172,34 +235,49 @@ const KunjunganSales = () => {
               className='form-input'
               aria-invalid={errors.email?.message}
             />
-            <div className='text-caption-2 text-danger-500 mt-1'>{errors.email?.message}</div>
+            <div className='text-caption-2 text-danger-500 mt-1'>
+              {errors.email?.message}
+            </div>
           </div>
 
           <div>
             <label className='form-label mb-2'>Keterangan</label>
-            <textarea {...register('description')} type='text' className='form-input' />
+            <textarea
+              {...register('description')}
+              type='text'
+              className='form-input'
+            />
           </div>
 
-          <ReCAPTCHA ref={recaptchaRef} sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_GOOGLE} />
+          <ReCAPTCHA
+            ref={recaptchaRef}
+            sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_GOOGLE}
+          />
 
-          <button type='submit' className='btn-yellow w-full md:w-fit ml-0 md:ml-auto'>
+          <button
+            type='submit'
+            className='btn-yellow w-full md:w-fit ml-0 md:ml-auto'
+          >
             Simpan
           </button>
         </form>
         <PageContent path='/kunjungan-sales' />
       </div>
     </div>
-  )
-}
+  );
+};
 
 const validationSchema = Yup.object().shape({
-  email: Yup.string().email('Format harus seperti contoh@email.com').required('Harus di-isi'),
+  email: Yup.string()
+    .email('Format harus seperti contoh@email.com')
+    .required('Harus di-isi'),
   company: Yup.string().required('Harus di-isi'),
   phone: Yup.string().required('Harus di-isi'),
   mobile: Yup.string().required('Harus di-isi'),
   address: Yup.string().required('Harus di-isi'),
-  city: Yup.string().required('Harus dipilih')
-})
+  city: Yup.string().required('Harus dipilih'),
+  state: Yup.string().required('Harus dipilih'),
+});
 
 const defaultValues = {
   email: '',
@@ -208,8 +286,9 @@ const defaultValues = {
   mobile: '',
   address: '',
   city: '',
+  state: '',
   companyType: '',
-  description: ''
-}
+  description: '',
+};
 
-export default KunjunganSales
+export default KunjunganSales;
-- 
cgit v1.2.3


From 9c4951174cf6cf5c15b0b2a808d5d14befac6169 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 15 Jan 2025 13:40:59 +0700
Subject: <iman> update code filter bad words registrasi

---
 src/lib/form/components/KunjunganSales.jsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src/lib/form/components/KunjunganSales.jsx')

diff --git a/src/lib/form/components/KunjunganSales.jsx b/src/lib/form/components/KunjunganSales.jsx
index 3779b836..0f63de03 100644
--- a/src/lib/form/components/KunjunganSales.jsx
+++ b/src/lib/form/components/KunjunganSales.jsx
@@ -42,7 +42,7 @@ const KunjunganSales = () => {
 
   useEffect(() => {
     const loadState = async () => {
-      let dataState = await stateApi();
+      let dataState = await stateApi({ tempo: false });
       dataState = dataState.map((state) => ({
         value: state.id,
         label: state.name,
-- 
cgit v1.2.3

