Commit 9d289af2 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

create algorithm for show products in home page

parent 43419890
...@@ -27,22 +27,38 @@ export default function Home() { ...@@ -27,22 +27,38 @@ export default function Home() {
const cart = useContext(CartContext); const cart = useContext(CartContext);
const [category, setCategory] = useState(-1); const [category, setCategory] = useState(-1);
const [categoryList, setCategoryList] = useState([]); const [categoryList, setCategoryList] = useState([]);
const [productsFilter, setProductsFilter] = useState([]);
const [algorithm, setAlgorithm] = useState([]);
const productsFilter = useEffect(() => {
!!router.query?.q && router.query?.q?.length let product_cache =
? products.filter( !!router.query?.q && router.query?.q?.length
(prod) => ? products.filter(
(category === -1 || category === prod.cateId) && (prod) =>
String(prod.name) (category === -1 || category === prod.cateId) &&
.toLocaleLowerCase() String(prod.name)
.includes(String(router.query.q).toLocaleLowerCase()) && .toLocaleLowerCase()
Number(prod.stock) > 0 .includes(String(router.query.q).toLocaleLowerCase()) &&
) Number(prod.stock) > 0
: products.filter( )
(prod) => : products.filter(
(category === -1 || category === prod.cateId) && (prod) =>
Number(prod.stock) > 0 (category === -1 || category === prod.cateId) &&
); Number(prod.stock) > 0
);
setProductsFilter(product_cache);
if (!algorithm.length) {
let seed = [...Array(products.length).keys()];
let productRandom = [];
for (let i = 0; i < seed.length; i++) {
let point = Math.floor(Math.random() * seed.length);
productRandom.push(seed[point]);
seed.slice(point, 1);
}
setAlgorithm(productRandom);
}
}, [products, category]);
async function onCart(id, isRemove = false) { async function onCart(id, isRemove = false) {
if (!user.value?.token) { if (!user.value?.token) {
...@@ -111,13 +127,15 @@ export default function Home() { ...@@ -111,13 +127,15 @@ export default function Home() {
}; };
useEffect(() => { useEffect(() => {
if (!user.value?.token) return;
fetchCategory(); fetchCategory();
FetchProduct(); FetchProduct();
}, [user]); }, [user]);
useEffect(() => { useEffect(() => {
if (router.query?.cat) { if (router.query?.cat) {
setCategory(Number(router.query.cat)); setCategory(Number(router.query.cat));
} else { } else if (category !== -1) {
setCategory(-1); setCategory(-1);
} }
}, [router]); }, [router]);
...@@ -133,7 +151,7 @@ export default function Home() { ...@@ -133,7 +151,7 @@ export default function Home() {
/> />
<div> <div>
<Box className="flex justify-start mb-3 px-10 max-w-[1520px] mx-auto"> <Box className="flex justify-start mb-3 px-10 max-w-[1520px] mx-auto">
<Box sx={{ display: "flex", overflowX: "scroll", maxWidth: "100vw" }}> <Box sx={{ display: "flex", overflowX: "scroll", maxWidth: "100vw" }}>
<Button <Button
variant={category === -1 ? "contained" : "text"} variant={category === -1 ? "contained" : "text"}
className={`${category === -1 ? "" : "bg-white"} mx-1`} className={`${category === -1 ? "" : "bg-white"} mx-1`}
...@@ -177,37 +195,45 @@ export default function Home() { ...@@ -177,37 +195,45 @@ export default function Home() {
{productsFilter.length ? ( {productsFilter.length ? (
<div className="mx-auto text-left grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 2xl:grid-cols-4 max-w-[1520px]"> <div className="mx-auto text-left grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 2xl:grid-cols-4 max-w-[1520px]">
{productsFilter.map((prod, idx) => ( {algorithm.map(
<ProductCard (algor, idx) =>
key={idx} !!productsFilter[algor]?.id && (
isFav={ <ProductCard
!!wishlist.value.filter( key={idx}
(wish) => wish.product_id === Number(prod.id) isFav={
).length !!wishlist.value.filter(
} (wish) =>
isCart={ wish.product_id === Number(productsFilter[algor].id)
!!cart.value.filter((ct) => ct.product_id === Number(prod.id)) ).length
.length }
} isCart={
product={prod} !!cart.value.filter(
cartHandler={() => (ct) =>
onCart( ct.product_id === Number(productsFilter[algor].id)
prod.id, ).length
!!cart.value.filter( }
(ct) => ct.product_id === Number(prod.id) product={productsFilter[algor]}
).length cartHandler={() =>
) onCart(
} prod.id,
favHandler={() => !!cart.value.filter(
onWishlist( (ct) =>
prod.id, ct.product_id === Number(productsFilter[algor].id)
!!wishlist.value.filter( ).length
(wish) => wish.product_id === Number(prod.id) )
).length }
) favHandler={() =>
} onWishlist(
/> productsFilter[algor].id,
))} !!wishlist.value.filter(
(wish) =>
wish.product_id === Number(productsFilter[algor].id)
).length
)
}
/>
)
)}
</div> </div>
) : ( ) : (
<Paper sx={{ p: 2, textAlign: "center" }}>ไม่พบรายการ</Paper> <Paper sx={{ p: 2, textAlign: "center" }}>ไม่พบรายการ</Paper>
......
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