From b9b4eb3ed751a69c023a45e7cdca8da01ae6b1cd Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 15 Jan 2025 13:21:48 +0700
Subject: <iman> update merchant

---
 src/lib/merchant/components/InformasiVendor.jsx | 84 ++++++++++++++++++-------
 1 file changed, 62 insertions(+), 22 deletions(-)

(limited to 'src/lib/merchant/components/InformasiVendor.jsx')

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 = () => {
-- 
cgit v1.2.3

