summaryrefslogtreecommitdiff
path: root/src/lib/review/components/CustomerReviews.jsx
blob: 6ca0fa7b879f356db01dd1a5aa79037280093a85 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import DesktopView from '@/core/components/views/DesktopView';
import MobileView from '@/core/components/views/MobileView';
import Image from 'next/image';
import { Swiper, SwiperSlide } from 'swiper/react';
import { Autoplay } from 'swiper';
import { useEffect, useState } from 'react';

const { useQuery } = require('react-query');
const { getCustomerReviews } = require('../api/customerReviewsApi');

const CustomerReviews = () => {
  const [data, setData] = useState(null);

  useEffect(() => {
    const localData = localStorage.getItem('Homepage_customerReviews');
    if (localData) {
      setData(JSON.parse(localData));
    }
  },[])


  const { data: fetchCustomerReviews } = useQuery(
    'customerReviews',
    getCustomerReviews,{
      enabled: !data,
      onSuccess: (data) => {
        if (data) {
          localStorage.setItem('Homepage_customerReviews', JSON.stringify(data));
          setData(data);
        }
      }
    }
  );

  const customerReviews = data

  return (
    <div className='px-4 sm:px-0'>
      <h1 className='font-semibold text-[14px] sm:text-h-lg mb-4'>
        Ulasan Konsumen Kami
      </h1>

      <DesktopView>
        <Swiper slidesPerView={3.2} spaceBetween={16} {...swiperProps}>
          {customerReviews &&
            customerReviews?.map((customerReview) => (
              <SwiperSlide className='pb-4' key={customerReview.id}>
                <Card customerReview={customerReview} />
              </SwiperSlide>
            ))}
        </Swiper>
      </DesktopView>

      <MobileView>
        <Swiper slidesPerView={1.1} spaceBetween={8} {...swiperProps}>
          {customerReviews &&
            customerReviews?.map((customerReview) => (
              <SwiperSlide className='pb-4' key={customerReview.id}>
                <Card customerReview={customerReview} />
              </SwiperSlide>
            ))}
        </Swiper>
      </MobileView>
    </div>
  );
};

const swiperProps = {
  autoplay: {
    delay: 6000,
    disableOnInteraction: false,
  },
  loop: true,
  modules: [Autoplay],
};

const Card = ({ customerReview }) => (
  <div className='bg-gray-200 rounded-md px-5 py-6 shadow-md shadow-gray-500/20 h-full'>
    <div className='flex items-center space-x-3 mb-4'>
      <Image
        src={customerReview.image}
        alt={customerReview.customerName}
        width={48}
        height={48}
        className='rounded-full border border-yellow-400'
      />
      <div className='text-body-2 sm:text-body-1 font-semibold leading-6 text-gray-900'>
        {customerReview.customerName}
      </div>
    </div>
    <div
      className='leading-7 text-gray-800'
      dangerouslySetInnerHTML={{ __html: customerReview.ulasan }}
    />
  </div>
);

export default CustomerReviews;