From a5db88b5e8574edb1908fd2a9b9fa6cf5ed2ff05 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Wed, 6 Mar 2024 14:30:27 +0700
Subject: add approve dan reject

---
 src/lib/transaction/components/Transaction.jsx | 83 ++++++++++++++++++++------
 1 file changed, 65 insertions(+), 18 deletions(-)

(limited to 'src/lib/transaction/components/Transaction.jsx')

diff --git a/src/lib/transaction/components/Transaction.jsx b/src/lib/transaction/components/Transaction.jsx
index ab76c671..8f4b2038 100644
--- a/src/lib/transaction/components/Transaction.jsx
+++ b/src/lib/transaction/components/Transaction.jsx
@@ -31,13 +31,16 @@ import useAirwayBill from '../hooks/useAirwayBill';
 import Manifest from '@/lib/treckingAwb/component/Manifest';
 import useAuth from '@/core/hooks/useAuth';
 import StepApproval from './stepper';
+import aprpoveApi from '../api/approveApi';
+import rejectApi from '../api/rejectApi';
 
 const Transaction = ({ id }) => {
-  const auth = { ...useAuth(), web_role: 2, feature: { so_approval: true } };
-  const statusApprovalWeb = 1
+  const auth = useAuth();
   const { transaction } = useTransaction({ id });
-  const { queryAirwayBill } = useAirwayBill({ orderId: id });
+  
+  const statusApprovalWeb = transaction.data?.approvalStep
 
+  const { queryAirwayBill } = useAirwayBill({ orderId: id });
   const [airwayBillPopup, setAirwayBillPopup] = useState(null);
 
   const poNumber = useRef(null);
@@ -73,6 +76,11 @@ const Transaction = ({ id }) => {
   const [cancelTransaction, setCancelTransaction] = useState(false);
   const openCancelTransaction = () => setCancelTransaction(true);
   const closeCancelTransaction = () => setCancelTransaction(false);
+
+  const [rejectTransaction, setRejectTransaction] = useState(false);
+  
+  const openRejectTransaction = () => setRejectTransaction(true);
+  const closeRejectTransaction = () => setRejectTransaction(false);
   const submitCancelTransaction = async () => {
     const isCancelled = await cancelTransactionApi({
       transaction: transaction.data,
@@ -94,6 +102,18 @@ const Transaction = ({ id }) => {
     transaction.refetch();
   };
 
+  const handleApproval = async () => {
+    await aprpoveApi({ id });
+    toast.success('Berhasil melanjutkan approval');
+    transaction.refetch();
+  }
+
+  const handleReject = async () => {
+   await rejectApi({ id });
+   closeRejectTransaction()
+   transaction.refetch();
+  }
+
   const memoizeVariantGroupCard = useMemo(
     () => (
       <div className='p-4 pt-0 flex flex-col gap-y-3'>
@@ -161,6 +181,33 @@ const Transaction = ({ id }) => {
           </div>
         </BottomPopup>
 
+        <BottomPopup
+          active={rejectTransaction}
+          close={closeRejectTransaction}
+          title='Batalkan Transaksi'
+        >
+          <div className='leading-7 text-gray_r-12/80'>
+            Apakah anda yakin Membatalkan transaksi{' '}
+            <span className='underline'>{transaction.data?.name}</span>?
+          </div>
+          <div className='flex justify-end mt-6 gap-x-4'>
+            <button
+              className='btn-solid-red w-full md:w-fit'
+              type='button'
+              onClick={handleReject}
+            >
+              Ya, Batalkan
+            </button>
+            <button
+              className='btn-light w-full md:w-fit'
+              type='button'
+              onClick={closeRejectTransaction}
+            >
+              Batal
+            </button>
+          </div>
+        </BottomPopup>
+
         <BottomPopup title='Upload PO' close={closeUploadPo} active={uploadPo}>
           <div>
             <label>Nomor PO</label>
@@ -246,7 +293,7 @@ const Transaction = ({ id }) => {
 
           <Divider />
 
-          {!auth.feature.so_approval && (
+          {!auth?.feature.soApproval && (
             <div className='p-4 flex flex-col gap-y-4'>
               <DescriptionRow label='Purchase Order'>
                 {transaction.data?.purchaseOrderName || '-'}
@@ -312,13 +359,13 @@ const Transaction = ({ id }) => {
           <Divider />
 
           <div className='p-4 pt-0'>
-            {transaction.data?.status == 'draft' && auth.feature.so_approval && (
+            {transaction.data?.status == 'draft' && auth?.feature.soApproval && (
               <div className='flex gap-x-2'>
                 <button
                   className='btn-yellow w-full'
                   onClick={checkout}
                   disabled={
-                    transaction.data?.status === 'cancel' ? true : false  || auth.web_role === statusApprovalWeb ? true : false
+                    transaction.data?.status === 'cancel' ? true : false  || auth?.webRole === statusApprovalWeb ? true : false
                   }
                 >
                   Approve
@@ -327,14 +374,14 @@ const Transaction = ({ id }) => {
                   className='btn-solid-red px-7 w-full'
                   onClick={checkout}
                   disabled={
-                    transaction.data?.status === 'cancel' ? true : false  || auth.web_role === statusApprovalWeb ? true : false
+                    transaction.data?.status === 'cancel' ? true : false  || auth?.webRole === statusApprovalWeb ? true : false
                   }
                 >
                   Reject
                 </button>
               </div>
             )}
-            {transaction.data?.status == 'draft' && !auth.feature.so_approval && (
+            {transaction.data?.status == 'draft' && !auth?.feature?.soApproval && (
               <button className='btn-yellow w-full mt-4' onClick={checkout}>
                 Lanjutkan Transaksi
               </button>
@@ -368,10 +415,10 @@ const Transaction = ({ id }) => {
                 <h1 className='text-title-sm font-semibold mb-6'>
                   Detail Transaksi
                 </h1>
-                {auth.feature.so_approval && (
+                {auth?.feature?.soApproval && (
                   <StepApproval
                     layer={statusApprovalWeb}
-                    status={'cancel'}
+                    status={transaction?.data?.status}
                     className='ml-auto'
                   />
                 )}
@@ -392,22 +439,22 @@ const Transaction = ({ id }) => {
                   Download
                 </button>
                 {transaction.data?.status == 'draft' &&
-                  auth.feature.so_approval && auth.web_role && (
+                  auth?.feature?.soApproval && auth?.webRole && (
                     <div className='flex gap-x-2'>
                       <button
                         className='btn-yellow'
-                        onClick={checkout}
+                        onClick={handleApproval}
                         disabled={
-                          transaction.data?.status === 'cancel' ? true : false || auth.web_role === statusApprovalWeb ? true : false
+                          transaction.data?.status === 'cancel' ? true : false || auth?.webRole === statusApprovalWeb ? true : false || statusApprovalWeb < 1 ? true : false
                         }
                       >
                         Approve
                       </button>
                       <button
                         className='btn-solid-red px-7'
-                        onClick={checkout}
+                        onClick={openRejectTransaction}
                         disabled={
-                          transaction.data?.status === 'cancel' ? true : false || auth.web_role === statusApprovalWeb ? true : false
+                          transaction.data?.status === 'cancel' ? true : false || auth?.webRole === statusApprovalWeb ? true : false  || statusApprovalWeb < 1 ? true : false
                         }
                       >
                         Reject
@@ -415,12 +462,12 @@ const Transaction = ({ id }) => {
                     </div>
                   )}
                 {transaction.data?.status == 'draft' &&
-                  !auth.feature.so_approval && (
+                  !auth?.feature.soApproval && (
                     <button className='btn-yellow' onClick={checkout}>
                       Lanjutkan Transaksi
                     </button>
                   )}
-                {transaction.data?.status != 'draft' && (
+                {transaction.data?.status != 'draft' &&  !auth?.feature.soApproval &&  (
                   <button
                     className='btn-light'
                     disabled={transaction.data?.status != 'waiting'}
@@ -443,7 +490,7 @@ const Transaction = ({ id }) => {
                   <div>Ketentuan Pembayaran</div>
                   <div>: {transaction?.data?.paymentTerm}</div>
 
-                  {!auth.feature.so_approval && (
+                  {!auth?.feature?.soApproval && (
                     <>
                       <div>Purchase Order</div>
                       <div>
-- 
cgit v1.2.3

