4
0
forked from crowetic/commerce

update remove item from cart, landing page fetch, image normalization

This commit is contained in:
Greg Hoskin 2021-04-04 18:07:14 -06:00
parent d0a04a8fe9
commit abd86329d5
3 changed files with 32 additions and 14 deletions

View File

@ -31,27 +31,30 @@ export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler = { export const handler = {
fetchOptions: { fetchOptions: {
query: checkoutLineItemRemoveMutation, query: 'cart',
method: 'removeItem',
}, },
async fetcher({ async fetcher({
input: { itemId }, input: { itemId },
options, options,
fetch, fetch,
}: HookFetcherContext<RemoveCartItemBody>) { }: HookFetcherContext<RemoveCartItemBody>) {
const data = await fetch<Mutation, MutationCheckoutLineItemsRemoveArgs>({ const response = await fetch<Mutation, MutationCheckoutLineItemsRemoveArgs>(
{
...options, ...options,
variables: { checkoutId: getCheckoutId(), lineItemIds: [itemId] }, variables: [itemId],
}) }
return checkoutToCart(data.checkoutLineItemsRemove) )
return checkoutToCart(response)
}, },
useHook: ({ useHook: ({
fetch, fetch,
}: MutationHookContext<Cart | null, RemoveCartItemBody>) => < }: MutationHookContext<Cart | null, RemoveCartItemBody>) => <
T extends LineItem | undefined = undefined T extends LineItem | undefined = undefined
>( >(
ctx: { item?: T } = {} item
) => { ) => {
const { item } = ctx // const { item } = ctx
const { mutate } = useCart() const { mutate } = useCart()
const removeItem: RemoveItemFn<LineItem> = async (input) => { const removeItem: RemoveItemFn<LineItem> = async (input) => {
const itemId = input?.id ?? item?.id const itemId = input?.id ?? item?.id

View File

@ -21,9 +21,10 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => { }): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {} let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config) config = getConfig(config)
const { results } = await config.fetchSwell('products', 'get') const { results } = await config.fetchSwell('products', 'list', {
limit: variables.first,
})
const products = results.map((product) => normalizeProduct(product)) ?? [] const products = results.map((product) => normalizeProduct(product)) ?? []
return { return {
products, products,
} }

View File

@ -46,11 +46,25 @@ const normalizeProductOption = ({
} }
} }
const normalizeProductImages = (images) => type SwellImage = {
images?.map(({ file, id }) => ({ file: {
url: file.url, url: String
id, height: Number
width: Number
}
id: string
}
const normalizeProductImages = (images) => {
if (!images) {
return [{ url: '/' }]
}
return images?.map(({ file, ...rest }: SwellImage) => ({
url: file?.url,
height: file.height,
width: file.width,
...rest,
})) }))
}
const normalizeProductVariants = (variants) => { const normalizeProductVariants = (variants) => {
return variants?.map(({ id, name, values, price, stock_status }) => ({ return variants?.map(({ id, name, values, price, stock_status }) => ({