Use sortBy for search products (#997)

This commit is contained in:
Dawid Tarasiuk 2021-03-11 11:40:58 +01:00 committed by GitHub
parent a7736e2bf9
commit bf9b7c5120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View file

@ -23,6 +23,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix no channels crash - #984 by @dominik-zeglen
- Update webhooks - #982 by @piotrgrundas
- Fix trigger form change when collections are being added to list of product collections - #987 by @gax97
- Use default sort for search products list - #997 by @orzechdev
# 2.11.1

View file

@ -48,7 +48,8 @@ export enum ProductListUrlSortField {
name = "name",
productType = "productType",
status = "status",
price = "price"
price = "price",
rank = "rank"
}
export type ProductListUrlSort = Sort<ProductListUrlSortField>;
export interface ProductListUrlQueryParams

View file

@ -53,7 +53,7 @@ import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandl
import createFilterHandlers from "@saleor/utils/handlers/filterHandlers";
import { getSortUrlVariables } from "@saleor/utils/sort";
import { useWarehouseList } from "@saleor/warehouses/queries";
import React from "react";
import React, { useEffect } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import ProductListPage from "../../components/ProductListPage";
@ -192,6 +192,21 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
params
});
useEffect(() => {
const sortWithQuery = ProductListUrlSortField.rank;
const sortWithoutQuery =
params.sort === ProductListUrlSortField.rank
? ProductListUrlSortField.name
: params.sort;
navigate(
productListUrl({
...params,
asc: params.query ? undefined : params.asc,
sort: params.query ? sortWithQuery : sortWithoutQuery
})
);
}, [params.query]);
const handleTabChange = (tab: number) => {
reset();
navigate(

View file

@ -17,6 +17,8 @@ export function getSortQueryField(
return ProductOrderField.TYPE;
case ProductListUrlSortField.status:
return ProductOrderField.PUBLISHED;
case ProductListUrlSortField.rank:
return ProductOrderField.RANK;
default:
return undefined;
}
@ -26,15 +28,17 @@ export function getSortQueryVariables(
params: ProductListUrlQueryParams,
channel: string
): ProductOrder {
const direction = getOrderDirection(params.asc);
if (params.sort === ProductListUrlSortField.attribute) {
return {
attributeId: params.attributeId,
direction: getOrderDirection(params.asc)
direction
};
}
const field = getSortQueryField(params.sort);
return {
channel,
direction: getOrderDirection(params.asc),
field: getSortQueryField(params.sort)
direction,
field
};
}