11import React , { useState , Fragment , useEffect } from "react" ;
22import { useSelector , useDispatch } from "react-redux" ;
3- import { fetchAllProductsAsync , selectAllProducts } from "../productSlice" ;
3+ import {
4+ fetchAllProductsAsync ,
5+ fetchProductsByFiltersAsync ,
6+ selectAllProducts ,
7+ } from "../productSlice" ;
48import { Dialog , Disclosure , Menu , Transition } from "@headlessui/react" ;
59import { StarIcon , XMarkIcon } from "@heroicons/react/24/outline" ;
610import { ChevronLeftIcon , ChevronRightIcon } from "@heroicons/react/20/solid" ;
@@ -22,52 +26,36 @@ const sortOptions = [
2226] ;
2327
2428const filters = [
29+ {
30+ id : "category" ,
31+ name : "Category" ,
32+ options : [
33+ { value : "beauty" , label : "Beauty" , checked : false } ,
34+ { value : "fragrances" , label : "Fragrances" , checked : false } ,
35+ { value : "furniture" , label : "Furniture" , checked : false } ,
36+ { value : "groceries" , label : "Groceries" , checked : false } ,
37+ ] ,
38+ } ,
2539 {
2640 id : "brand" ,
2741 name : "Brands" ,
2842 options : [
2943 { value : "Essence" , label : "Essence" , checked : false } ,
30-
3144 { value : "Glamour Beauty" , label : "Glamour Beauty" , checked : false } ,
32-
3345 { value : "Velvet Touch" , label : "Velvet Touch" , checked : false } ,
34-
3546 { value : "Chic Cosmetics" , label : "Chic Cosmetics" , checked : false } ,
36-
3747 { value : "Nail Couture" , label : "Nail Couture" , checked : false } ,
38-
3948 { value : "Calvin Klein" , label : "Calvin Klein" , checked : false } ,
40-
4149 { value : "Chanel" , label : "Chanel" , checked : false } ,
42-
4350 { value : "Dior" , label : "Dior" , checked : false } ,
44-
4551 { value : "Dolce & Gabbana" , label : "Dolce & Gabbana" , checked : false } ,
46-
4752 { value : "Gucci" , label : "Gucci" , checked : false } ,
48-
4953 { value : "Annibale Colombo" , label : "Annibale Colombo" , checked : false } ,
50-
5154 { value : "Furniture Co." , label : "Furniture Co." , checked : false } ,
52-
5355 { value : "Knoll" , label : "Knoll" , checked : false } ,
54-
5556 { value : "Bath Trends" , label : "Bath Trends" , checked : false } ,
5657 ] ,
5758 } ,
58- {
59- id : "category" ,
60- name : "Category" ,
61- options : [
62- { value : "beauty" , label : "Beauty" , checked : false } ,
63-
64- { value : "fragrances" , label : "Fragrances" , checked : false } ,
65-
66- { value : "furniture" , label : "Furniture" , checked : false } ,
67-
68- { value : "groceries" , label : "Groceries" , checked : false } ,
69- ] ,
70- } ,
7159] ;
7260
7361function classNames ( ...classes ) {
@@ -78,10 +66,18 @@ export default function ProductList() {
7866 const [ mobileFiltersOpen , setMobileFiltersOpen ] = useState ( false ) ;
7967 const products = useSelector ( selectAllProducts ) ;
8068 const dispatch = useDispatch ( ) ;
69+ const [ filter , setFilter ] = useState ( { } ) ;
70+ const handleFilter = ( e , section , option ) => {
71+ const newFilter = { ...filter , [ section . id ] : option . value } ;
72+ setFilter ( newFilter ) ;
73+ dispatch ( fetchProductsByFiltersAsync ( newFilter ) ) ;
74+ console . log ( section . id , option . value ) ;
75+ } ;
8176
8277 useEffect ( ( ) => {
8378 dispatch ( fetchAllProductsAsync ( ) ) ;
8479 } , [ dispatch ] ) ;
80+
8581 return (
8682 < div className = "bg-white" >
8783 < div >
@@ -314,6 +310,9 @@ export default function ProductList() {
314310 defaultValue = { option . value }
315311 type = "checkbox"
316312 defaultChecked = { option . checked }
313+ onChange = { ( e ) => {
314+ handleFilter ( e , section , option ) ;
315+ } }
317316 className = "h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
318317 />
319318 < label
0 commit comments