From 465a18b628f394b7b832313768133417b4d049ce Mon Sep 17 00:00:00 2001 From: goncy Date: Mon, 19 Apr 2021 10:52:34 -0300 Subject: [PATCH] Replace cart hooks --- framework/csv/cart/use-add-item.tsx | 29 ++-------- framework/csv/cart/use-cart.tsx | 52 +++++------------ framework/csv/cart/use-remove-item.tsx | 60 ++----------------- framework/csv/cart/use-update-item.tsx | 79 ++------------------------ framework/csv/provider.ts | 8 +-- 5 files changed, 31 insertions(+), 197 deletions(-) diff --git a/framework/csv/cart/use-add-item.tsx b/framework/csv/cart/use-add-item.tsx index a18c25d87..9395a9d99 100644 --- a/framework/csv/cart/use-add-item.tsx +++ b/framework/csv/cart/use-add-item.tsx @@ -1,28 +1,7 @@ -import { useCallback } from 'react' -import type { MutationHook } from '@commerce/utils/types' -import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' -import useCart from './use-cart' import { Cart, CartItemBody } from '../types' -export default useAddItem as UseAddItem - -export const handler: MutationHook = { - fetchOptions: { - query: ``, - }, - async fetcher() { - return {} as Promise - }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() - - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, +export default function useAddItem() { + return (_item: CartItemBody): Cart => { + return (null as unknown) as Cart + } } diff --git a/framework/csv/cart/use-cart.tsx b/framework/csv/cart/use-cart.tsx index 5f466ec14..8660d3274 100644 --- a/framework/csv/cart/use-cart.tsx +++ b/framework/csv/cart/use-cart.tsx @@ -1,41 +1,15 @@ -import { useMemo } from 'react' -import useCommerceCart, { - FetchCartInput, - UseCart, -} from '@commerce/cart/use-cart' +import { Cart } from '@framework/types' -import { Cart } from '../types' -import { SWRHook } from '@commerce/utils/types' - -export default useCommerceCart as UseCart - -export const handler: SWRHook< - Cart | null, - {}, - FetchCartInput, - { isEmpty?: boolean } -> = { - fetchOptions: { - query: ``, - }, - async fetcher({ input: { cartId: checkoutId }, options, fetch }) { - return null - }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 - }, - enumerable: true, - }, - }), - [response] - ) - }, +export interface CartResponse { + data: Cart | null + isLoading: boolean + isEmpty: boolean +} + +export default function useCart(): CartResponse { + return { + data: null, + isLoading: false, + isEmpty: true, + } } diff --git a/framework/csv/cart/use-remove-item.tsx b/framework/csv/cart/use-remove-item.tsx index 4729bd482..fd2ce751d 100644 --- a/framework/csv/cart/use-remove-item.tsx +++ b/framework/csv/cart/use-remove-item.tsx @@ -1,57 +1,7 @@ -import { useCallback } from 'react' -import type { - MutationHookContext, - HookFetcherContext, -} from '@commerce/utils/types' -import { RemoveCartItemBody } from '@commerce/types' -import { ValidationError } from '@commerce/utils/errors' -import useRemoveItem, { - RemoveItemInput as RemoveItemInputBase, - UseRemoveItem, -} from '@commerce/cart/use-remove-item' -import useCart from './use-cart' -import { Cart, LineItem } from '../types' +import { Cart, CartItemBody } from '../types' -export type RemoveItemFn = T extends LineItem - ? (input?: RemoveItemInput) => Promise - : (input: RemoveItemInput) => Promise - -export type RemoveItemInput = T extends LineItem - ? Partial - : RemoveItemInputBase - -export default useRemoveItem as UseRemoveItem - -export const handler = { - fetchOptions: { - query: ``, - }, - async fetcher({ input: { itemId } }: HookFetcherContext) { - return { itemId } - }, - useHook: ({ - fetch, - }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { item?: T } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() - const removeItem: RemoveItemFn = async (input) => { - const itemId = input?.id ?? item?.id - - if (!itemId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } - - const data = await fetch({ input: { itemId } }) - await mutate(data, false) - return data - } - - return useCallback(removeItem as RemoveItemFn, [fetch, mutate]) - }, +export default function useRemoveItem() { + return (_item: CartItemBody): Cart | null => { + return null + } } diff --git a/framework/csv/cart/use-update-item.tsx b/framework/csv/cart/use-update-item.tsx index d3eb32c0f..7965f27b3 100644 --- a/framework/csv/cart/use-update-item.tsx +++ b/framework/csv/cart/use-update-item.tsx @@ -1,76 +1,7 @@ -import { useCallback } from 'react' -import debounce from 'lodash.debounce' -import type { - HookFetcherContext, - MutationHookContext, -} from '@commerce/utils/types' -import { ValidationError } from '@commerce/utils/errors' -import useUpdateItem, { - UpdateItemInput as UpdateItemInputBase, - UseUpdateItem, -} from '@commerce/cart/use-update-item' +import { Cart, CartItemBody } from '../types' -import useCart from './use-cart' -import { handler as removeItemHandler } from './use-remove-item' -import type { Cart, LineItem, UpdateCartItemBody } from '../types' -import { checkoutToCart } from '../utils' - -export type UpdateItemInput = T extends LineItem - ? Partial> - : UpdateItemInputBase - -export default useUpdateItem as UseUpdateItem - -export const handler = { - fetchOptions: { - query: ``, - }, - async fetcher({ - input: { itemId, item }, - }: HookFetcherContext) { - return { - item, - itemId, - } - }, - useHook: ({ - fetch, - }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { - item?: T - wait?: number - } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() as any - - return useCallback( - debounce(async (input: UpdateItemInput) => { - const itemId = input.id ?? item?.id - const productId = input.productId ?? item?.productId - const variantId = input.productId ?? item?.variantId - if (!itemId || !productId || !variantId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } - - const data = await fetch({ - input: { - item: { - productId, - variantId, - quantity: input.quantity, - }, - itemId, - }, - }) - await mutate(data, false) - return data - }, ctx.wait ?? 500), - [fetch, mutate] - ) - }, +export default function useUpdateItem() { + return (_item: CartItemBody): Cart => { + return (null as unknown) as Cart + } } diff --git a/framework/csv/provider.ts b/framework/csv/provider.ts index a2fc3afb0..478308b80 100644 --- a/framework/csv/provider.ts +++ b/framework/csv/provider.ts @@ -1,7 +1,7 @@ -import { handler as useCart } from './cart/use-cart' -import { handler as useAddItem } from './cart/use-add-item' -import { handler as useUpdateItem } from './cart/use-update-item' -import { handler as useRemoveItem } from './cart/use-remove-item' +import useCart from './cart/use-cart' +import useAddItem from './cart/use-add-item' +import useUpdateItem from './cart/use-update-item' +import useRemoveItem from './cart/use-remove-item' import { handler as useCustomer } from './customer/use-customer' import { handler as useSearch } from './product/use-search'