From 9159b649258703ea0539ea958cdf8b3c5987e474 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Fri, 15 Aug 2025 09:48:06 +0700
Subject: <hafid> fix alur repeat order

---
 src/lib/transaction/components/Transactions.jsx | 39 +++++++++++++------------
 1 file changed, 21 insertions(+), 18 deletions(-)

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

diff --git a/src/lib/transaction/components/Transactions.jsx b/src/lib/transaction/components/Transactions.jsx
index de93d742..2596b47f 100644
--- a/src/lib/transaction/components/Transactions.jsx
+++ b/src/lib/transaction/components/Transactions.jsx
@@ -345,8 +345,6 @@ const Transactions = ({ context = '' }) => {
   }, []);
 
   const handleBuyBack = async (products) => {
-    // if (status === 'success') return;
-
     try {
       // setStatus('loading');
 
@@ -358,32 +356,37 @@ const Transactions = ({ context = '' }) => {
             id: product.id,
             qty: product.quantity,
             selected: true,
-            source: 'buy', // Tetap gunakan 'buy' agar bisa masuk ke halaman pembelian
+            source: 'buy',
             qtyAppend: false,
+          }).catch(error => {
+            return { error, product };
           })
         )
       );
 
-      // ✅ Panggil setRefreshCart(true) setiap kali satu produk berhasil ditambahkan
+      const failedOperations = results.filter(result => result && result.error);
+      
+      if (failedOperations.length > 0) {
+        console.error('Some products failed to add to cart:', failedOperations);
+        toast.error(`${failedOperations.length} produk gagal ditambahkan ke keranjang`);
+        
+        // You might want to proceed with the successful ones or handle differently
+        if (failedOperations.length < products.length) {
+          toast.success(`${products.length - failedOperations.length} produk berhasil ditambahkan ke keranjang`);
+          setRefreshCart(true);
+          router.push('/shop/cart');
+        }
+        return;
+      }
 
+      // All operations succeeded
       setRefreshCart(true);
-
-      // setStatus('idle');
       toast.success('Semua produk berhasil ditambahkan ke keranjang belanja');
-      // Tampilkan notifikasi
-      // toast({
-      //   title: 'Tambah ke keranjang',
-      //   description: 'Semua produk berhasil ditambahkan ke keranjang belanja',
-      //   status: 'success',
-      //   duration: 3000,
-      //   isClosable: true,
-      //   position: 'top',
-      // });
-
-      // Redirect ke halaman checkout
-      router.push('/shop/checkout?source=buy');
+      router.push('/shop/cart');
+
     } catch (error) {
       console.error('Gagal menambahkan produk ke keranjang:', error);
+      toast.error('Terjadi kesalahan saat menambahkan produk ke keranjang');
       // setStatus('error');
     }
   };
-- 
cgit v1.2.3

