From 0a0c497204acbac562700d80f38e74aa9ffcd94e Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 1 Dec 2022 16:26:21 +0700 Subject: dynamic filter, dynamic pagination, detail brand, to title case --- src/pages/shop/brands/[slug].js | 122 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/pages/shop/brands/[slug].js (limited to 'src/pages/shop/brands') diff --git a/src/pages/shop/brands/[slug].js b/src/pages/shop/brands/[slug].js new file mode 100644 index 00000000..b532e7a7 --- /dev/null +++ b/src/pages/shop/brands/[slug].js @@ -0,0 +1,122 @@ +import axios from "axios"; +import { useState } from "react"; +import Filter from "../../../components/Filter"; +import Header from "../../../components/Header"; +import Layout from "../../../components/Layout"; +import Pagination from "../../../components/Pagination"; +import ProductCard from "../../../components/ProductCard"; +import { getNameFromSlug } from "../../../helpers/slug"; +import FilterIcon from "../../../icons/filter.svg"; + +export async function getServerSideProps(context) { + const { + slug, + page = 1, + category = '', + price_from = '0', + price_to = '0', + order_by = '', + } = context.query; + + let urlParameter = [ + 'q=*', + `page=${page}`, + `brand=${getNameFromSlug(slug)}`, + `category=${category}`, + `price_from=${price_from}`, + `price_to=${price_to}`, + `order_by=${order_by}` + ].join('&'); + let searchResults = await axios(`${process.env.SELF_HOST}/api/shop/search?${urlParameter}`); + searchResults = searchResults.data; + + return { + props: { + searchResults, + page, + slug, + category, + price_from, + price_to, + order_by + } + }; +} + +export default function BrandDetail({ + searchResults, + page, + slug, + category, + price_from, + price_to, + order_by +}) { + const pageCount = Math.ceil(searchResults.response.numFound / searchResults.responseHeader.params.rows); + const productStart = searchResults.responseHeader.params.start; + const productRows = searchResults.responseHeader.params.rows; + const productFound = searchResults.response.numFound; + + const [activeFilter, setActiveFilter] = useState(false); + + const route = () => { + let route = `/shop/brands/${slug}`; + if (category) route += `&category=${category}`; + if (price_from > 0) route += `&price_from=${price_from}`; + if (price_to > 0) route += `&price_to=${price_to}`; + if (order_by) route += `&order_by=${order_by}`; + return route; + } + + return ( + <> +
+ setActiveFilter(false)} + defaultPriceFrom={price_from} + defaultPriceTo={price_to} + defaultBrand='' + defaultCategory={category} + defaultOrderBy={order_by} + searchResults={searchResults} + disableFilter={['brand']} + /> + +
+ +

Produk

+
+ {productFound > 0 ? ( + <> + Menampilkan  + {pageCount > 1 ? ( + <> + {productStart + 1}-{ + (productStart + productRows) > productFound ? productFound : productStart + productRows + } +  dari  + + ) : ''} + {searchResults.response.numFound} +  produk untuk brand {getNameFromSlug(slug)} + + ) : 'Mungkin yang anda cari'} +
+
+ {searchResults.response.products.map((product) => ( + + ))} +
+ +
+ +
+
+
+ + ) +} \ No newline at end of file -- cgit v1.2.3