Skip to content

Commit 138d9f2

Browse files
adding sorting functionality(desc according to new ruleJSON Server's behavior for sorting changed with different versions.? _sort=-price)
1 parent a92b677 commit 138d9f2

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/features/product/components/ProductList.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import {
1818
} from "@heroicons/react/20/solid";
1919

2020
const 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

2826
const 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
))}

src/features/product/productAPI.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ export function fetchAllProducts() {
99
export function fetchProductsByFilters(filter) {
1010
// filter ={"category":"frangrances"}
1111
// filter ={"brand":"Essence"}
12+
// TODO:we will on server support mutilple value
13+
14+
// reference for sorting functionality
15+
// JSON Server's behavior for sorting changed with different versions.
16+
17+
// To resolve the issue, update JSON Server using the command:
18+
19+
// npm install -g json-server
20+
21+
// After updating, sorting by default will be ascending. No need to use _order:asc
22+
23+
// GET /products?_sort=price
24+
25+
// For descending order,
26+
27+
// ***** use GET /products?_sort=-price ****
1228
let queryString = "";
1329
for (let key in filter) {
1430
queryString += `${key}=${filter[key]}&`;

0 commit comments

Comments
 (0)