Skip to content

Commit

Permalink
feat(index.jsx): add search and filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadj-Said-Bouras committed Nov 28, 2023
1 parent 0e95fce commit d6d9744
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions src/pages/products/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,62 @@ export default function ProductsPage() {
const {
register,
handleSubmit,
setValue,
formState: { errors, isSubmitting },
} = useForm();

const [categoryFilter, setCategoryFilter] = useState(null);
const [keywordFilter, setKeywordFilter] = useState("");
const [locationFilter, setLocationFilter] = useState("");
const [items, setItems] = useState([]);
const [filteredItems, setFilteredItems] = useState([]);

const onSubmit = (data) => {
setKeywordFilter(data.keyword || "");
setLocationFilter(data.location || "");
};

useEffect(() => {
async function getItems() {
let items = await getAllItems("items");
setItems(items);
}
getItems();
}, []);

useEffect(() => {
// Function to filter items based on category, keyword, and location
const filterItems = () => {
const filtered = items.filter((item) => {
return (
(!categoryFilter || item.category === categoryFilter) &&
(!keywordFilter ||
(item.name &&
item.name
.toLowerCase()
.includes(keywordFilter.toLowerCase())) ||
(item.description &&
item.description
.toLowerCase()
.includes(keywordFilter.toLowerCase()))) &&
(!locationFilter ||
(item.location &&
item.location
.toLowerCase()
.includes(locationFilter.toLowerCase())))
);
});
setFilteredItems(filtered);
};

filterItems();
}, [items, categoryFilter, keywordFilter, locationFilter]);

const handleKeywordChange = (value) => {
setKeywordFilter(value);
setValue("keyword", value);
};

return (
<div className='my-32'>
<div className='my-28'>
Expand All @@ -53,35 +90,24 @@ export default function ProductsPage() {
</div>
<form onSubmit={handleSubmit(onSubmit)}>
<div className='flex flex-row justify-start gap-5 items-center'>
<SearchBar
requiredMessage={false}
register={register}
errors={errors}
validation={{
maxLength: {
value: 30,
message: "30 character max",
},
}}
queryParams={{}}
/>

{/* <Input
<Input
className='w-1/3 -ml-5'
name='keyword'
type='text'
labelText=''
placeholder='ex: keyboard'
placeholder='Search by name or description'
requiredMessage={false}
register={register}
onChange={(e) => handleKeywordChange(e.target.value)}
value={keywordFilter}
errors={errors}
validation={{
maxLength: {
value: 30,
message: "30 character max",
value: 15,
message: "15 character max",
},
}}
/> */}
/>
<Input
className='w-1/5 rounded-xl'
name='location'
Expand Down Expand Up @@ -110,29 +136,9 @@ export default function ProductsPage() {
</div>
</form>
<div className='flex flex-wrap justify-between w-full mt-5'>
{
items &&
items.map((item) => (
<ItemCard key={item.id} item={item} />
))
// .filter((item) => {
// return (
// (!categoryFilter ||
// item.category === categoryFilter) &&
// (!keywordFilter ||
// item.name
// .toLowerCase()
// .includes(
// keywordFilter.toLowerCase(),
// )) &&
// (!locationFilter ||
// item.location
// .toLowerCase()
// .includes(locationFilter.toLowerCase()))
// );
// })
//.map((item) => <ItemCard key={item.id} {...item} />)}
}
{filteredItems.map((item) => (
<ItemCard key={item.id} item={item} />
))}
</div>
</div>
);
Expand Down

1 comment on commit d6d9744

@vercel
Copy link

@vercel vercel bot commented on d6d9744 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.