forked from crowetic/commerce
fixes after upstream changes
This commit is contained in:
parent
af201cccdc
commit
1b9ae58d37
@ -1,40 +1,4 @@
|
|||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
||||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
import type { ShopifyProvider } from '..'
|
||||||
import useCommerceCustomer from '@commerce/use-customer'
|
|
||||||
import getCustomerQuery from '@framework/utils/queries/get-customer-query'
|
|
||||||
import { getCustomerToken } from '@framework/utils/customer-token'
|
|
||||||
|
|
||||||
const defaultOpts = {
|
export default useCustomer as UseCustomer<ShopifyProvider>
|
||||||
query: getCustomerQuery,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fetcher: HookFetcher<any | null> = async (options, _, fetch) => {
|
|
||||||
const customerAccessToken = getCustomerToken()
|
|
||||||
if (customerAccessToken) {
|
|
||||||
const data = await fetch<any | null>({
|
|
||||||
...defaultOpts,
|
|
||||||
...options,
|
|
||||||
variables: { customerAccessToken },
|
|
||||||
})
|
|
||||||
return data?.customer ?? null
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(
|
|
||||||
customFetcher: typeof fetcher,
|
|
||||||
swrOptions?: SwrOptions<any | null>
|
|
||||||
) {
|
|
||||||
const useCustomer = () => {
|
|
||||||
return useCommerceCustomer(defaultOpts, [], customFetcher, {
|
|
||||||
revalidateOnFocus: false,
|
|
||||||
...swrOptions,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
useCustomer.extend = extendHook
|
|
||||||
|
|
||||||
return useCustomer
|
|
||||||
}
|
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -1,72 +1,4 @@
|
|||||||
import useCommerceSearch from '@commerce/products/use-search'
|
import useSearch, { UseSearch } from '@commerce/products/use-search'
|
||||||
import { getAllProductsQuery } from '@framework/utils/queries'
|
import type { ShopifyProvider } from '..'
|
||||||
|
|
||||||
import type { Product } from 'framework/bigcommerce/schema'
|
export default useSearch as UseSearch<ShopifyProvider>
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
|
||||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
|
||||||
import type { ProductConnection, ProductEdge } from '@framework/schema'
|
|
||||||
|
|
||||||
import getSearchVariables from '@framework/utils/get-search-variables'
|
|
||||||
|
|
||||||
import { normalizeProduct } from '@framework/lib/normalize'
|
|
||||||
|
|
||||||
export type SearchProductsInput = {
|
|
||||||
search?: string
|
|
||||||
categoryId?: string
|
|
||||||
brandId?: string
|
|
||||||
sort?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SearchRequestProductsData = {
|
|
||||||
products?: ProductEdge[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SearchProductsData = {
|
|
||||||
products: Product[]
|
|
||||||
found: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fetcher: HookFetcher<
|
|
||||||
SearchProductsData,
|
|
||||||
SearchProductsInput
|
|
||||||
> = async (options, input, fetch) => {
|
|
||||||
const resp = await fetch({
|
|
||||||
query: options?.query,
|
|
||||||
method: options?.method,
|
|
||||||
variables: getSearchVariables(input),
|
|
||||||
})
|
|
||||||
const edges = resp.products?.edges
|
|
||||||
return {
|
|
||||||
products: edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p)),
|
|
||||||
found: !!edges?.length,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(
|
|
||||||
customFetcher: typeof fetcher,
|
|
||||||
swrOptions?: SwrOptions<SearchProductsData, SearchProductsInput>
|
|
||||||
) {
|
|
||||||
const useSearch = (input: SearchProductsInput = {}) => {
|
|
||||||
const response = useCommerceSearch(
|
|
||||||
{
|
|
||||||
query: getAllProductsQuery,
|
|
||||||
},
|
|
||||||
[
|
|
||||||
['search', input.search],
|
|
||||||
['categoryId', input.categoryId],
|
|
||||||
['brandId', input.brandId],
|
|
||||||
['sort', input.sort],
|
|
||||||
],
|
|
||||||
customFetcher,
|
|
||||||
{ revalidateOnFocus: false, ...swrOptions }
|
|
||||||
)
|
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
|
|
||||||
useSearch.extend = extendHook
|
|
||||||
|
|
||||||
return useSearch
|
|
||||||
}
|
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -9,12 +9,23 @@ import {
|
|||||||
} from './const'
|
} from './const'
|
||||||
|
|
||||||
import { Cart } from './types'
|
import { Cart } from './types'
|
||||||
import { normalizeCart } from './lib/normalize'
|
import { Customer } from '@commerce/types'
|
||||||
import handleFetchResponse from './utils/handle-fetch-response'
|
import { normalizeCart, normalizeProduct } from './lib/normalize'
|
||||||
import getCheckoutQuery from './utils/queries/get-checkout-query'
|
import { FetchCartInput } from '@commerce/cart/use-cart'
|
||||||
import { FetchCartInput, UseCartInput } from '@commerce/cart/use-cart'
|
|
||||||
import { checkoutCreate, checkoutToCart } from './cart/utils'
|
import { checkoutCreate, checkoutToCart } from './cart/utils'
|
||||||
|
|
||||||
|
import {
|
||||||
|
getAllProductsQuery,
|
||||||
|
getCustomerQuery,
|
||||||
|
getCheckoutQuery,
|
||||||
|
handleFetchResponse,
|
||||||
|
getSearchVariables,
|
||||||
|
} from './utils'
|
||||||
|
|
||||||
|
import { ProductEdge } from './schema'
|
||||||
|
import { SearchProductsInput } from 'framework/bigcommerce/provider'
|
||||||
|
import { SearchProductsData } from 'framework/bigcommerce/api/catalog/products'
|
||||||
|
|
||||||
const fetcher: Fetcher = async ({ method = 'POST', variables, query }) => {
|
const fetcher: Fetcher = async ({ method = 'POST', variables, query }) => {
|
||||||
return handleFetchResponse(
|
return handleFetchResponse(
|
||||||
await fetch(API_URL, {
|
await fetch(API_URL, {
|
||||||
@ -55,16 +66,13 @@ export const cartFetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
|||||||
const useCart: HookHandler<
|
const useCart: HookHandler<
|
||||||
Cart | null,
|
Cart | null,
|
||||||
{},
|
{},
|
||||||
any,
|
FetchCartInput,
|
||||||
any,
|
|
||||||
any,
|
|
||||||
{ isEmpty?: boolean }
|
{ isEmpty?: boolean }
|
||||||
> = {
|
> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: getCheckoutQuery,
|
query: getCheckoutQuery,
|
||||||
},
|
},
|
||||||
fetcher: cartFetcher,
|
fetcher: cartFetcher,
|
||||||
normalizer: normalizeCart,
|
|
||||||
useHook({ input, useData }) {
|
useHook({ input, useData }) {
|
||||||
const response = useData({
|
const response = useData({
|
||||||
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
|
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
|
||||||
@ -85,6 +93,60 @@ const useCart: HookHandler<
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const useSearch: HookHandler<
|
||||||
|
SearchProductsData,
|
||||||
|
SearchProductsInput,
|
||||||
|
SearchProductsInput
|
||||||
|
> = {
|
||||||
|
fetchOptions: {
|
||||||
|
query: getAllProductsQuery,
|
||||||
|
},
|
||||||
|
async fetcher({ input, options, fetch }) {
|
||||||
|
const resp = await fetch({
|
||||||
|
query: options?.query,
|
||||||
|
method: options?.method,
|
||||||
|
variables: getSearchVariables(input),
|
||||||
|
})
|
||||||
|
const edges = resp.products?.edges
|
||||||
|
return {
|
||||||
|
products: edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p)),
|
||||||
|
found: !!edges?.length,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
useHook({ input, useData }) {
|
||||||
|
return useData({
|
||||||
|
input: [
|
||||||
|
['search', input.search],
|
||||||
|
['categoryId', input.categoryId],
|
||||||
|
['brandId', input.brandId],
|
||||||
|
['sort', input.sort],
|
||||||
|
],
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
...input.swrOptions,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const useCustomerHandler: HookHandler<Customer | null> = {
|
||||||
|
fetchOptions: {
|
||||||
|
query: getCustomerQuery,
|
||||||
|
},
|
||||||
|
async fetcher({ options, fetch }) {
|
||||||
|
const data = await fetch<any | null>(options)
|
||||||
|
return data?.customer ?? null
|
||||||
|
},
|
||||||
|
useHook({ input, useData }) {
|
||||||
|
return useData({
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
...input.swrOptions,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
export const shopifyProvider = {
|
export const shopifyProvider = {
|
||||||
locale: 'en-us',
|
locale: 'en-us',
|
||||||
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||||
@ -92,6 +154,8 @@ export const shopifyProvider = {
|
|||||||
fetcher,
|
fetcher,
|
||||||
cartNormalizer: normalizeCart,
|
cartNormalizer: normalizeCart,
|
||||||
cart: { useCart },
|
cart: { useCart },
|
||||||
|
customer: { useCustomer: useCustomerHandler },
|
||||||
|
products: { useSearch },
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ShopifyProvider = typeof shopifyProvider
|
export type ShopifyProvider = typeof shopifyProvider
|
||||||
|
9
framework/shopify/utils/index.ts
Normal file
9
framework/shopify/utils/index.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
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 * from './customer-token'
|
||||||
|
export * from './queries'
|
||||||
|
export * from './mutations'
|
Loading…
x
Reference in New Issue
Block a user