Add category filter to manage products
This commit is contained in:
@@ -29,6 +29,7 @@ export const ManageProductsScreen = () => {
|
|||||||
const categories = useCategories();
|
const categories = useCategories();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
|
const [selectedCategory, setSelectedCategory] = useState<string>('all');
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const [category, setCategory] = useState('');
|
const [category, setCategory] = useState('');
|
||||||
const [barcode, setBarcode] = useState('');
|
const [barcode, setBarcode] = useState('');
|
||||||
@@ -139,12 +140,22 @@ export const ManageProductsScreen = () => {
|
|||||||
}
|
}
|
||||||
}, [category, categoryOptions]);
|
}, [category, categoryOptions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedCategory !== 'all' && !categoryOptions.includes(selectedCategory)) {
|
||||||
|
setSelectedCategory('all');
|
||||||
|
}
|
||||||
|
}, [categoryOptions, selectedCategory]);
|
||||||
|
|
||||||
const filtered = useMemo(
|
const filtered = useMemo(
|
||||||
() =>
|
() =>
|
||||||
products.filter((p) =>
|
products.filter((p) => {
|
||||||
`${p.name} ${p.category}`.toLowerCase().includes(search.toLowerCase()),
|
const matchesSearch = `${p.name} ${p.category}`
|
||||||
),
|
.toLowerCase()
|
||||||
[products, search],
|
.includes(search.toLowerCase());
|
||||||
|
const matchesCategory = selectedCategory === 'all' || p.category === selectedCategory;
|
||||||
|
return matchesSearch && matchesCategory;
|
||||||
|
}),
|
||||||
|
[products, search, selectedCategory],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -240,12 +251,29 @@ export const ManageProductsScreen = () => {
|
|||||||
<Button component={RouterLink} to="/categories" variant="outlined" sx={{ alignSelf: 'flex-start' }}>
|
<Button component={RouterLink} to="/categories" variant="outlined" sx={{ alignSelf: 'flex-start' }}>
|
||||||
Edit Categories
|
Edit Categories
|
||||||
</Button>
|
</Button>
|
||||||
|
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}>
|
||||||
<TextField
|
<TextField
|
||||||
placeholder="Search"
|
placeholder="Search"
|
||||||
value={search}
|
value={search}
|
||||||
onChange={(event) => setSearch(event.target.value)}
|
onChange={(event) => setSearch(event.target.value)}
|
||||||
InputProps={{ startAdornment: <InputAdornment position="start">{<SearchIcon />}</InputAdornment> }}
|
InputProps={{ startAdornment: <InputAdornment position="start">{<SearchIcon />}</InputAdornment> }}
|
||||||
|
fullWidth
|
||||||
/>
|
/>
|
||||||
|
<TextField
|
||||||
|
select
|
||||||
|
label="Category"
|
||||||
|
value={selectedCategory}
|
||||||
|
onChange={(event) => setSelectedCategory(event.target.value)}
|
||||||
|
sx={{ minWidth: { sm: 180 } }}
|
||||||
|
>
|
||||||
|
<MenuItem value="all">All categories</MenuItem>
|
||||||
|
{categoryOptions.map((cat) => (
|
||||||
|
<MenuItem key={cat} value={cat}>
|
||||||
|
{cat}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</TextField>
|
||||||
|
</Stack>
|
||||||
<Stack spacing={1}>
|
<Stack spacing={1}>
|
||||||
<Typography variant="subtitle1">Add Product</Typography>
|
<Typography variant="subtitle1">Add Product</Typography>
|
||||||
<TextField
|
<TextField
|
||||||
|
|||||||
Reference in New Issue
Block a user