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'
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
}
}

View File

@ -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

View File

@ -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'

View File

@ -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}

View File

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