summaryrefslogtreecommitdiff
path: root/src/lib/home/components/PreferredBrand.jsx
blob: 6b64a44447b26bac90003d36a9c13eff9ad257fe (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
62
63
64
65
66
67
68
69
70
71
import { Swiper, SwiperSlide } from 'swiper/react'
import { useCallback, useEffect, useState } from 'react'
import usePreferredBrand from '../hooks/usePreferredBrand'
import PreferredBrandSkeleton from './Skeleton/PreferredBrandSkeleton'
import BrandCard from '@/lib/brand/components/BrandCard'
import useDevice from '@/core/hooks/useDevice'
import Link from '@/core/components/elements/Link/Link'
import axios from 'axios'

const PreferredBrand = () => {
  let query = 'level_s'
  let params = 'prioritas'
  const [isLoading, setIsLoading] = useState(true)
  const [startWith, setStartWith] = useState(null)
  const [manufactures, setManufactures] = useState([])

  const loadBrand = useCallback(async () => {
    setIsLoading(true)
    const name = startWith ? `${startWith}*` : ''
    const result = await axios(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/brands?params=${name}`)
    
    setIsLoading(false)
    setManufactures((manufactures) => [...result.data])
  }, [startWith])

  const toggleStartWith = (alphabet) => {
    setManufactures([])
    if (alphabet == startWith) {
      setStartWith(null)
      return
    }
    setStartWith(alphabet)
  }

  useEffect(() => {
    loadBrand()
  }, [loadBrand])

  const { preferredBrands } = usePreferredBrand(query)
  const { isMobile, isDesktop } = useDevice()

  return (
    <div className='px-4 sm:px-0'>
      <div className='flex justify-between items-center mb-4'>
        <div className='font-semibold sm:text-h-lg'>Brand Pilihan</div>
        {isDesktop && (
          <Link href='/shop/brands' className='!text-red-500 font-semibold'>
            Lihat Semua
          </Link>
        )}
        {isMobile && (
          <Link href='/shop/brands' className='!text-red-500 font-semibold sm:text-h-sm'>
            Lihat Semua
          </Link>
        )}
      </div>
      {manufactures.isLoading && <PreferredBrandSkeleton />}
      {!manufactures.isLoading && (
        <Swiper slidesPerView={isMobile ? 3.5 : 7.5} spaceBetween={isMobile ? 12 : 24} freeMode>
          {manufactures.map((manufacture) => (
            <SwiperSlide key={manufacture.id}>
              <BrandCard brand={manufacture} />
            </SwiperSlide>
          ))}
        </Swiper>
      )}
    </div>
  )
}

export default PreferredBrand