From f2451beed670dc697aa2026b2df6ad1ad1e90da2 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Mon, 9 Dec 2024 15:00:45 +0700
Subject: <iman> update pengajuan tempo

---
 src/pages/pengajuan-tempo/index.jsx | 44 ++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 10 deletions(-)

(limited to 'src/pages/pengajuan-tempo/index.jsx')

diff --git a/src/pages/pengajuan-tempo/index.jsx b/src/pages/pengajuan-tempo/index.jsx
index 271914c3..f0681663 100644
--- a/src/pages/pengajuan-tempo/index.jsx
+++ b/src/pages/pengajuan-tempo/index.jsx
@@ -7,38 +7,62 @@ import MobileView from '@/core/components/views/MobileView';
 import { getAuth } from '~/libs/auth';
 import { useRouter } from 'next/router';
 import { useEffect, useState } from 'react';
+import odooApi from '@/core/api/odooApi';
 
 const PagePengajuanTempo = dynamic(() =>
   import('@/lib/pengajuan-tempo/component/PengajuanTempo')
 );
 
 export default function TrackingOrder() {
+  const [tempo, setTempo] = useState(null);
+  const [isLoading, setIsLoading] = useState(true);
   const auth = getAuth();
-  console.log('auth', auth);
   const router = useRouter();
-  const [isLoading, setIsLoading] = useState(true); // Set default isLoading to true
 
   useEffect(() => {
-    // if (!auth) return; // Prevent unnecessary processing if auth is not ready
+    const loadTempo = async () => {
+      try {
+        if (auth?.partnerId) {
+          const dataTempo = await odooApi(
+            'GET',
+            `/api/v1/check/${auth.partnerId}/tempo`
+          );
+          setTempo(dataTempo);
+        }
+      } catch (error) {
+        console.error('Error fetching tempo data:', error);
+      } finally {
+        setIsLoading(false);
+      }
+    };
 
+    if (auth) {
+      loadTempo();
+    } else {
+      setIsLoading(false);
+    }
+  }, [auth]);
+
+  useEffect(() => {
     if (!auth) {
       const nextUrl = encodeURIComponent(router.asPath);
       router.push(`/login?next=${nextUrl}`);
     } else if (!auth.parentId) {
       router.push('/pengajuan-tempo/switch-account');
-    } else if (auth.tempoProgres === 'review') {
+    } else if (auth.tempoProgres === 'review' && !tempo?.paymentTerm) {
       router.push('/pengajuan-tempo/review');
-    } else if (auth.tempoProgres === 'approve') {
+    } else if (
+      auth.tempoProgres === 'approve' &&
+      tempo?.paymentTerm?.toLowerCase().includes('tempo')
+    ) {
       router.push('/pengajuan-tempo/approve');
     } else if (auth.tempoProgres === 'rejected') {
       router.push('/pengajuan-tempo/rejected');
-    } else {
-      setIsLoading(false); // Set isLoading to false only when routing is not required
     }
-  }, [auth, router]);
+  }, [auth, router, tempo]);
 
-  if (isLoading || !auth) {
-    return null; // Show nothing while loading or if auth is not ready
+  if (isLoading || !auth || !tempo) {
+    return null; // Tidak render apa pun selama loading atau auth/tempo belum tersedia
   }
 
   return (
-- 
cgit v1.2.3

