Commit b732c9d0 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

add category filter on admin stock

parent e381489b
......@@ -28,10 +28,15 @@ export default function Stock() {
const [products, setProducts] = useState([]);
const [updateState, setUpdateState] = useState({ open: false, data: {} });
const [deleteState, setDeleteState] = useState({ open: false, id: -1 });
const [category, setCategory] = useState([]);
const [category, setCategory] = useState(-1);
const [categoryList, setCategoryList] = useState([]);
const productsFilter = !!router.query?.q
? products.filter((prod) => String(prod.name).includes(router.query.q))
? products.filter((prod) =>
String(prod.name)
.toLocaleLowerCase()
.includes(String(router.query.q).toLocaleLowerCase())
)
: products;
useEffect(() => {
......@@ -41,10 +46,18 @@ export default function Stock() {
}, [modal, updateState, deleteState]);
useEffect(() => {
axios.get("/api/category").then((res) => {
setCategory(res.data);
setCategoryList(res.data);
});
}, []);
useEffect(() => {
if (router.query?.cat) {
setCategory(Number(router.query.cat));
} else {
setCategory(-1);
}
}, [router]);
return (
<>
<Head>
......@@ -57,8 +70,54 @@ export default function Stock() {
เพิ่มสินค้า
</Button>
</Box>
<Box className="flex justify-start mb-3 px-10 max-w-[1520px] mx-auto">
<Box sx={{ display: "flex", overflowX: "scroll", maxWidth: "100vw" }}>
<Button
variant={category === -1 ? "contained" : "text"}
className={`${category === -1 ? "" : "bg-white"} mx-1`}
onClick={() => {
let que = router.query;
delete que.cat;
router.push({
pathname: location.pathname,
query: { ...que },
});
}}
>
ทั้งหมด
</Button>
{categoryList.map((cat, idx) => (
<Button
className={`${cat.id === category ? "" : "bg-white"} mx-1`}
variant={cat.id === category ? "contained" : "text"}
key={idx}
onClick={() => {
if (cat.id === category) {
let que = router.query;
delete que.cat;
router.push({
pathname: location.pathname,
query: { ...que },
});
} else {
router.push({
pathname: location.pathname,
query: { ...router.query, cat: cat.id },
});
}
}}
>
{cat.name}
</Button>
))}
</Box>
</Box>
<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((product, idx) => (
{productsFilter.map(
(product, idx) =>
(category === -1 || category === product.cateId) && (
<Card
key={idx}
sx={{
......@@ -71,7 +130,9 @@ export default function Stock() {
}}
>
<Box sx={{ px: 1 }}>
<h5 className="h-[40px] overflow-y-hidden">{product.name}</h5>
<h5 className="h-[40px] overflow-y-hidden">
{product.name}
</h5>
</Box>
<CardMedia
className="rounded object-contain"
......@@ -93,10 +154,15 @@ export default function Stock() {
<TableCell sx={{ m: 0, p: 0, textAlign: "center " }}>
{Number(product.price).toLocaleString() + " บาท"}
</TableCell>
<TableCell> {`ส่วนลด ${product.discount}%`}</TableCell>
<TableCell>
{" "}
{`ส่วนลด ${product.discount}%`}
</TableCell>
</TableRow>
<TableRow>
<TableCell>เข้าชม {product.watch_count} ครั้ง</TableCell>
<TableCell>
เข้าชม {product.watch_count} ครั้ง
</TableCell>
<TableCell>คงเหลือ {product.stock} ชิ้น</TableCell>
</TableRow>
</TableBody>
......@@ -105,19 +171,24 @@ export default function Stock() {
<CardActions disableSpacing sx={{ justifyContent: "end" }}>
<Button
color="warning"
onClick={() => setUpdateState({ open: true, data: product })}
onClick={() =>
setUpdateState({ open: true, data: product })
}
>
<Edit /> แก้ไข
</Button>
<Button
color="error"
onClick={() => setDeleteState({ open: true, id: product.id })}
onClick={() =>
setDeleteState({ open: true, id: product.id })
}
>
<Delete /> ลบ
</Button>
</CardActions>
</Card>
))}
)
)}
</div>
<AddProduct open={modal} handleClose={() => setModal(false)} />
</Box>
......
......@@ -133,26 +133,6 @@ export default function Home() {
/>
<div>
<Box className="flex justify-start mb-3 px-10 max-w-[1520px] mx-auto">
{/* <Box className="flex items-center mx-3">หมวดหมู่</Box> */}
{/* <FormControl>
<Select
variant="standard"
value={category}
onChange={(e) => {
router.push({
pathname: location.pathname,
query: { ...router.query, cat: e.target.value },
});
}}
>
<MenuItem value="-1">ทั้งหมด</MenuItem>
{categoryList.map((cat, idx) => (
<MenuItem key={idx} value={cat.id}>
{cat.name}
</MenuItem>
))}
</Select>
</FormControl> */}
<Box sx={{ display: "flex", overflowX: "scroll", maxWidth: "100vw" }}>
<Button
variant={category === -1 ? "contained" : "text"}
......
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