summaryrefslogtreecommitdiff
path: root/src-migrate/modules/product-promo/components/Section.tsx
blob: 4e8a7dd514af0016b276633b52cb62bcb7b9e0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import style from "../styles/section.module.css"

import { Button, Skeleton } from '@chakra-ui/react'
import { useQuery } from 'react-query'

import SmoothRender from "~/components/ui/smooth-render"
import clsxm from "~/libs/clsxm"
import { IPromotion } from '~/types/promotion'
import { useModalStore } from "../stores/useModalStore"
import ProductPromoCard from './Card'
import ProductPromoModal from "./Modal"

type Props = {
  productId: number;
}

const ProductPromoSection = ({ productId }: Props) => {
  const promotionsQuery = useQuery({
    queryKey: [`promotions.highlight`, productId],
    queryFn: async () => await fetch(`/api/product-variant/${productId}/promotion/highlight`).then((res) => res.json()) as { data: IPromotion[] }
  })

  const promotions = promotionsQuery.data

  const { openModal } = useModalStore()

  return (
    <SmoothRender
      isLoaded={(promotions?.data && promotions?.data.length > 0) || false}
      height='450px'
      duration='700ms'
    >
      <ProductPromoModal />

      {promotions?.data && promotions?.data.length > 0 && (
        <div className={style.titleWrapper}>
          <span className={style.title}>Promo Tersedia</span>
          <Button colorScheme="yellow" type='button' onClick={() => openModal(productId)}>
            Lihat Semua
          </Button>
        </div>
      )}

      <Skeleton
        isLoaded={promotionsQuery.isSuccess}
        className={clsxm(
          "flex gap-x-4 overflow-x-auto px-4 md:px-0", {
          "min-h-[340px]": promotions?.data && promotions?.data.length > 0
        })}
      >
        {promotions?.data.map((promotion) => (
          <div key={promotion.id} className="min-w-[400px] max-w-[400px]">
            <ProductPromoCard promotion={promotion}  />
          </div>
        ))}
      </Skeleton>
    </SmoothRender>
  )
}

export default ProductPromoSection