Commit 3c4db1d5 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

optimize search products;

change cat_id in url to cat_name because id in mongodb is too long for
url
parent 5afb5a92
...@@ -83,6 +83,8 @@ function Navbar(props) { ...@@ -83,6 +83,8 @@ function Navbar(props) {
const isMobileMenuOpen = Boolean(mobileMoreAnchorEl); const isMobileMenuOpen = Boolean(mobileMoreAnchorEl);
function onUpdateSearch(value) { function onUpdateSearch(value) {
setSearch(value);
// return
let query = router.query; let query = router.query;
if (value) { if (value) {
query.q = value; query.q = value;
...@@ -97,7 +99,6 @@ function Navbar(props) { ...@@ -97,7 +99,6 @@ function Navbar(props) {
React.useEffect(() => { React.useEffect(() => {
setSearch(router.query?.q ?? ""); setSearch(router.query?.q ?? "");
console.log(router.query);
}, [router]); }, [router]);
const handleMobileMenuClose = () => { const handleMobileMenuClose = () => {
......
...@@ -26,7 +26,7 @@ export default function Home() { ...@@ -26,7 +26,7 @@ export default function Home() {
const [message, setMessage] = useState({ message: "", error: false }); const [message, setMessage] = useState({ message: "", error: false });
const wishlist = useContext(WishlistContext); const wishlist = useContext(WishlistContext);
const cart = useContext(CartContext); const cart = useContext(CartContext);
const [category, setCategory] = useState(-1); const [category, setCategory] = useState("");
const [categoryList, setCategoryList] = useState([]); const [categoryList, setCategoryList] = useState([]);
const [productsFilter, setProductsFilter] = useState([]); const [productsFilter, setProductsFilter] = useState([]);
const [algorithm, setAlgorithm] = useState([]); const [algorithm, setAlgorithm] = useState([]);
...@@ -47,11 +47,14 @@ export default function Home() { ...@@ -47,11 +47,14 @@ export default function Home() {
useEffect(() => { useEffect(() => {
// products filter for search and category // products filter for search and category
let cat_id = category.length
? categoryList.filter((c) => c.name == category)[0]?.id ?? ""
: "";
let product_cache = let product_cache =
!!router.query?.q && router.query?.q?.length !!router.query?.q && router.query?.q?.length
? algorithm.filter( ? algorithm.filter(
(pid) => (pid) =>
(category === -1 || category === products[pid].cateId) && (category.length === 0 || cat_id === products[pid].cateId) &&
String(products[pid].name) String(products[pid].name)
.toLocaleLowerCase() .toLocaleLowerCase()
.includes(String(router.query.q).toLocaleLowerCase()) && .includes(String(router.query.q).toLocaleLowerCase()) &&
...@@ -59,7 +62,7 @@ export default function Home() { ...@@ -59,7 +62,7 @@ export default function Home() {
) )
: algorithm.filter( : algorithm.filter(
(pid) => (pid) =>
(category === -1 || category === products[pid]?.cateId) && (category.length === 0 || cat_id === products[pid]?.cateId) &&
Number(products[pid].stock) > 0 Number(products[pid].stock) > 0
); );
setProductsFilter(product_cache); setProductsFilter(product_cache);
...@@ -144,7 +147,7 @@ export default function Home() { ...@@ -144,7 +147,7 @@ export default function Home() {
if (router.query?.cat) { if (router.query?.cat) {
setCategory(router.query.cat); setCategory(router.query.cat);
} else if (category !== -1) { } else if (category !== -1) {
setCategory(-1); setCategory("");
} }
}, [router]); }, [router]);
...@@ -162,8 +165,8 @@ export default function Home() { ...@@ -162,8 +165,8 @@ export default function Home() {
<Box className="flex justify-start mb-3 sm:px-10 max-w-[1520px] mx-auto"> <Box className="flex justify-start mb-3 sm:px-10 max-w-[1520px] mx-auto">
<Box sx={{ display: "flex", overflowX: "scroll" }}> <Box sx={{ display: "flex", overflowX: "scroll" }}>
<Button <Button
variant={category === -1 ? "contained" : "text"} variant={category.length === 0 ? "contained" : "text"}
className={`${category === -1 ? "" : "bg-white"} mx-1`} className={`${category.length === 0 ? "" : "bg-white"} mx-1`}
onClick={() => { onClick={() => {
let que = router.query; let que = router.query;
delete que.cat; delete que.cat;
...@@ -173,15 +176,15 @@ export default function Home() { ...@@ -173,15 +176,15 @@ export default function Home() {
}); });
}} }}
> >
ทั้งหมด{category === -1 ? `(${productsFilter.length})` : ""} ทั้งหมด{category.length === 0 ? `(${productsFilter.length})` : ""}
</Button> </Button>
{categoryList.map((cat, idx) => ( {categoryList.map((cat, idx) => (
<Button <Button
className={`${cat.id === category ? "" : "bg-white"} mx-1`} className={`${cat.name === category ? "" : "bg-white"} mx-1`}
variant={cat.id === category ? "contained" : "text"} variant={cat.name === category ? "contained" : "text"}
key={idx} key={idx}
onClick={() => { onClick={() => {
if (cat.id === category) { if (cat.name === category) {
let que = router.query; let que = router.query;
delete que.cat; delete que.cat;
router.push({ router.push({
...@@ -191,7 +194,7 @@ export default function Home() { ...@@ -191,7 +194,7 @@ export default function Home() {
} else { } else {
router.push({ router.push({
pathname: location.pathname, pathname: location.pathname,
query: { ...router.query, cat: cat.id }, query: { ...router.query, cat: cat.name },
}); });
} }
}} }}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment