From 89f32128f37d99b490de7590e2116a9cfd853f89 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 22 Dec 2023 17:33:46 +0700
Subject: Update promotion program feature

---
 .../product-promo/components/CardCountdown.tsx     | 67 ++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 src-migrate/modules/product-promo/components/CardCountdown.tsx

(limited to 'src-migrate/modules/product-promo/components/CardCountdown.tsx')

diff --git a/src-migrate/modules/product-promo/components/CardCountdown.tsx b/src-migrate/modules/product-promo/components/CardCountdown.tsx
new file mode 100644
index 00000000..e398a390
--- /dev/null
+++ b/src-migrate/modules/product-promo/components/CardCountdown.tsx
@@ -0,0 +1,67 @@
+import style from '../styles/card-countdown.module.css'
+
+import React, { useEffect, useState } from 'react'
+import { useQuery } from 'react-query'
+import { ClockIcon } from 'lucide-react'
+import { Skeleton } from '@chakra-ui/react'
+import moment from 'moment'
+
+import clsxm from '~/common/libs/clsxm'
+import { IPromotion } from '~/common/types/promotion'
+import { getPromotionProgram } from '~/services/promotionProgram'
+
+type Props = {
+  promotion: IPromotion
+}
+
+const ProductPromoCardCountdown = ({ promotion }: Props) => {
+  const query = useQuery(['promotion-program', promotion.program_id], async () => {
+    return await getPromotionProgram(promotion.program_id)
+  })
+
+  const program = query.data?.data || null
+
+  const [count, setCount] = useState(program?.time_left || 0);
+
+  useEffect(() => {
+    let interval: NodeJS.Timeout;
+
+    if (program?.time_left && program?.time_left > 0) {
+      setCount(program?.time_left);
+
+      interval = setInterval(() => {
+        setCount((prevCount) => prevCount - 1);
+      }, 1000);
+    }
+
+    return () => {
+      clearInterval(interval);
+    };
+  }, [program?.time_left]);
+
+  const duration = moment.duration(count, 'seconds')
+
+  const countdownClass = {
+    'text-white': true,
+    'bg-[#312782]': promotion.type.value === 'bundling',
+    'bg-[#329E44]': promotion.type.value === 'discount_loading',
+    'bg-[#FAD147]': promotion.type.value === 'merchandise',
+    'text-gray-700': promotion.type.value === 'merchandise',
+  }
+
+  return (
+    <Skeleton isLoaded={query.isFetched} className={clsxm(style.countdownSection, countdownClass)}>
+      <span>
+        <ClockIcon size={20} />
+      </span>
+      <span>Berakhir dalam</span>
+      <div className={style.countdown}>
+        <span>{duration.hours().toString().padStart(2, '0')}</span>
+        <span>{duration.minutes().toString().padStart(2, '0')}</span>
+        <span>{duration.seconds().toString().padStart(2, '0')}</span>
+      </div>
+    </Skeleton>
+  )
+}
+
+export default ProductPromoCardCountdown
\ No newline at end of file
-- 
cgit v1.2.3

