summaryrefslogtreecommitdiff
path: root/src-migrate/modules/product-promo/components/CardCountdown.tsx
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-12-22 17:33:46 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-12-22 17:33:46 +0700
commit89f32128f37d99b490de7590e2116a9cfd853f89 (patch)
treefeb74cc6bd0030b291fbf3dbba9b89a7afd6ea31 /src-migrate/modules/product-promo/components/CardCountdown.tsx
parentc9366090153e8aba3a673b2b77cbc8acc24e59a5 (diff)
Update promotion program feature
Diffstat (limited to 'src-migrate/modules/product-promo/components/CardCountdown.tsx')
-rw-r--r--src-migrate/modules/product-promo/components/CardCountdown.tsx67
1 files changed, 67 insertions, 0 deletions
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