diff --git a/framework/saleor/utils/get-search-variables.ts b/framework/saleor/utils/get-search-variables.ts index 04f215e5a..163328d88 100644 --- a/framework/saleor/utils/get-search-variables.ts +++ b/framework/saleor/utils/get-search-variables.ts @@ -1,4 +1,4 @@ -import getSortVariables from './get-sort-variables' +import { getSortVariables } from './get-sort-variables' import type { SearchProductsInput } from '../product/use-search' export const getSearchVariables = ({ @@ -7,10 +7,11 @@ export const getSearchVariables = ({ categoryId, sort, }: SearchProductsInput) => { + const sortBy = { direction: 'ASC', ...getSortVariables(sort, !!categoryId), channel: "default-channel"}; return { categoryId, filter: { search }, - ...getSortVariables(sort, !!categoryId), + sortBy } } diff --git a/framework/saleor/utils/get-sort-variables.ts b/framework/saleor/utils/get-sort-variables.ts index 141d9a180..e29437f42 100644 --- a/framework/saleor/utils/get-sort-variables.ts +++ b/framework/saleor/utils/get-sort-variables.ts @@ -1,32 +1,31 @@ -const getSortVariables = (sort?: string, isCategory: boolean = false) => { +export const getSortVariables = (sort?: string, isCategory: boolean = false) => { let output = {} switch (sort) { case 'price-asc': output = { - sortKey: 'PRICE', - reverse: false, + field: 'PRICE', + direction: 'ASC' } break case 'price-desc': output = { - sortKey: 'PRICE', - reverse: true, + field: 'PRICE', + direction: 'DESC', } break case 'trending-desc': output = { - sortKey: 'BEST_SELLING', - reverse: false, + field: 'RANK', + direction: 'DESC', } break case 'latest-desc': output = { - sortKey: isCategory ? 'CREATED' : 'CREATED_AT', - reverse: true, + field: 'DATE', + direction: 'DESC' } break } return output } -export default getSortVariables diff --git a/framework/saleor/utils/index.ts b/framework/saleor/utils/index.ts index 3f80df3d2..9fd590d38 100644 --- a/framework/saleor/utils/index.ts +++ b/framework/saleor/utils/index.ts @@ -1,6 +1,7 @@ +export { getSortVariables } from './get-sort-variables' + export { default as handleFetchResponse } from './handle-fetch-response' export { default as getSearchVariables } from './get-search-variables' -export { default as getSortVariables } from './get-sort-variables' export { default as getVendors } from './get-vendors' export { default as getCategories } from './get-categories' export { default as getCheckoutId } from './get-checkout-id' diff --git a/framework/saleor/utils/queries/get-all-products-query.ts b/framework/saleor/utils/queries/get-all-products-query.ts index 36858228d..1c3981bf0 100644 --- a/framework/saleor/utils/queries/get-all-products-query.ts +++ b/framework/saleor/utils/queries/get-all-products-query.ts @@ -1,5 +1,5 @@ export const productConnection = /* GraphQL */ ` - fragment productConnnection on ProductCountableConnection { + fragment productConnection on ProductCountableConnection { pageInfo { hasNextPage hasPreviousPage @@ -32,10 +32,11 @@ export const getAllProductsQuery = /* GraphQL */ ` query getAllProducts( $first: Int = 100 $filter: ProductFilterInput + $sortBy: ProductOrder $channel: String = "default-channel" ) { - products(first: $first, channel: $channel, filter: $filter) { - ...productConnnection + products(first: $first, channel: $channel, filter: $filter, sortBy: $sortBy) { + ...productConnection } } ${productConnection} diff --git a/pages/search.tsx b/pages/search.tsx index c2121c0da..d8a502c11 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -48,10 +48,7 @@ export async function getStaticProps({ } } -export default function Search({ - categories, - brands, -}: InferGetStaticPropsType) { +export default function Search({ categories, brands }: InferGetStaticPropsType) { const [activeFilter, setActiveFilter] = useState('') const [toggleFilter, setToggleFilter] = useState(false)