@@ -18,11 +18,9 @@ import {
1818} from "@heroicons/react/20/solid" ;
1919
2020const sortOptions = [
21- { name : "Most Popular" , href : "#" , current : true } ,
22- { name : "Best Rating" , href : "#" , current : false } ,
23- { name : "Newest" , href : "#" , current : false } ,
24- { name : "Price: Low to High" , href : "#" , current : false } ,
25- { name : "Price: High to Low" , href : "#" , current : false } ,
21+ { name : "Best Rating" , sort : "rating" , current : false } ,
22+ { name : "Price: Low to High" , sort : "price" , order : "asc" , current : false } ,
23+ { name : "Price: High to Low" , sort : "price" , order : "desc" , current : false } ,
2624] ;
2725
2826const filters = [
@@ -67,11 +65,19 @@ export default function ProductList() {
6765 const products = useSelector ( selectAllProducts ) ;
6866 const dispatch = useDispatch ( ) ;
6967 const [ filter , setFilter ] = useState ( { } ) ;
68+
7069 const handleFilter = ( e , section , option ) => {
7170 const newFilter = { ...filter , [ section . id ] : option . value } ;
7271 setFilter ( newFilter ) ;
7372 dispatch ( fetchProductsByFiltersAsync ( newFilter ) ) ;
74- console . log ( section . id , option . value ) ;
73+ } ;
74+ const handleSort = ( e , option ) => {
75+ const newFilter = {
76+ ...filter ,
77+ _sort : option . order === "desc" ? `-${ option . sort } ` : option . sort ,
78+ } ;
79+ setFilter ( newFilter ) ;
80+ dispatch ( fetchProductsByFiltersAsync ( newFilter ) ) ;
7581 } ;
7682
7783 useEffect ( ( ) => {
@@ -223,8 +229,10 @@ export default function ProductList() {
223229 { sortOptions . map ( ( option ) => (
224230 < Menu . Item key = { option . name } >
225231 { ( { active } ) => (
226- < a
227- href = { option . href }
232+ < p
233+ onClick = { ( e ) => {
234+ handleSort ( e , option ) ;
235+ } }
228236 className = { classNames (
229237 option . current
230238 ? "font-medium text-gray-900"
@@ -234,7 +242,7 @@ export default function ProductList() {
234242 ) }
235243 >
236244 { option . name }
237- </ a >
245+ </ p >
238246 ) }
239247 </ Menu . Item >
240248 ) ) }
0 commit comments