From 3eb78e21b89bd0bdddb803767ea6ec983794994b Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 28 Sep 2024 10:01:30 +0700
Subject: <iman> update temp switch account

---
 .../modules/register/components/FormBisnis.tsx     | 52 +++++++++++++++-------
 1 file changed, 37 insertions(+), 15 deletions(-)

(limited to 'src-migrate/modules/register/components/FormBisnis.tsx')

diff --git a/src-migrate/modules/register/components/FormBisnis.tsx b/src-migrate/modules/register/components/FormBisnis.tsx
index cc05a3bf..932bd326 100644
--- a/src-migrate/modules/register/components/FormBisnis.tsx
+++ b/src-migrate/modules/register/components/FormBisnis.tsx
@@ -51,6 +51,10 @@ interface companyType {
   value: string;
   label: string;
 }
+interface Auth {
+  id: string;
+  parentId: string;
+}
 
 const form: React.FC<FormProps> = ({
   type,
@@ -68,7 +72,7 @@ const form: React.FC<FormProps> = ({
   // Inside your component
   const [formattedNpwp, setFormattedNpwp] = useState<string>(''); // State for formatted NPWP
   const [unformattedNpwp, setUnformattedNpwp] = useState<string>(''); // State for unformatted NPWP
-
+  const auth = useAuth() as unknown as Auth;
   const [industries, setIndustries] = useState<industry_id[]>([]);
   const [companyTypes, setCompanyTypes] = useState<companyType[]>([]);
 
@@ -342,22 +346,40 @@ const form: React.FC<FormProps> = ({
   const [fileUrl, setFileUrl] = useState('');
   const [popUpNPWP, setPopUpNpwp] = useState(false);
   const [popSppkp, setPopUpSppkp] = useState(false);
-  const [hasNPWP, sethasNpwp] = useState(false);
-  const [hasSPPKP, sethasSPPKP] = useState(false);
+  const [hasNPWP, setHasNpwp] = useState(false);
+  const [hasSPPKP, setHasSPPKP] = useState(false);
   const [fileUrlSppkp, setFileUrlSppkp] = useState('');
 
   useEffect(() => {
-    const url = `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/v1/user/download/npwp/109119`;
-    if (url) {
-      setFileUrl(url);
-      sethasNpwp(true);
-    }
-    const urlSppkp = `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/v1/user/download/sppkp/109119`;
-    if (urlSppkp) {
-      setFileUrlSppkp(urlSppkp);
-      sethasSPPKP(true);
+    const checkUrl = async (url: string | URL | Request) => {
+      try {
+        const response = await fetch(url, { method: 'HEAD' });
+        return response.ok; // Returns true if status is 200-299
+      } catch (error) {
+        console.error('Error accessing URL:', url, error);
+        return false;
+      }
+    };
+
+    const npwpUrl = `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/v1/user/download/npwp/${auth?.parentId}`;
+    const sppkpUrl = `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/v1/user/download/sppkp/${auth?.parentId}`;
+
+    if (auth?.parentId) {
+      checkUrl(npwpUrl).then((isAccessible) => {
+        if (isAccessible) {
+          setFileUrl(npwpUrl);
+          setHasNpwp(true);
+        }
+      });
+
+      checkUrl(sppkpUrl).then((isAccessible) => {
+        if (isAccessible) {
+          setFileUrlSppkp(sppkpUrl);
+          setHasSPPKP(true);
+        }
+      });
     }
-  }, []);
+  }, [auth?.parentId]);
 
   return (
     <>
@@ -783,7 +805,7 @@ const form: React.FC<FormProps> = ({
                     className={`w-6 mr-2 hover:scale-110 transition-transform duration-200}`}
                   />
                   <XCircleIcon
-                    onClick={() => sethasNpwp(false)}
+                    onClick={() => setHasNpwp(false)}
                     className='w-6 hover:scale-110 transition-transform duration-200'
                   />
                 </div>
@@ -830,7 +852,7 @@ const form: React.FC<FormProps> = ({
                     className={`w-6 mr-2 hover:scale-110 transition-transform duration-200}`}
                   />
                   <XCircleIcon
-                    onClick={() => sethasSPPKP(false)}
+                    onClick={() => setHasSPPKP(false)}
                     className='w-6 hover:scale-110 transition-transform duration-200'
                   />
                 </div>
-- 
cgit v1.2.3

