saleor: fix product sorting

This commit is contained in:
Zaiste 2021-05-13 13:33:21 +02:00
parent f5ec8e17a4
commit 51e2daebb7
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
5 changed files with 19 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import getSortVariables from './get-sort-variables' import { getSortVariables } from './get-sort-variables'
import type { SearchProductsInput } from '../product/use-search' import type { SearchProductsInput } from '../product/use-search'
export const getSearchVariables = ({ export const getSearchVariables = ({
@ -7,10 +7,11 @@ export const getSearchVariables = ({
categoryId, categoryId,
sort, sort,
}: SearchProductsInput) => { }: SearchProductsInput) => {
const sortBy = { direction: 'ASC', ...getSortVariables(sort, !!categoryId), channel: "default-channel"};
return { return {
categoryId, categoryId,
filter: { search }, filter: { search },
...getSortVariables(sort, !!categoryId), sortBy
} }
} }

View File

@ -1,32 +1,31 @@
const getSortVariables = (sort?: string, isCategory: boolean = false) => { export const getSortVariables = (sort?: string, isCategory: boolean = false) => {
let output = {} let output = {}
switch (sort) { switch (sort) {
case 'price-asc': case 'price-asc':
output = { output = {
sortKey: 'PRICE', field: 'PRICE',
reverse: false, direction: 'ASC'
} }
break break
case 'price-desc': case 'price-desc':
output = { output = {
sortKey: 'PRICE', field: 'PRICE',
reverse: true, direction: 'DESC',
} }
break break
case 'trending-desc': case 'trending-desc':
output = { output = {
sortKey: 'BEST_SELLING', field: 'RANK',
reverse: false, direction: 'DESC',
} }
break break
case 'latest-desc': case 'latest-desc':
output = { output = {
sortKey: isCategory ? 'CREATED' : 'CREATED_AT', field: 'DATE',
reverse: true, direction: 'DESC'
} }
break break
} }
return output return output
} }
export default getSortVariables

View File

@ -1,6 +1,7 @@
export { getSortVariables } from './get-sort-variables'
export { default as handleFetchResponse } from './handle-fetch-response' export { default as handleFetchResponse } from './handle-fetch-response'
export { default as getSearchVariables } from './get-search-variables' 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 getVendors } from './get-vendors'
export { default as getCategories } from './get-categories' export { default as getCategories } from './get-categories'
export { default as getCheckoutId } from './get-checkout-id' export { default as getCheckoutId } from './get-checkout-id'

View File

@ -1,5 +1,5 @@
export const productConnection = /* GraphQL */ ` export const productConnection = /* GraphQL */ `
fragment productConnnection on ProductCountableConnection { fragment productConnection on ProductCountableConnection {
pageInfo { pageInfo {
hasNextPage hasNextPage
hasPreviousPage hasPreviousPage
@ -32,10 +32,11 @@ export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts( query getAllProducts(
$first: Int = 100 $first: Int = 100
$filter: ProductFilterInput $filter: ProductFilterInput
$sortBy: ProductOrder
$channel: String = "default-channel" $channel: String = "default-channel"
) { ) {
products(first: $first, channel: $channel, filter: $filter) { products(first: $first, channel: $channel, filter: $filter, sortBy: $sortBy) {
...productConnnection ...productConnection
} }
} }
${productConnection} ${productConnection}

View File

@ -48,10 +48,7 @@ export async function getStaticProps({
} }
} }
export default function Search({ export default function Search({ categories, brands }: InferGetStaticPropsType<typeof getStaticProps>) {
categories,
brands,
}: InferGetStaticPropsType<typeof getStaticProps>) {
const [activeFilter, setActiveFilter] = useState('') const [activeFilter, setActiveFilter] = useState('')
const [toggleFilter, setToggleFilter] = useState(false) const [toggleFilter, setToggleFilter] = useState(false)