cleanup, add sorting

This commit is contained in:
Greg Hoskin
2021-04-25 14:20:58 -05:00
parent 6a9c6c3bca
commit a409c373c4
52 changed files with 74 additions and 618 deletions

View File

@@ -1,17 +1,16 @@
import { CollectionEdge } from '../schema'
import { getConfig, SwellConfig } from '../api'
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
const getAllCollections = async (options?: {
variables?: any
config: SwellConfig
preview?: boolean
}) => {
let { config, variables = { first: 250 } } = options ?? {}
let { config, variables = { limit: 25 } } = options ?? {}
config = getConfig(config)
const { data } = await config.fetch(getAllCollectionsQuery, { variables })
const edges = data.collections?.edges ?? []
const response = await config.fetchSwell('categories', 'list', { variables })
const edges = response.results ?? []
const categories = edges.map(
({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({

View File

@@ -2,7 +2,6 @@ import { Product } from '@commerce/types'
import { getConfig, SwellConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products'
import { ProductEdge } from '../schema'
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
type ProductPath = {
path: string
@@ -21,17 +20,18 @@ const getAllProductPaths = async (options?: {
config?: SwellConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
let { config, variables = { limit: 100 } } = options ?? {}
config = getConfig(config)
const products = await fetchAllProducts({
config,
query: getAllProductsPathsQuery,
query: 'products',
method: 'list',
variables,
})
return {
products: products?.map(({ node: { handle } }: ProductEdge) => ({
products: products?.map(({ slug: handle }) => ({
node: {
path: `/${handle}`,
},

View File

@@ -1,7 +1,4 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SwellConfig } from '../api'
import { ProductEdge } from '../schema'
import { getAllProductsQuery } from '../utils/queries'
import { normalizeProduct } from '../utils/normalize'
import { Product } from '@commerce/types'

View File

@@ -22,7 +22,6 @@ const getProduct = async (options: {
if (product.variants) {
product.variants = product.variants?.results
}
// console.log('product', product)
return {
product: normalizeProduct(product),
}

View File

@@ -1,7 +1,7 @@
import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { getAllProductsQuery, normalizeProduct } from '../utils'
import { normalizeProduct } from '../utils'
import { Product } from '@commerce/types'
@@ -25,14 +25,22 @@ export const handler: SWRHook<
SearchProductsInput
> = {
fetchOptions: {
query: getAllProductsQuery,
query: 'products', // String(Math.random()),
method: 'list',
},
async fetcher({ input, options, fetch }) {
const { categoryId, search } = input
const sortMap = new Map([
['latest-desc', ''],
['price-asc', 'price_asc'],
['price-desc', 'price_desc'],
['trending-desc', 'popularity'],
])
const { categoryId, search, sort = 'latest-desc' } = input
const mappedSort = sortMap.get(sort)
const { results, count: found } = await fetch({
query: 'products',
method: 'list',
variables: { category: categoryId, search },
variables: { category: categoryId, search, sort: mappedSort },
})
const products = results.map((product) => {