From a49c62c01c09a950ccdb16b1def75ae590dc0930 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 2 Jan 2025 09:36:29 +0700
Subject: <iman> update duration flashsale

---
 src/lib/flashSale/components/FlashSale.jsx | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

(limited to 'src/lib/flashSale/components/FlashSale.jsx')

diff --git a/src/lib/flashSale/components/FlashSale.jsx b/src/lib/flashSale/components/FlashSale.jsx
index f4be279e..715c25c5 100644
--- a/src/lib/flashSale/components/FlashSale.jsx
+++ b/src/lib/flashSale/components/FlashSale.jsx
@@ -9,13 +9,23 @@ import { FlashSaleSkeleton } from '../skeleton/FlashSaleSkeleton';
 const FlashSale = () => {
   const [flashSales, setFlashSales] = useState(null);
   const [isLoading, setIsLoading] = useState(true);
+  const [duration, setDuration] = useState();
+  const calculateRemainingTime = (endDate) => {
+    const currentTime = new Date();
+    const endTime = new Date(endDate);
 
+    const remainingTimeInSeconds = (endTime - currentTime) / 1000;
+
+    return Math.max(Math.round(remainingTimeInSeconds), 0);
+  };
   useEffect(() => {
     const loadFlashSales = async () => {
       const res = await fetch('/api/flashsale-header');
       const { data } = await res.json();
       if (data) {
         setFlashSales(data);
+        const remainingTime = calculateRemainingTime(data[0].endDate);
+        setDuration(remainingTime);
       }
       setIsLoading(false);
     };
@@ -37,7 +47,7 @@ const FlashSale = () => {
                 <div className='font-medium sm:text-h-lg mt-1.5'>
                   {flashSale.name}
                 </div>
-                <CountDown initialTime={flashSale.duration} />
+                <CountDown initialTime={duration} />
               </div>
 
               <div className='relative'>
@@ -59,7 +69,7 @@ const FlashSale = () => {
                 />
                 <FlashSaleProduct
                   flashSaleId={flashSale.pricelistId}
-                  duration={flashSale.duration}
+                  duration={duration}
                 />
               </div>
             </div>
-- 
cgit v1.2.3


From 58e8c5b5e3faf5bed973e19d8be036c1e7d004bc Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Mon, 6 Jan 2025 15:23:01 +0700
Subject: <iman> update code duration

---
 src/lib/flashSale/components/FlashSale.jsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src/lib/flashSale/components/FlashSale.jsx')

diff --git a/src/lib/flashSale/components/FlashSale.jsx b/src/lib/flashSale/components/FlashSale.jsx
index 715c25c5..9c0e713b 100644
--- a/src/lib/flashSale/components/FlashSale.jsx
+++ b/src/lib/flashSale/components/FlashSale.jsx
@@ -24,7 +24,7 @@ const FlashSale = () => {
       const { data } = await res.json();
       if (data) {
         setFlashSales(data);
-        const remainingTime = calculateRemainingTime(data[0].endDate);
+        const remainingTime = calculateRemainingTime(data[0]?.endDate);
         setDuration(remainingTime);
       }
       setIsLoading(false);
-- 
cgit v1.2.3

