summaryrefslogtreecommitdiff
path: root/src/pages/shop/brands.js
blob: 797843ee3a2316f40a8283153a8a9b704b8d8734 (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
import { LazyLoadImage } from "react-lazy-load-image-component";
import Header from "../../components/Header";
import apiOdoo from "../../helpers/apiOdoo";
import "react-lazy-load-image-component/src/effects/blur.css";
import Link from "next/link";
import { createSlug } from "../../helpers/slug";
import InfiniteScroll from "react-infinite-scroll-component";
import { useState } from "react";
import Spinner from "../../components/Spinner";

export async function getServerSideProps() {
  let initialManufactures = await apiOdoo('GET', '/api/v1/manufacture?limit=31');
  return {props: {initialManufactures}};
}

export default function Brands({ initialManufactures }) {
  const [manufactures, setManufactures] = useState(initialManufactures.manufactures);
  const [hasMoreManufacture, setHasMoreManufacture] = useState(true);

  const getMoreManufactures = async () => {
    const result = await apiOdoo('GET', `/api/v1/manufacture?limit=31&offset=${manufactures.length}`);
    if (manufactures.length + 30 >= result.manufacture_total) setHasMoreManufacture(false);
    setManufactures((manufactures) => [...manufactures, ...result.manufactures]);
  };
  return (
    <>
      <Header title='Semua Brand di Indoteknik' />
      <main className="p-4">
        <h1>Semua Brand di Indoteknik</h1>
        <InfiniteScroll
          dataLength={manufactures.length}
          next={getMoreManufactures}
          hasMore={hasMoreManufacture}
          className="grid grid-cols-3 gap-4 mt-4 !overflow-x-hidden"
          loader={
            <div className="flex justify-center items-center border border-gray-300 p-1 rounded h-14">
              <Spinner className="w-6 h-6 text-gray-600 fill-gray-900"/>
            </div>
          }
        >
          {manufactures?.map((manufacture, index) => (
            manufacture.name ? (
              <Link href={`/shop/brands/${createSlug(manufacture.name, manufacture.id)}`} className="flex justify-center items-center border border-gray-300 p-1 rounded h-16 text-gray-800 text-sm text-center bg-white" key={index}>
                {manufacture.logo ? (
                  <LazyLoadImage effect="blur" src={manufacture.logo} alt={manufacture.name || ''} className="w-full max-h-full object-contain object-center" />
                ) : manufacture.name}
              </Link>
            ) : ''
          ))}
        </InfiniteScroll>
      </main>
    </>
  )
}