From 0626907b555ead7991d03c374edc096254aced8d Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Tue, 30 Sep 2025 15:05:41 +0700
Subject: <Miqdad> Fix Login & upload image

---
 app/page.tsx | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

(limited to 'app/page.tsx')

diff --git a/app/page.tsx b/app/page.tsx
index f295c33..fe94498 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -120,25 +120,25 @@ export default function Home() {
       }
     }
 
-
     try {
-      // const newSjImage = imageSj.replace(/^.*?,/, "");
-      // const newPackageImage = imagePackage.replace(/^.*?,/, "");
-      // const newDispatchImage =
-      //   imageDispatch && imageDispatch.startsWith("data:")
-      //     ? imageDispatch.replace(/^.*?,/, "")
-      //     : undefined;
+      // siapkan base64 tanpa prefix, kalau kosong biarkan undefined
       const newSjImage = imageSj ? imageSj.replace(/^.*?,/, "") : undefined;
       const newPackageImage = imagePackage ? imagePackage.replace(/^.*?,/, "") : undefined;
       const newDispatchImage =
         imageDispatch && imageDispatch.startsWith("data:")
           ? imageDispatch.replace(/^.*?,/, "")
-          : undefined;
+          : imageDispatch || undefined; // kalau sudah base64 tanpa prefix
+
+      // tandai mana yang BENAR-BENAR akan dikirim
+      const submittedSj = !!newSjImage;
+      const submittedPackage = !!newPackageImage;
+      const submittedDispatch = !!newDispatchImage && !isDriver;
 
+      // bangun payload: hanya isi yang ada
       const data: Record<string, string> = {};
-      if (newSjImage) data.sj_document = newSjImage;
-      if (newPackageImage) data.paket_document = newPackageImage;
-      if (!isDriver && newDispatchImage) data.dispatch_document = newDispatchImage;
+      if (submittedSj) data.sj_document = newSjImage!;
+      if (submittedPackage) data.paket_document = newPackageImage!;
+      if (submittedDispatch) data.dispatch_document = newDispatchImage!;
 
       const response = (await odooApi(
         "PUT",
@@ -148,20 +148,22 @@ export default function Home() {
 
       if (response?.status?.code === 200) {
         alert("Berhasil Submit Data");
+        // barcode bebas: kalau mau tetap kosongkan
         setBarcode("");
-        setImageSj("");
-        setImagePackage("");
-        setImageDispatch("");
+
+        if (submittedSj) setImageSj("");
+        if (submittedPackage) setImagePackage("");
+        if (submittedDispatch) setImageDispatch("");
       } else if (response?.status?.code === 404) {
         alert("Gagal Submit Data, Picking Code Tidak Ditemukan ");
       } else {
         alert("Gagal Submit Data, Silahkan Coba Lagi");
       }
     } catch (error) {
-      if (error instanceof Error) {
-        console.error("Error mengirim data:", error.message);
-      } else if (axios.isAxiosError(error)) {
+      if (axios.isAxiosError(error)) {
         console.error("Error:", error.response?.data);
+      } else if (error instanceof Error) {
+        console.error("Error mengirim data:", error.message);
       } else {
         console.error("Unknown error:", error);
       }
@@ -182,7 +184,7 @@ export default function Home() {
             <div className="h-4" />
             <div className="flex justify-between">
               <SjCamera />
-              <PackageCamera />
+              {!isDispatch && <PackageCamera />}
               {!isDriver && <DispatchCamera />}
             </div>
             <div className="h-2" />
-- 
cgit v1.2.3

