From b9b4eb3ed751a69c023a45e7cdca8da01ae6b1cd Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 15 Jan 2025 13:21:48 +0700 Subject: update merchant --- src/lib/form/api/createMerchantApi.js | 8 -- src/lib/merchant/api/createMerchantApi.js | 14 +++ src/lib/merchant/api/getMerchantApi.js | 13 +++ src/lib/merchant/components/Dokumen.jsx | 4 +- .../merchant/components/InformasiPerusahaan.jsx | 57 +++++++-- src/lib/merchant/components/InformasiVendor.jsx | 84 ++++++++++---- src/lib/merchant/components/Merchant.jsx | 4 +- src/lib/merchant/components/SyaratDagang.jsx | 128 ++++++++++++++++----- 8 files changed, 240 insertions(+), 72 deletions(-) delete mode 100644 src/lib/form/api/createMerchantApi.js create mode 100644 src/lib/merchant/api/createMerchantApi.js create mode 100644 src/lib/merchant/api/getMerchantApi.js (limited to 'src/lib') diff --git a/src/lib/form/api/createMerchantApi.js b/src/lib/form/api/createMerchantApi.js deleted file mode 100644 index 8fd79857..00000000 --- a/src/lib/form/api/createMerchantApi.js +++ /dev/null @@ -1,8 +0,0 @@ -import odooApi from '@/core/api/odooApi'; - -const createMerchantApi = async ({ data }) => { - const lead = await odooApi('POST', '/api/v1/merchant', data); - return lead; -}; - -export default createMerchantApi; diff --git a/src/lib/merchant/api/createMerchantApi.js b/src/lib/merchant/api/createMerchantApi.js new file mode 100644 index 00000000..4c4f96ba --- /dev/null +++ b/src/lib/merchant/api/createMerchantApi.js @@ -0,0 +1,14 @@ +import odooApi from '@/core/api/odooApi'; +import { getAuth } from '@/core/utils/auth'; + +const createMerchantApi = async ({ data }) => { + const auth = getAuth(); + const lead = await odooApi( + 'POST', + `/api/v1/merchant/${auth.partnerId}`, + data + ); + return lead; +}; + +export default createMerchantApi; diff --git a/src/lib/merchant/api/getMerchantApi.js b/src/lib/merchant/api/getMerchantApi.js new file mode 100644 index 00000000..a1a4f877 --- /dev/null +++ b/src/lib/merchant/api/getMerchantApi.js @@ -0,0 +1,13 @@ +import odooApi from '@/core/api/odooApi'; +import { getAuth } from '@/core/utils/auth'; + +const createMerchantApi = async () => { + const auth = getAuth(); + const lead = await odooApi( + 'GET', + `/api/v1/detail-merchant/${auth.partnerId}` + ); + return lead; +}; + +export default createMerchantApi; diff --git a/src/lib/merchant/components/Dokumen.jsx b/src/lib/merchant/components/Dokumen.jsx index fb4d1914..19df56a5 100644 --- a/src/lib/merchant/components/Dokumen.jsx +++ b/src/lib/merchant/components/Dokumen.jsx @@ -9,7 +9,7 @@ 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 createMerchantApi from '../../form/api/createMerchantApi'; +import createMerchantApi from '../api/createMerchantApi'; import addressApi from '@/lib/address/api/addressApi'; import PageContent from '@/lib/content/components/PageContent'; import { useRouter } from 'next/router'; @@ -320,7 +320,7 @@ const Dokumen = ({ handleIsError }) => { file_dataProduk: dataProduk ? dataProduk : '', file_pricelist: pricelist ? pricelist : '', }; - isError(true); + isError(false); toast.dismiss(toastId); toast.success('Berhasil menambahkan data'); // const formData = new FormData(); diff --git a/src/lib/merchant/components/InformasiPerusahaan.jsx b/src/lib/merchant/components/InformasiPerusahaan.jsx index 60e61e2e..a5456378 100644 --- a/src/lib/merchant/components/InformasiPerusahaan.jsx +++ b/src/lib/merchant/components/InformasiPerusahaan.jsx @@ -9,7 +9,8 @@ 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 createMerchantApi from '../../form/api/createMerchantApi'; +import createMerchantApi from '../api/createMerchantApi'; +import getMerchantApi from '../api/getMerchantApi'; import addressApi from '@/lib/address/api/addressApi'; import PageContent from '@/lib/content/components/PageContent'; import { useRouter } from 'next/router'; @@ -114,6 +115,44 @@ const CreateMerchant = ({ handleIsError }) => { { value: 5, label: 'Wholesaler (Pedagang Besar)' }, ]; + useEffect(() => { + const loadData = async () => { + try { + const data = await getMerchantApi(); + console.log('data', data); + reset({ + pejabatName: data.pejabatName ? data.pejabatName : '', + PICName: data.picMerchant || '', + PICPosition: data.picPosition || '', + address: data.address || '', + state: data.state || '', + city: data.city || '', + district: data.district || '', + subDistrict: data.subDistrict || '', + zip: parseInt(data.zip) || '', + email: data.emailCompany || '', + emailSales: data.emailSales || '', + emailFinance: data.emailFinance || '', + bank: data.bankName || '', + rekening: data.rekeningName || '', + accountNumber: data.accountNumber || '', + phone: data.phone || '', + mobile: data.mobile || '', + bisnisType: data.bisnisType ? parseInt(data.bisnisType) : null, + categoryPerusahaan: data.categoryPerusahaan + ? parseInt(data.categoryPerusahaan) + : null, + website: data.website || '', + }); + } catch (error) { + console.error('Error loading profile:', error); + handleIsError(true); // Jika ada error, panggil fungsi error handler + } + }; + + loadData(); + }, [reset, handleIsError]); + useEffect(() => { const loadProfile = async () => { try { @@ -170,10 +209,10 @@ const CreateMerchant = ({ handleIsError }) => { }, [auth, watchState]); const watchCity = watch('city'); - + console.log('watchCity', watchCity); useEffect(() => { if (watchCity) { - setValue('district', ''); + // setValue('district', ''); const loadDistricts = async () => { let dataDistricts = await districtApi({ cityId: watchCity }); dataDistricts = dataDistricts.map((district) => ({ @@ -189,7 +228,7 @@ const CreateMerchant = ({ handleIsError }) => { const watchDistrict = watch('district'); useEffect(() => { if (watchDistrict) { - setValue('subDistrict', ''); + // setValue('subDistrict', ''); const loadSubDistricts = async () => { let dataSubDistricts = await subDistrictApi({ districtId: watchDistrict, @@ -211,7 +250,7 @@ const CreateMerchant = ({ handleIsError }) => { let kecamatan = ''; if (watchDistrict) { - setValue('zip', ''); + // setValue('zip', ''); for (const data in districts) { if (districts[data].value == watchDistrict) { kecamatan = districts[data].label.toLowerCase(); @@ -266,7 +305,6 @@ const CreateMerchant = ({ handleIsError }) => { pejabat_name: values.pejabatName, pic_merchant: values.PICName, pic_position: values.PICPosition, - partner_id: auth.partnerId, address: values.address, state: values.state, city: values.city, @@ -283,6 +321,7 @@ const CreateMerchant = ({ handleIsError }) => { mobile: values.mobile, bisnis_type: values.bisnisType, category_perusahaan: values.categoryPerusahaan, + website: values.website, description: 'Nama Perusahaan : ' + values.company + @@ -297,15 +336,13 @@ const CreateMerchant = ({ handleIsError }) => { ' \r\n No Hp : ' + values.mobile, }; - // isError(true); - // toast.dismiss(toastId); - // toast.success('Berhasil menambahkan data'); const create_leads = await createMerchantApi({ data }); if (create_leads) { toast.dismiss(toastId); toast.success('Berhasil menambahkan data'); + isError(false); reset(); - router.push('/'); + // router.push('/'); } else { toast.dismiss(toastId); toast.error('Gagal menambahkan data'); diff --git a/src/lib/merchant/components/InformasiVendor.jsx b/src/lib/merchant/components/InformasiVendor.jsx index 6e57eade..d47d22ee 100644 --- a/src/lib/merchant/components/InformasiVendor.jsx +++ b/src/lib/merchant/components/InformasiVendor.jsx @@ -9,7 +9,8 @@ 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 createMerchantApi from '../../form/api/createMerchantApi'; +import createMerchantApi from '../api/createMerchantApi'; +import getMerchantApi from '../api/getMerchantApi'; import addressApi from '@/lib/address/api/addressApi'; import PageContent from '@/lib/content/components/PageContent'; import { useRouter } from 'next/router'; @@ -77,6 +78,7 @@ const InformasiVendor = ({ handleIsError }) => { const [fileNames, setFileNames] = useState({}); const [DeatailFile, setDetailFile] = useState({}); const [subDistricts, setSubDistricts] = useState([]); + const [categoryProduk, setCategoryProduk] = useState([]); const [zips, setZips] = useState([]); const [isExample, setIsExample] = useState(false); const [BannerMerchant, setBannerMerchant] = useState(); @@ -129,13 +131,35 @@ const InformasiVendor = ({ handleIsError }) => { { id: 2477, name: 'Peralatan Horeca & Food Service' }, ]; - const midIndex = Math.ceil(category_produk.length / 2); - const firstColumn = category_produk.slice(0, midIndex); - const secondColumn = category_produk.slice(midIndex); + const midIndex = Math.ceil(categoryProduk.length / 2); + const firstColumn = categoryProduk.slice(0, midIndex); + const secondColumn = categoryProduk.slice(midIndex); const [kreditLimitFormat, setKreditLimitFormat] = useState(); + useEffect(() => { + const loadData = async () => { + const data = await getMerchantApi(); + console.log('data vendor', data); + reset({ + hargaTayang: data.hargaTayang || '', + categoryProduk: data.categoryProduk || '', + merkDagang: data.merkDagang || '', + isPengajuanTempo: data.isPengajuanTempo || '', + tempoDuration: parseInt(data.tempoDuration) || '', + // kreditLimit: parseInt(data.kreditLimit) || '', + waktuPengiriman: data.waktuPengiriman || '', + terhitungSejak: parseInt(data.terhitungSejak) || '', + }); + handleKreditLimitChange(data.kreditLimit); + setSelectedIds(watch('categoryProduk').split(',').map(Number)); + }; + + loadData(); + }, []); + const handleKreditLimitChange = (e) => { - let value = e.target.value; + console.log('e', e); + let value = e.target?.value ? e.target.value : e; // Hapus semua karakter non-numeric value = value.replace(/[^\d]/g, ''); @@ -159,12 +183,17 @@ const InformasiVendor = ({ handleIsError }) => { // form.categoryProduk ? form.categoryProduk.split(',').map(Number) : [] // Parse string menjadi array angka // [] // Parse string menjadi array angka ); - + console.log( + 'kategori produk', + watch('categoryProduk').split(',').map(Number) + ); + console.log('selectedIds', selectedIds); const handleCheckboxChange = (id) => { const updatedSelected = selectedIds.includes(id) ? selectedIds.filter((selectedId) => selectedId !== id) : [...selectedIds, id]; + console.log('updatedSelected', updatedSelected); setSelectedIds(updatedSelected); // Mengubah array kembali menjadi string yang dipisahkan oleh koma @@ -184,6 +213,21 @@ const InformasiVendor = ({ handleIsError }) => { }); }, []); + console.log('categoryProduk', categoryProduk); + useEffect(() => { + const loadCategories = async () => { + let dataCategories = await odooApi('GET', '/api/v1/category/tree'); + const formattedCategories = dataCategories.map((category) => ({ + id: category.id, + name: category.name, + })); + console.log('formattedCategories', formattedCategories); + // Simpan hasil ke state + setCategoryProduk(formattedCategories); + }; + loadCategories(); + }, []); + useEffect(() => { const loadProfile = async () => { try { @@ -333,7 +377,7 @@ const InformasiVendor = ({ handleIsError }) => { const data = { ...values, harga_tayang: values.hargaTayang, - category_produk: values.categoryProduk, + categoryProduk: values.categoryProduk, merk_dagang: values.merkDagang, is_pengajuan_tempo: values.isPengajuanTempo, tempo_duration: values.tempoDuration, @@ -341,21 +385,17 @@ const InformasiVendor = ({ handleIsError }) => { waktu_pengiriman: values.waktuPengiriman, terhitung_sejak: values.terhitungSejak, }; - isError(true); - toast.dismiss(toastId); - toast.success('Berhasil menambahkan data'); - // const formData = new FormData(); - // formData.append('npwp', values.npwp[0]); - // const create_leads = await createMerchantApi({ data }); - // if (create_leads) { - // toast.dismiss(toastId); - // toast.success('Berhasil menambahkan data'); - // reset(); - // router.push('/'); - // } else { - // toast.dismiss(toastId); - // toast.error('Gagal menambahkan data'); - // } + const create_leads = await createMerchantApi({ data }); + if (create_leads) { + toast.dismiss(toastId); + isError(false); + toast.success('Berhasil menambahkan data'); + reset(); + // router.push('/+'); + } else { + toast.dismiss(toastId); + toast.error('Gagal menambahkan data'); + } }; // const DownLoadSurat = () => { diff --git a/src/lib/merchant/components/Merchant.jsx b/src/lib/merchant/components/Merchant.jsx index 59f80be5..a995cb19 100644 --- a/src/lib/merchant/components/Merchant.jsx +++ b/src/lib/merchant/components/Merchant.jsx @@ -36,7 +36,9 @@ const Merchant = () => { const handleIsError = (value) => { console.log('value yang dihasilkan', value); - goToNextStep(); + if (!value) { + goToNextStep(); + } setError(value); // Memperbarui state berdasarkan isError }; const stepDivs = [ diff --git a/src/lib/merchant/components/SyaratDagang.jsx b/src/lib/merchant/components/SyaratDagang.jsx index 36c6e0a9..83faf520 100644 --- a/src/lib/merchant/components/SyaratDagang.jsx +++ b/src/lib/merchant/components/SyaratDagang.jsx @@ -9,7 +9,8 @@ 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 createMerchantApi from '../../form/api/createMerchantApi'; +import createMerchantApi from '../api/createMerchantApi'; +import getMerchantApi from '../api/getMerchantApi'; import addressApi from '@/lib/address/api/addressApi'; import PageContent from '@/lib/content/components/PageContent'; import { useRouter } from 'next/router'; @@ -76,6 +77,7 @@ const SyaratDagang = ({ handleIsError }) => { const [fileNames, setFileNames] = useState({}); const [DeatailFile, setDetailFile] = useState({}); const [subDistricts, setSubDistricts] = useState([]); + const [bigData, setbigData] = useState([]); const [zips, setZips] = useState([]); const [isExample, setIsExample] = useState(false); const [BannerMerchant, setBannerMerchant] = useState(); @@ -84,6 +86,43 @@ const SyaratDagang = ({ handleIsError }) => { const recaptchaRef = useRef(null); const router = useRouter(); + useEffect(() => { + const loadData = async () => { + const data = await getMerchantApi(); + setbigData(data); + console.log('data vendor', data); + reset({ + isKembaliBarang: data.isKembaliBarang || '', + textReturn: data.textReturn || '', + tenggatWaktu: (() => { + const waktu = data.tenggatWaktu + ? data.tenggatWaktu.split(' ')[0] + : ''; + if (waktu === '14') return '14'; + if (waktu === '30') return '30'; + return 'custom'; + })(), + customTenggatWaktu: (() => { + if (watch('tenggatWaktu') === 'custom') return data.tenggatWaktu; + return ''; + })(), + sertifikatProduk: data.sertifikatProduk || '', + customSertifikatProduk: data.customSertifikatProduk || '', + tempoGaransi: parseInt(data.tempoGaransi) || '', + explainGaransi: data.explainGaransi || '', + minimumPembelian: data.minimumPembelian || '', + isOrderQuantity: data.isOrderQuantity || '', + }); + // handleKreditLimitChange(data.kreditLimit); + setSelectedIds(watch('sertifikatProduk').split(',').map(Number)); + if (watch('customSertifikatProduk')) { + // setSelectedIds([...selectedIds, 4]); + } + }; + + loadData(); + }, []); + console.log('sertifikatProduk', watch('sertifikatProduk')); useEffect(() => { window.scrollTo({ top: 0, @@ -111,9 +150,9 @@ const SyaratDagang = ({ handleIsError }) => { { value: 3, label: '2 Tahun Garansi' }, ]; const dataMinimumOrderQuantity = [ - { value: 1, label: 'Dus' }, - { value: 2, label: 'Lusin' }, - { value: 3, label: 'Minimum pembelian' }, + { value: 'dus', label: 'Dus' }, + { value: 'lusin', label: 'Lusin' }, + { value: 'minimum pembelian', label: 'Minimum pembelian' }, ]; const dataCategoryPerusahaan = [ @@ -140,7 +179,7 @@ const SyaratDagang = ({ handleIsError }) => { // form.sertifikatProduk ? form.sertifikatProduk.split(',').map(Number) : [] // Parse string menjadi array angka // [] // Parse string menjadi array angka ); - + console.log('selectedIds', selectedIds); const handleCheckboxChange = (id) => { const updatedSelected = selectedIds.includes(id) ? selectedIds.filter((selectedId) => selectedId !== id) @@ -151,6 +190,14 @@ const SyaratDagang = ({ handleIsError }) => { // Mengubah array kembali menjadi string yang dipisahkan oleh koma setValue('sertifikatProduk', updatedSelected.join(',')); }; + const custom_sertifikat_produk_handle = () => { + const updatedSelected = [...selectedIds, 4]; + + setSelectedIds(updatedSelected); + + // Mengubah array kembali menjadi string yang dipisahkan oleh koma + setValue('sertifikatProduk', updatedSelected.join(',')); + }; const isChecked = (id) => selectedIds.includes(id); @@ -316,27 +363,26 @@ const SyaratDagang = ({ handleIsError }) => { tenggat_waktu: values.tenggatWaktu, custom_tenggat_waktu: values.customTenggatWaktu, sertifikat_produk: values.sertifikatProduk, - custom_sertifikat_produk: values.customSertifikatProduk, + custom_sertifikat_produk: + values.customSertifikatProduk == '' + ? false + : values.customSertifikatProduk, tempo_garansi: values.tempoGaransi, explain_garansi: values.explainGaransi, is_order_quantity: values.isOrderQuantity, minimum_pembelian: values.minimumPembelian, }; - isError(true); - toast.dismiss(toastId); - toast.success('Berhasil menambahkan data'); - // const formData = new FormData(); - // formData.append('npwp', values.npwp[0]); - // const create_leads = await createMerchantApi({ data }); - // if (create_leads) { - // toast.dismiss(toastId); - // toast.success('Berhasil menambahkan data'); - // reset(); - // router.push('/'); - // } else { - // toast.dismiss(toastId); - // toast.error('Gagal menambahkan data'); - // } + const create_leads = await createMerchantApi({ data }); + if (create_leads) { + toast.dismiss(toastId); + toast.success('Berhasil menambahkan data'); + isError(false); + reset(); + // router.push('/'); + } else { + toast.dismiss(toastId); + toast.error('Gagal menambahkan data'); + } }; // const DownLoadSurat = () => { @@ -463,10 +509,18 @@ const SyaratDagang = ({ handleIsError }) => { value={watch('tenggatWaktu')} > - + setValue('customTenggatWaktu', ' ')} + > 14 hari sejak data dikirimkan - + setValue('customTenggatWaktu', ' ')} + > 30 hari sejak data dikirimkan
@@ -476,7 +530,6 @@ const SyaratDagang = ({ handleIsError }) => { placeholder='Masukkan jumlah hari untuk tenggat waktu' type='text' onFocus={() => setValue('tenggatWaktu', 'custom')} - onChange={() => setValue('tenggatWaktu', 'custom')} className='form-input mt-2' />
@@ -539,8 +592,10 @@ const SyaratDagang = ({ handleIsError }) => { {...register('customSertifikatProduk')} placeholder='Masukkan Dokumen/Sertifikat yang dimiliki oleh brand' type='text' - onFocus={() => setSelectedIds([...selectedIds, 4])} - onChange={() => setSelectedIds([...selectedIds, 4])} + onFocus={() => { + custom_sertifikat_produk_handle(); + }} + // onFocus={() => handleCheckboxChange(4)} className='form-input mt-2' /> @@ -613,11 +668,19 @@ const SyaratDagang = ({ handleIsError }) => {
- + setValue('isOrderQuantity', 'ya')} + > Ya @@ -631,11 +694,18 @@ const SyaratDagang = ({ handleIsError }) => { placeholder={ 'Pilih jenis minimum order quantity' } + onFocus={() => + setValue('isOrderQuantity', 'ya') + } /> )} />
- + setValue('minimumPembelian', '')} + > Tidak Ada
-- cgit v1.2.3