From 7d5f7d239231cdfc439a57930e315586b206a0e5 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 17 Sep 2024 15:53:06 +0700
Subject: <iman> update new register

---
 src/lib/address/components/Addresses.jsx | 135 +++++++++++++++++++++++--------
 1 file changed, 100 insertions(+), 35 deletions(-)

(limited to 'src/lib/address/components/Addresses.jsx')

diff --git a/src/lib/address/components/Addresses.jsx b/src/lib/address/components/Addresses.jsx
index a610d371..a8879607 100644
--- a/src/lib/address/components/Addresses.jsx
+++ b/src/lib/address/components/Addresses.jsx
@@ -1,34 +1,72 @@
-import Link from '@/core/components/elements/Link/Link'
-import Spinner from '@/core/components/elements/Spinner/Spinner'
-import useAuth from '@/core/hooks/useAuth'
-import { getItemAddress, updateItemAddress } from '@/core/utils/address'
-import { useRouter } from 'next/router'
-import useAddresses from '../hooks/useAddresses'
-import MobileView from '@/core/components/views/MobileView'
-import DesktopView from '@/core/components/views/DesktopView'
-import Menu from '@/lib/auth/components/Menu'
+import { useState } from 'react';
+import Link from '@/core/components/elements/Link/Link';
+import Spinner from '@/core/components/elements/Spinner/Spinner';
+import useAuth from '@/core/hooks/useAuth';
+import { getItemAddress, updateItemAddress } from '@/core/utils/address';
+import { useRouter } from 'next/router';
+import useAddresses from '../hooks/useAddresses';
+import MobileView from '@/core/components/views/MobileView';
+import DesktopView from '@/core/components/views/DesktopView';
+import Menu from '@/lib/auth/components/Menu';
+import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
 
 const Addresses = () => {
-  const router = useRouter()
-  const { select = null } = router.query
-  const { addresses } = useAddresses()
-  const selectedAddress = getItemAddress(select || '')
+  const router = useRouter();
+  const { select = null } = router.query;
+  const { addresses } = useAddresses();
+  const selectedAddress = getItemAddress(select || '');
+  const [changeConfirmation, setChangeConfirmation] = useState(false);
+  const [selectedForChange, setSelectedForChange] = useState(null); // State baru untuk simpan alamat yang akan diubah
+
   const changeSelectedAddress = (id) => {
-    if (!select) return
-    updateItemAddress(select, id)
-    router.back()
-  }
+    if (!select) return;
+    updateItemAddress(select, id);
+    router.back();
+  };
+
+  const handleConfirmSubmit = () => {
+    setChangeConfirmation(false);
+    if (selectedForChange) {
+      router.push(`/my/address/${selectedForChange}/edit`);
+    }
+  };
 
   if (addresses.isLoading) {
     return (
       <div className='flex justify-center my-6'>
         <Spinner className='w-6 text-gray_r-12/50 fill-gray_r-12' />
       </div>
-    )
+    );
   }
 
   return (
     <>
+      <BottomPopup
+        active={changeConfirmation}
+        close={() => setChangeConfirmation(false)} // Menutup popup
+        title='Ubah alamat Bisnis'
+      >
+        <div className='leading-7 text-gray_r-12/80'>
+          Anda akan mengubah alamat utama bisnis?
+        </div>
+        <div className='flex mt-6 gap-x-4 md:justify-end'>
+          <button
+            className='btn-solid-red flex-1 md:flex-none'
+            type='button'
+            onClick={handleConfirmSubmit}
+          >
+            Yakin
+          </button>
+          <button
+            className='btn-light flex-1 md:flex-none'
+            type='button'
+            onClick={() => setChangeConfirmation(false)}
+          >
+            Batal
+          </button>
+        </div>
+      </BottomPopup>
+
       <MobileView>
         <div className='p-4'>
           <div className='text-right'>
@@ -37,7 +75,10 @@ const Addresses = () => {
 
           <div className='grid gap-y-4 mt-4'>
             {addresses.data?.map((address, index) => {
-              const type = address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address'
+              const type =
+                address.type.charAt(0).toUpperCase() +
+                address.type.slice(1) +
+                ' Address';
               return (
                 <AddressCard
                   key={index}
@@ -45,9 +86,11 @@ const Addresses = () => {
                   type={type}
                   changeSelectedAddress={changeSelectedAddress}
                   selectedAddress={selectedAddress}
+                  setChangeConfirmation={setChangeConfirmation} // Memanggil popup
+                  setSelectedForChange={setSelectedForChange} // Simpan id address yang akan diubah
                   select={select}
                 />
-              )
+              );
             })}
           </div>
         </div>
@@ -72,7 +115,9 @@ const Addresses = () => {
             <div className='grid grid-cols-2 gap-4'>
               {addresses.data?.map((address, index) => {
                 const type =
-                  address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address'
+                  address.type.charAt(0).toUpperCase() +
+                  address.type.slice(1) +
+                  ' Address';
                 return (
                   <AddressCard
                     key={index}
@@ -80,20 +125,30 @@ const Addresses = () => {
                     type={type}
                     changeSelectedAddress={changeSelectedAddress}
                     selectedAddress={selectedAddress}
+                    setChangeConfirmation={setChangeConfirmation}
+                    setSelectedForChange={setSelectedForChange}
                     select={select}
                   />
-                )
+                );
               })}
             </div>
           </div>
         </div>
       </DesktopView>
     </>
-  )
-}
+  );
+};
 
