4
0
forked from crowetic/commerce

Fixed remaining issues with types

This commit is contained in:
Luis Alvarez 2021-02-15 12:02:24 -05:00
parent 499516e967
commit bb0b8d2771
8 changed files with 26 additions and 14 deletions

View File

@ -68,7 +68,7 @@ async function getCustomerWishlist({
const productsById = graphqlData.products.reduce<{ const productsById = graphqlData.products.reduce<{
[k: number]: ProductEdge [k: number]: ProductEdge
}>((prods, p) => { }>((prods, p) => {
prods[p.node.entityId] = p prods[Number(p.node.entityId)] = p as any
return prods return prods
}, {}) }, {})
// Populate the wishlist items with the graphql products // Populate the wishlist items with the graphql products

View File

@ -3,6 +3,7 @@ import setProductLocaleMeta from '../api/utils/set-product-locale-meta'
import { productInfoFragment } from '../api/fragments/product' import { productInfoFragment } from '../api/fragments/product'
import { BigcommerceConfig, getConfig } from '../api' import { BigcommerceConfig, getConfig } from '../api'
import { normalizeProduct } from '@framework/lib/normalize' import { normalizeProduct } from '@framework/lib/normalize'
import type { Product } from '@commerce/types'
export const getProductQuery = /* GraphQL */ ` export const getProductQuery = /* GraphQL */ `
query getProduct( query getProduct(

View File

@ -107,7 +107,7 @@ const useWishlist: HookHandler<
const { data: customer } = useCustomer() const { data: customer } = useCustomer()
const response = useData({ const response = useData({
input: [ input: [
['customerId', customer?.id], ['customerId', (customer as any)?.id],
['includeProducts', input.includeProducts], ['includeProducts', input.includeProducts],
], ],
swrOptions: { swrOptions: {

View File

@ -1,19 +1,23 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import { HookFetcher } from '@commerce/utils/types' import { HookFetcher } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
import useWishlistAddItem from '@commerce/wishlist/use-add-item' import useWishlistAddItem, {
AddItemInput,
} from '@commerce/wishlist/use-add-item'
import { UseWishlistInput } from '@commerce/wishlist/use-wishlist'
import type { ItemBody, AddItemBody } from '../api/wishlist' import type { ItemBody, AddItemBody } from '../api/wishlist'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import useWishlist, { UseWishlistOptions, Wishlist } from './use-wishlist' import useWishlist from './use-wishlist'
import type { BigcommerceProvider } from '..'
const defaultOpts = { const defaultOpts = {
url: '/api/bigcommerce/wishlist', url: '/api/bigcommerce/wishlist',
method: 'POST', method: 'POST',
} }
export type AddItemInput = ItemBody // export type AddItemInput = ItemBody
export const fetcher: HookFetcher<Wishlist, AddItemBody> = ( export const fetcher: HookFetcher<any, AddItemBody> = (
options, options,
{ item }, { item },
fetch fetch
@ -27,13 +31,13 @@ export const fetcher: HookFetcher<Wishlist, AddItemBody> = (
} }
export function extendHook(customFetcher: typeof fetcher) { export function extendHook(customFetcher: typeof fetcher) {
const useAddItem = (opts?: UseWishlistOptions) => { const useAddItem = (opts?: UseWishlistInput<BigcommerceProvider>) => {
const { data: customer } = useCustomer() const { data: customer } = useCustomer()
const { revalidate } = useWishlist(opts) const { revalidate } = useWishlist(opts)
const fn = useWishlistAddItem(defaultOpts, customFetcher) const fn = useWishlistAddItem(defaultOpts, customFetcher)
return useCallback( return useCallback(
async function addItem(input: AddItemInput) { async function addItem(input: AddItemInput<any>) {
if (!customer) { if (!customer) {
// A signed customer is required in order to have a wishlist // A signed customer is required in order to have a wishlist
throw new CommerceError({ throw new CommerceError({

View File

@ -4,7 +4,7 @@ import { CommerceError } from '@commerce/utils/errors'
import useWishlistRemoveItem from '@commerce/wishlist/use-remove-item' import useWishlistRemoveItem from '@commerce/wishlist/use-remove-item'
import type { RemoveItemBody } from '../api/wishlist' import type { RemoveItemBody } from '../api/wishlist'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import useWishlist, { UseWishlistOptions, Wishlist } from './use-wishlist' import useWishlist from './use-wishlist'
const defaultOpts = { const defaultOpts = {
url: '/api/bigcommerce/wishlist', url: '/api/bigcommerce/wishlist',
@ -15,7 +15,7 @@ export type RemoveItemInput = {
id: string | number id: string | number
} }
export const fetcher: HookFetcher<Wishlist | null, RemoveItemBody> = ( export const fetcher: HookFetcher<any | null, RemoveItemBody> = (
options, options,
{ itemId }, { itemId },
fetch fetch
@ -28,10 +28,10 @@ export const fetcher: HookFetcher<Wishlist | null, RemoveItemBody> = (
} }
export function extendHook(customFetcher: typeof fetcher) { export function extendHook(customFetcher: typeof fetcher) {
const useRemoveItem = (opts?: UseWishlistOptions) => { const useRemoveItem = (opts?: any) => {
const { data: customer } = useCustomer() const { data: customer } = useCustomer()
const { revalidate } = useWishlist(opts) const { revalidate } = useWishlist(opts)
const fn = useWishlistRemoveItem<Wishlist | null, RemoveItemBody>( const fn = useWishlistRemoveItem<any | null, RemoveItemBody>(
defaultOpts, defaultOpts,
customFetcher customFetcher
) )

View File

@ -1,4 +1,11 @@
import useAction from '../utils/use-action' import useAction from '../utils/use-action'
import type { CartItemBody } from '../types'
// Input expected by the action returned by the `useAddItem` hook
// export interface AddItemInput {
// includeProducts?: boolean
// }
export type AddItemInput<T extends CartItemBody> = T
const useAddItem = useAction const useAddItem = useAction

View File

@ -61,7 +61,7 @@ export default function Slug({
return router.isFallback ? ( return router.isFallback ? (
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views <h1>Loading...</h1> // TODO (BC) Add Skeleton Views
) : ( ) : (
<ProductView product={product} /> <ProductView product={product as any} />
) )
} }

View File

@ -44,7 +44,7 @@ export default function Wishlist() {
) : ( ) : (
data && data &&
data.items?.map((item) => ( data.items?.map((item) => (
<WishlistCard key={item.id} item={item} /> <WishlistCard key={item.id} product={item as any} />
)) ))
)} )}
</div> </div>