From f99e0aba70efad0deb907d8e27f09fc9f527c8a4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 17 Feb 2023 17:07:50 +0700 Subject: Refactor --- src2/pages/shop/search.js | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src2/pages/shop/search.js (limited to 'src2/pages/shop/search.js') diff --git a/src2/pages/shop/search.js b/src2/pages/shop/search.js new file mode 100644 index 00000000..4152bd43 --- /dev/null +++ b/src2/pages/shop/search.js @@ -0,0 +1,125 @@ +import axios from "axios"; +import Header from "@/components/layouts/Header"; +import Layout from "@/components/layouts/Layout"; +import Pagination from "@/components/elements/Pagination"; +import ProductCard from "@/components/products/ProductCard"; +import FilterIcon from "@/icons/filter.svg"; +import { useEffect, useState } from "react"; +import Filter from "@/components/elements/Filter"; +import Footer from "@/components/layouts/Footer"; + +export async function getServerSideProps(context) { + const { + q = '*', + page = 1, + brand = '', + category = '', + price_from = '', + price_to = '', + order_by = '', + } = context.query; + + let urlParameter = [ + `page=${page}`, + `brand=${brand}`, + `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?q=${q}&${urlParameter}`); + searchResults = searchResults.data; + return { props: { searchResults, q, page, brand, category, price_from, price_to, order_by } }; +} + +export default function ShopSearch({ + searchResults, + q, + page, + brand, + 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; + + // Variable for props state + const [activeFilter, setActiveFilter] = useState(false); + const [filterCount, setFilterCount] = useState(0); + + const route = () => { + let route = `/shop/search?q=${q}`; + if (brand) route += `&brand=${brand}`; + if (category) route += `&category=${category}`; + if (price_from) route += `&price_from=${price_from}`; + if (price_to) route += `&price_to=${price_to}`; + if (order_by) route += `&order_by=${order_by}`; + return route; + } + + useEffect(() => { + let calculateFilterCount = 0; + if (brand) calculateFilterCount++; + if (category) calculateFilterCount++; + if (price_from || price_to) calculateFilterCount++; + if (order_by) calculateFilterCount++; + setFilterCount(calculateFilterCount); + }, [brand, category, price_from, price_to, order_by]); + + return ( + <> +
+ setActiveFilter(false)} + defaultPriceFrom={price_from} + defaultPriceTo={price_to} + defaultBrand={brand} + defaultCategory={category} + defaultOrderBy={order_by} + searchResults={searchResults} + /> + +
+

Produk

+
+ {productFound > 0 ? ( + <> + Menampilkan  + {pageCount > 1 ? ( + <> + {productStart + 1}-{ + (productStart + productRows) > productFound ? productFound : productStart + productRows + } +  dari  + + ) : ''} + {searchResults.response.numFound} +  produk { q != '*' && (<>untuk pencarian {q}) } + + ) : 'Mungkin yang anda cari'} +
+ +
+ {searchResults.response.products.map((product) => ( + + ))} +
+ +
+ +
+
+ +