From 9165f69c465b3f412bd7875b5ccd52fe66fb9ee5 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Tue, 11 Nov 2025 13:56:52 +0700
Subject: <Miqdad> fix sj docs not sending all images

---
 app/page.tsx | 94 +++++++++++++++++++++---------------------------------------
 1 file changed, 33 insertions(+), 61 deletions(-)

(limited to 'app/page.tsx')

diff --git a/app/page.tsx b/app/page.tsx
index 35609a6..d398dde 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -83,80 +83,52 @@ export default function Home() {
       return;
     }
 
+    // --- normalisasi SJ jadi array & strip prefix ---
     const sjArr: string[] = Array.isArray(imageSj)
       ? imageSj
       : imageSj
       ? [imageSj as unknown as string]
       : [];
+    const sjList = sjArr.map((x) => x.replace(/^.*?,/, "")); // base64 murni
 
-    if (isDispatch) {
-      if (!imageDispatch) {
-        alert("Foto Dispatch Wajib Diisi");
-        setIsLoading(false);
-        return;
-      }
-    } else {
-      if (sjArr.length < 1 || !imagePackage) {
-        alert("Barcode, Foto SJ, dan Foto Penerima harus tersedia.");
-        setIsLoading(false);
-        return;
-      }
-    }
-
-    try {
-      const latestSj = sjArr.at(-1) || null;
-      const newSjImage = latestSj ? latestSj.replace(/^.*?,/, "") : undefined;
-
-      const newPackageImage = imagePackage
-        ? imagePackage.replace(/^.*?,/, "")
-        : undefined;
+    const pkgB64 = imagePackage ? imagePackage.replace(/^.*?,/, "") : undefined;
+    const dispatchB64 =
+      imageDispatch && imageDispatch.startsWith("data:")
+        ? imageDispatch.replace(/^.*?,/, "")
+        : imageDispatch || undefined;
 
-      const newDispatchImage =
-        imageDispatch && imageDispatch.startsWith("data:")
-          ? imageDispatch.replace(/^.*?,/, "")
-          : imageDispatch || undefined;
+    // payload umum (dikirm sekali saja)
+    const common: Record<string, string> = {};
+    if (pkgB64) common.paket_document = pkgB64;
+    if (!isDriver && dispatchB64) common.dispatch_document = dispatchB64;
+    if (isDispatch && shippingMethod) common.shipping_method = shippingMethod;
+    if (shippingMethod === "self_pickup") common.self_pu = "true";
 
-      // Kirim hanya yang ada
-      const submittedSj = !!newSjImage;
-      const submittedPackage = !!newPackageImage;
-      const submittedDispatch = !!newDispatchImage && !isDriver;
+    const url = `/api/v1/stock-picking/${barcode}/documentation`;
 
-      const data: Record<string, string> = {};
-      if (submittedSj) data.sj_documentations = newSjImage; // plural sesuai API
-      if (submittedPackage) data.paket_document = newPackageImage!;
-      if (submittedDispatch) data.dispatch_document = newDispatchImage!;
-      if (isDispatch && shippingMethod) data.shipping_method = shippingMethod;
-      if (shippingMethod === "self_pickup") data.self_pu = "true";
-
-      const response = (await odooApi(
-        "PUT",
-        `/api/v1/stock-picking/${barcode}/documentation`,
-        data
-      )) as { status?: { code?: number } };
-
-      if (response?.status?.code === 200) {
-        alert("Berhasil Submit Data");
-        setBarcode("");
+    try {
+      // 1) Kalau ada foto SJ, kirim yang pertama + payload umum
+      if (sjList.length > 0) {
+        await odooApi("PUT", url, { ...common, sj_documentations: sjList[0] });
 
-        if (submittedSj) {
-          const next = sjArr.slice(0, -1);
-          setImageSj(next);
+        // 2) Kirim sisa foto SJ satu-per-satu (tanpa field lain)
+        for (let i = 1; i < sjList.length; i += 1) {
+          await odooApi("PUT", url, { sj_documentations: sjList[i] });
         }
-        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 (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);
+        // Tidak ada foto SJ → cukup kirim field lain
+        await odooApi("PUT", url, common);
       }
+
+      alert("Berhasil Submit Data");
+      setBarcode("");
+      // bersihkan semua SJ yang sudah terkirim
+      if (sjList.length > 0) setImageSj([]);
+      if (pkgB64) setImagePackage("");
+      if (dispatchB64) setImageDispatch("");
+    } catch (err) {
+      console.error(err);
+      alert("Gagal submit sebagian/seluruh foto. Coba lagi.");
     } finally {
       setIsLoading(false);
     }
-- 
cgit v1.2.3