-const AddressCard = ({ address, selectedAddress, changeSelectedAddress, type, select }) => {
-  const auth = useAuth()
+const AddressCard = ({
+  address,
+  selectedAddress,
+  changeSelectedAddress,
+  type,
+  select,
+  setChangeConfirmation,
+  setSelectedForChange,
+}) => {
+  const auth = useAuth();
 
   return (
     <div
@@ -106,23 +161,33 @@ const AddressCard = ({ address, selectedAddress, changeSelectedAddress, type, se
           (select && 'cursor-pointer hover:bg-gray_r-4 transition')
         }`}
     >
-      <div onClick={() => changeSelectedAddress(address.id)} className={select && 'cursor-pointer'}>
+      <div
+        onClick={() => changeSelectedAddress(address.id)}
+        className={select && 'cursor-pointer'}
+      >
         <div className='flex gap-x-2'>
           <div className='badge-red'>{type}</div>
-          {auth?.partnerId == address.id && <div className='badge-green'>Utama</div>}
+          {auth?.partnerId == address.id && (
+            <div className='badge-green'>Utama</div>
+          )}
         </div>
         <p className='font-medium mt-2'>{address.name}</p>
-        {address.mobile && <p className='mt-2 text-gray_r-11'>{address.mobile}</p>}
+        {address.mobile && (
+          <p className='mt-2 text-gray_r-11'>{address.mobile}</p>
+        )}
         <p className='mt-1 leading-6 text-gray_r-11'>{address.street}</p>
       </div>
-      <Link
-        href={`/my/address/${address.id}/edit`}
+      <button
+        onClick={() => {
+          setSelectedForChange(address.id); // Set alamat yang dipilih
+          setChangeConfirmation(true); // Tampilkan popup konfirmasi
+        }}
         className='btn-light bg-white mt-3 w-full !text-gray_r-11'
       >
         Ubah Alamat
-      </Link>
+      </button>
     </div>
-  )
-}
+  );
+};
 
-export default Addresses
+export default Addresses;
-- 
cgit v1.2.3


From dc3f5a3196a41c676f328aa0804af516d39e69f9 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 17 Sep 2024 16:23:41 +0700
Subject: <iman> update new register ubah alamat

---
 src/lib/address/components/Addresses.jsx | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(limited to 'src/lib/address/components/Addresses.jsx')

diff --git a/src/lib/address/components/Addresses.jsx b/src/lib/address/components/Addresses.jsx
index a8879607..9ca617ae 100644
--- a/src/lib/address/components/Addresses.jsx
+++ b/src/lib/address/components/Addresses.jsx
@@ -149,6 +149,7 @@ const AddressCard = ({
   setSelectedForChange,
 }) => {
   const auth = useAuth();
+  const router = useRouter();
 
   return (
     <div
@@ -179,8 +180,12 @@ const AddressCard = ({
       </div>
       <button
         onClick={() => {
-          setSelectedForChange(address.id); // Set alamat yang dipilih
-          setChangeConfirmation(true); // Tampilkan popup konfirmasi
+          if (type == 'Contact Address' && auth.parentId) {
+            setSelectedForChange(address.id); // Set alamat yang dipilih
+            setChangeConfirmation(true); // Tampilkan popup konfirmasi
+          } else {
+            router.push(`/my/address/${address.id}/edit`);
+          }
         }}
         className='btn-light bg-white mt-3 w-full !text-gray_r-11'
       >
-- 
cgit v1.2.3

