summaryrefslogtreecommitdiff
path: root/src/pages/shop/search.js
blob: 6e6839beeb364227b0869f72511531c108c6ae89 (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
import axios from "axios";
import Link from "next/link";
import { useEffect, useState } from "react";
import Header from "../../components/Header";
import ProductCard from "../../components/ProductCard";

export async function getServerSideProps(context) {
  const { q, page = 1 } = context.query;
  let searchResults = await axios(`http://${context.req.headers.host}/api/shop/search?q=${q}&page=${page}`);
  searchResults = searchResults.data;
  return { props: { searchResults, q } };
}

export default function ShopSearch({ searchResults, q, page = 1 }) {
  const pageTotal = Math.ceil(searchResults.response.numFound / searchResults.responseHeader.params.rows);

  return (
    <>
      <Header title={`Jual ${q} - Indoteknik`} />
      <main>
        <div className="p-4">
          <h1 className="mb-1">Produk</h1>
          <div className="text-xs mb-4">
            Menampilkan&nbsp;
            {searchResults.responseHeader.params.start + 1}-{searchResults.responseHeader.params.start + searchResults.responseHeader.params.rows}
            &nbsp;dari&nbsp;
            {searchResults.response.numFound}
            &nbsp;produk untuk pencarian {q}
          </div>
          <div className="grid grid-cols-2 gap-3">
            {searchResults.response.products.map((product) => (
              <ProductCard key={product.id} data={product} />
            ))}
          </div>
          <div className="flex justify-center gap-x-1 mt-4">
            {[...Array(pageTotal)].map((v, i) => (
              <Link href={`/shop/search?q=${q}&page=${i+1}`} key={i} className={"p-1 px-2 rounded ease-linear duration-150 " + (page == (i + 1) ? "bg-yellow-900 text-white" : "bg-gray-100 hover:bg-gray-300 text-gray-900")}>
                {i + 1}
              </Link>
            ))}
          </div>
        </div>
      </main>
    </>
  )
}