4
0
forked from crowetic/commerce

Type changes

This commit is contained in:
Luis Alvarez 2020-10-07 15:27:22 -05:00
parent fe66a8d746
commit e874f43abc
5 changed files with 17 additions and 17 deletions

View File

@ -81,7 +81,7 @@ const CartItem = ({
<span>{price}</span> <span>{price}</span>
<button <button
className="flex justify-end" className="flex justify-end"
onClick={() => removeItem({ itemId: item.id })} onClick={() => removeItem({ id: item.id })}
> >
<Trash /> <Trash />
</button> </button>

View File

@ -30,10 +30,8 @@ const ProductView: FC<Props> = ({ product, productData, className }) => {
const addToCart = () => { const addToCart = () => {
// TODO: loading state by awating the promise // TODO: loading state by awating the promise
addItem({ addItem({
item: { productId: product.entityId,
productId: product.entityId, variantId: product.variants.edges?.[0]?.node.entityId!,
variantId: product.variants.edges?.[0]?.node.entityId!,
},
}) })
} }

View File

@ -3,7 +3,7 @@ import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
import type { ItemBody, AddItemBody } from '../api/cart' import type { ItemBody, AddItemBody } from '../api/cart'
import { Cart, useCart } from '.' import { Cart, useCart } from '.'
export type { ItemBody, AddItemBody } export type UpdateItemInput = ItemBody
function fetcher(fetch: Fetcher<Cart>, { item }: AddItemBody) { function fetcher(fetch: Fetcher<Cart>, { item }: AddItemBody) {
if ( if (
@ -21,8 +21,8 @@ function fetcher(fetch: Fetcher<Cart>, { item }: AddItemBody) {
export default function useAddItem() { export default function useAddItem() {
const { mutate } = useCart() const { mutate } = useCart()
const fn = useCartAddItem<Cart, AddItemBody>(fetcher) const fn = useCartAddItem<Cart, AddItemBody>(fetcher)
const addItem: typeof fn = async (input) => { const addItem = async (input: UpdateItemInput) => {
const data = await fn(input) const data = await fn({ item: input })
await mutate(data, false) await mutate(data, false)
return data return data
} }

View File

@ -1,9 +1,11 @@
import type { Fetcher } from '@lib/commerce' import type { Fetcher } from '@lib/commerce'
import { default as useCartRemoveItem } from '@lib/commerce/cart/use-remove-item' import { default as useCartRemoveItem } from '@lib/commerce/cart/use-remove-item'
import type { ItemBody, RemoveItemBody } from '../api/cart' import type { RemoveItemBody } from '../api/cart'
import { Cart, useCart } from '.' import { Cart, useCart } from '.'
export type { ItemBody, RemoveItemBody } export type RemoveItemInput = {
id: string
}
export function fetcher( export function fetcher(
fetch: Fetcher<Cart | null>, fetch: Fetcher<Cart | null>,
@ -16,11 +18,11 @@ export function fetcher(
}) })
} }
export default function useRemoveItem() { export default function useRemoveItem(item?: any) {
const { mutate } = useCart() const { mutate } = useCart()
const fn = useCartRemoveItem<Cart | null, RemoveItemBody>(fetcher) const fn = useCartRemoveItem<Cart | null, RemoveItemBody>(fetcher)
const removeItem: typeof fn = async (input) => { const removeItem = async (input: RemoveItemInput) => {
const data = await fn(input) const data = await fn({ itemId: input.id ?? item?.id })
await mutate(data, false) await mutate(data, false)
return data return data
} }

View File

@ -26,15 +26,15 @@ function fetcher(
}) })
} }
export default function useUpdateItem(item: any = {}) { export default function useUpdateItem(item?: any) {
const { mutate } = useCart() const { mutate } = useCart()
const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(fetcher) const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(fetcher)
const updateItem = async (input: UpdateItemInput) => { const updateItem = async (input: UpdateItemInput) => {
const data = await fn({ const data = await fn({
itemId: input.id ?? item.id, itemId: input.id ?? item?.id,
item: { item: {
productId: input.productId ?? item.product_id, productId: input.productId ?? item?.product_id,
variantId: input.productId ?? item.variant_id, variantId: input.productId ?? item?.variant_id,
quantity: input.quantity, quantity: input.quantity,
}, },
}) })