Fixed types

This commit is contained in:
cond0r 2021-03-11 08:56:24 +02:00
parent bb14651785
commit 0c2c7343c3
14 changed files with 90 additions and 176 deletions

View File

@ -1,7 +1,7 @@
{ {
"provider": "bigcommerce", "provider": "shopify",
"features": { "features": {
"wishlist": true, "wishlist": false,
"customCheckout": false "customCheckout": false
} }
} }

View File

@ -4,8 +4,7 @@ export type SelectedOptions = Record<string, string | null>
export function getVariant(product: Product, opts: SelectedOptions) { export function getVariant(product: Product, opts: SelectedOptions) {
const variant = product.variants.find((variant) => { const variant = product.variants.find((variant) => {
return Object.entries(opts).every(([key, value]) => return Object.entries(opts).every(([key, value]) =>
value variant.options.find((option) => {
? variant.options.find((option) => {
if ( if (
option.__typename === 'MultipleChoiceOption' && option.__typename === 'MultipleChoiceOption' &&
option.displayName.toLowerCase() === key.toLowerCase() option.displayName.toLowerCase() === key.toLowerCase()
@ -13,9 +12,6 @@ export function getVariant(product: Product, opts: SelectedOptions) {
return option.values.find((v) => v.label.toLowerCase() === value) return option.values.find((v) => v.label.toLowerCase() === value)
} }
}) })
: !variant.options.find(
(v) => v.displayName.toLowerCase() === key.toLowerCase()
)
) )
}) })
return variant return variant

View File

@ -27,7 +27,7 @@ export const handler: SWRHook<
const data = await fetch({ const data = await fetch({
...options, ...options,
variables: { variables: {
checkoutId, checkoutId: checkoutId,
}, },
}) })
checkout = data.node checkout = data.node

View File

@ -2,9 +2,14 @@ import { Fetcher } from '@commerce/utils/types'
import { API_TOKEN, API_URL } from './const' import { API_TOKEN, API_URL } from './const'
import { handleFetchResponse } from './utils' import { handleFetchResponse } from './utils'
const fetcher: Fetcher = async ({ method = 'POST', variables, query }) => { const fetcher: Fetcher = async ({
url = API_URL,
method = 'POST',
variables,
query,
}) => {
return handleFetchResponse( return handleFetchResponse(
await fetch(API_URL, { await fetch(url, {
method, method,
body: JSON.stringify({ query, variables }), body: JSON.stringify({ query, variables }),
headers: { headers: {

View File

@ -28,8 +28,7 @@ export type ShopifyProps = {
export function CommerceProvider({ children, ...config }: ShopifyProps) { export function CommerceProvider({ children, ...config }: ShopifyProps) {
return ( return (
<CoreCommerceProvider <CoreCommerceProvider
// TODO: Fix this type provider={shopifyProvider}
provider={shopifyProvider as any}
config={{ ...shopifyConfig, ...config }} config={{ ...shopifyConfig, ...config }}
> >
{children} {children}

View File

@ -1,4 +1,4 @@
import { SHOPIFY_CHECKOUT_ID_COOKIE, STORE_DOMAIN } from './const' import { SHOPIFY_CHECKOUT_ID_COOKIE } from './const'
import { handler as useCart } from './cart/use-cart' import { handler as useCart } from './cart/use-cart'
import { handler as useAddItem } from './cart/use-add-item' import { handler as useAddItem } from './cart/use-add-item'
@ -17,7 +17,6 @@ import fetcher from './fetcher'
export const shopifyProvider = { export const shopifyProvider = {
locale: 'en-us', locale: 'en-us',
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE, cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
storeDomain: STORE_DOMAIN,
fetcher, fetcher,
cart: { useCart, useAddItem, useUpdateItem, useRemoveItem }, cart: { useCart, useAddItem, useUpdateItem, useRemoveItem },
customer: { useCustomer }, customer: { useCustomer },

View File

@ -7,13 +7,11 @@ export type ShopifyCheckout = {
lineItems: CheckoutLineItem[] lineItems: CheckoutLineItem[]
} }
export interface Cart extends Core.Cart { export type Cart = Core.Cart & {
id: string
lineItems: LineItem[] lineItems: LineItem[]
} }
export interface LineItem extends Core.LineItem { export interface LineItem extends Core.LineItem {
options: any[] options?: any[]
} }
/** /**

View File

@ -1,13 +0,0 @@
export const getCheckoutIdFromStorage = (token: string) => {
if (window && window.sessionStorage) {
return window.sessionStorage.getItem(token)
}
return null
}
export const setCheckoutIdInStorage = (token: string, id: string | number) => {
if (window && window.sessionStorage) {
return window.sessionStorage.setItem(token, id + '')
}
}

View File

@ -1,34 +1,13 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item'
import useCustomer from '../customer/use-customer' export function emptyHook() {
import useWishlist from './use-wishlist' const useEmptyHook = async (options = {}) => {
return useCallback(async function () {
export default useAddItem as UseAddItem<typeof handler> return Promise.resolve()
}, [])
export const handler: MutationHook<any, {}, any, any> = {
fetchOptions: {
query: '',
},
useHook: ({ fetch }) => () => {
const { data: customer } = useCustomer()
const { revalidate } = useWishlist()
return useCallback(
async function addItem(item) {
if (!customer) {
// A signed customer is required in order to have a wishlist
throw new CommerceError({
message: 'Signed customer not found',
})
} }
await revalidate() return useEmptyHook
return null
},
[fetch, revalidate, customer]
)
},
} }
export default emptyHook

View File

@ -1,36 +1,17 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useRemoveItem, {
UseRemoveItem,
} from '@commerce/wishlist/use-remove-item'
import useCustomer from '../customer/use-customer' type Options = {
import useWishlist from './use-wishlist' includeProducts?: boolean
}
export default useRemoveItem as UseRemoveItem<typeof handler> export function emptyHook(options?: Options) {
const useEmptyHook = async ({ id }: { id: string | number }) => {
export const handler: MutationHook<any, {}, any, any> = { return useCallback(async function () {
fetchOptions: { return Promise.resolve()
query: '', }, [])
},
useHook: ({ fetch }) => () => {
const { data: customer } = useCustomer()
const { revalidate } = useWishlist()
return useCallback(
async function addItem(item) {
if (!customer) {
// A signed customer is required in order to have a wishlist
throw new CommerceError({
message: 'Signed customer not found',
})
} }
await revalidate() return useEmptyHook
return null
},
[fetch, revalidate, customer]
)
},
} }
export default emptyHook

View File

@ -1,49 +1,46 @@
import { useMemo } from 'react' // TODO: replace this hook and other wishlist hooks with a handler, or remove them if
import { SWRHook } from '@commerce/utils/types' // Shopify doesn't have a wishlist
import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist'
import useCustomer from '../customer/use-customer'
export type UseWishlistInput = { includeProducts?: boolean } import { HookFetcher } from '@commerce/utils/types'
import { Product } from '../schema'
export default useWishlist as UseWishlist<typeof handler> const defaultOpts = {}
export const handler: SWRHook< export type Wishlist = {
any | null, items: [
UseWishlistInput, {
{ customerId?: number } & UseWishlistInput, product_id: number
{ isEmpty?: boolean } variant_id: number
> = { id: number
fetchOptions: { product: Product
url: '/api/bigcommerce/wishlist', }
method: 'GET', ]
},
fetcher() {
return { items: [] }
},
useHook: ({ useData }) => (input) => {
const { data: customer } = useCustomer()
const response = useData({
input: [
['customerId', customer?.entityId],
['includeProducts', input?.includeProducts],
],
swrOptions: {
revalidateOnFocus: false,
...input?.swrOptions,
},
})
return useMemo(
() =>
Object.create(response, {
isEmpty: {
get() {
return (response.data?.items?.length || 0) <= 0
},
enumerable: true,
},
}),
[response]
)
},
} }
export interface UseWishlistOptions {
includeProducts?: boolean
}
export interface UseWishlistInput extends UseWishlistOptions {
customerId?: number
}
export const fetcher: HookFetcher<Wishlist | null, UseWishlistInput> = () => {
return null
}
export function extendHook(
customFetcher: typeof fetcher,
// swrOptions?: SwrOptions<Wishlist | null, UseWishlistInput>
swrOptions?: any
) {
const useWishlist = ({ includeProducts }: UseWishlistOptions = {}) => {
return { data: null }
}
useWishlist.extend = extendHook
return useWishlist
}
export default extendHook(fetcher)

View File

@ -25,8 +25,7 @@ export async function getStaticProps({
const pageItem = pages.find((p) => (p.url ? getSlug(p.url) === slug : false)) const pageItem = pages.find((p) => (p.url ? getSlug(p.url) === slug : false))
const data = const data =
pageItem && pageItem &&
// TODO: Shopify - Fix this type (await getPage({ variables: { id: pageItem.id! }, config, preview }))
(await getPage({ variables: { id: pageItem.id! } as any, config, preview }))
const page = data?.page const page = data?.page
if (!page) { if (!page) {

View File

@ -1,26 +0,0 @@
import type { GetStaticPropsContext } from 'next'
import { getConfig } from '@framework/api'
import getAllPages from '@framework/common/get-all-pages'
import { Layout } from '@components/common'
import { Container, Text } from '@components/ui'
export async function getStaticProps({
preview,
locale,
}: GetStaticPropsContext) {
const config = getConfig({ locale })
const { pages } = await getAllPages({ config, preview })
return {
props: { pages },
}
}
export default function ActivateAccount() {
return (
<Container>
<Text variant="pageHeading">Activate Your Account</Text>
</Container>
)
}
ActivateAccount.Layout = Layout

View File

@ -22,8 +22,8 @@
"@components/*": ["components/*"], "@components/*": ["components/*"],
"@commerce": ["framework/commerce"], "@commerce": ["framework/commerce"],
"@commerce/*": ["framework/commerce/*"], "@commerce/*": ["framework/commerce/*"],
"@framework": ["framework/bigcommerce"], "@framework": ["framework/shopify"],
"@framework/*": ["framework/bigcommerce/*"] "@framework/*": ["framework/shopify/*"]
} }
}, },
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], "include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],