From e4d8981170a419c59f04814d11213e44695d5855 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 21 Nov 2022 17:25:47 +0700 Subject: Brand with startwith --- src/pages/shop/brands.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/pages/shop/brands.js b/src/pages/shop/brands.js index a473db15..5b49c922 100644 --- a/src/pages/shop/brands.js +++ b/src/pages/shop/brands.js @@ -5,9 +5,11 @@ import "react-lazy-load-image-component/src/effects/blur.css"; import Link from "../../components/Link"; import { createSlug } from "../../helpers/slug"; import InfiniteScroll from "react-infinite-scroll-component"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import Spinner from "../../components/Spinner"; import Layout from "../../components/Layout"; +import { Swiper, SwiperSlide } from "swiper/react"; +import "swiper/css"; export async function getServerSideProps() { let initialManufactures = await apiOdoo('GET', '/api/v1/manufacture?limit=31'); @@ -17,23 +19,51 @@ export async function getServerSideProps() { export default function Brands({ initialManufactures }) { const [manufactures, setManufactures] = useState(initialManufactures.manufactures); const [hasMoreManufacture, setHasMoreManufacture] = useState(true); + const [manufactureStartwith, setManufactureStartWith] = useState(''); + + const alpha = Array.from(Array(26)).map((e, i) => i + 65); + const alphabets = alpha.map((x) => String.fromCharCode(x)); 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); + const name = manufactureStartwith != '' ? `${manufactureStartwith}%` : ''; + const result = await apiOdoo('GET', `/api/v1/manufacture?limit=30&offset=${manufactures.length}&name=${name}`); + setHasMoreManufacture(manufactures.length + 30 < result.manufacture_total) setManufactures((manufactures) => [...manufactures, ...result.manufactures]); }; + + const filterManufactureStartWith = (character) => { + setManufactures([]); + if (manufactureStartwith == character) { + setManufactureStartWith(''); + } else { + setManufactureStartWith(character); + } + }; + + useEffect(() => { + getMoreManufactures(); + }, [manufactureStartwith]); + return ( <>
-

Semua Brand di Indoteknik

+

Semua Brand di Indoteknik

+ + {alphabets.map((alphabet, index) => ( + + + + ))} + -- cgit v1.2.3