forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* addItemToCart function implemneted Conflicts resolved * Add Item to cart functionality implemented * remove Item from cart implemented * removed unused code Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>
- Loading branch information
1 parent
c09a1c4
commit af8c7e0
Showing
5 changed files
with
102 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { normalizeCart } from '../../../lib/normalize' | ||
// import getCartCookie from '../../utils/get-cart-cookie' | ||
import type { CartEndpoint } from '.' | ||
import removeItemFromCartMutation from '@framework/api/mutations/removeItemFromCart-mutation' | ||
import { getCartQuery } from '@framework/api/queries/getCartQuery' | ||
|
||
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({ | ||
req, | ||
res, | ||
body: { cartId, itemId }, | ||
config, | ||
}) => { | ||
if (!itemId) { | ||
return res.status(400).json({ | ||
data: null, | ||
errors: [{ message: 'Invalid request' }], | ||
}) | ||
} | ||
const token = req.cookies[config.customerCookie] | ||
|
||
let accessToken = token ? JSON.parse(token).accessToken : null | ||
|
||
const removeItemResponse = await config.fetch( | ||
removeItemFromCartMutation, | ||
{ | ||
variables: { id: itemId }, | ||
}, | ||
{ headers: { 'x-vol-user-claims': accessToken } } | ||
) | ||
|
||
let currentCart = null | ||
if (removeItemResponse.data.deleteCurrentCartItem) { | ||
let result = await config.fetch( | ||
getCartQuery, | ||
{}, | ||
{ headers: { 'x-vol-user-claims': accessToken } } | ||
) | ||
currentCart = result?.data?.currentCart | ||
} | ||
res.status(200).json({ data: normalizeCart(currentCart) }) | ||
} | ||
|
||
export default removeItem |
9 changes: 9 additions & 0 deletions
9
framework/kibocommerce/api/mutations/removeItemFromCart-mutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Delete cart based on current user session | ||
*/ | ||
const removeItemFromCartMutation = /*GraphQL*/` | ||
mutation deleteCartItem($id: String!) { | ||
deleteCurrentCartItem(cartItemId:$id) | ||
}`; | ||
|
||
export default removeItemFromCartMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
import { MutationHook } from '@commerce/utils/types' | ||
import { useCallback } from 'react' | ||
import type { | ||
MutationHookContext, | ||
HookFetcherContext, | ||
} from '@commerce/utils/types' | ||
import { ValidationError } from '@commerce/utils/errors' | ||
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item' | ||
import type { Cart, LineItem, RemoveItemHook } from '@commerce/types/cart' | ||
import useCart from './use-cart' | ||
|
||
export type RemoveItemFn<T = any> = T extends LineItem | ||
? (input?: RemoveItemActionInput<T>) => Promise<Cart | null | undefined> | ||
: (input: RemoveItemActionInput<T>) => Promise<Cart | null> | ||
|
||
export type RemoveItemActionInput<T = any> = T extends LineItem | ||
? Partial<RemoveItemHook['actionInput']> | ||
: RemoveItemHook['actionInput'] | ||
|
||
export default useRemoveItem as UseRemoveItem<typeof handler> | ||
|
||
export const handler: MutationHook<any> = { | ||
export const handler = { | ||
fetchOptions: { | ||
query: '', | ||
url: '/api/cart', | ||
method: 'DELETE', | ||
}, | ||
async fetcher({ input, options, fetch }) {}, | ||
useHook: | ||
({ fetch }) => | ||
() => { | ||
return async function removeItem(input) { | ||
return {} | ||
async fetcher({ | ||
input: { itemId }, | ||
options, | ||
fetch, | ||
}: HookFetcherContext<RemoveItemHook>) { | ||
return await fetch({ ...options, body: { itemId } }) | ||
}, | ||
useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) => < | ||
T extends LineItem | undefined = undefined | ||
>( | ||
ctx: { item?: T } = {} | ||
) => { | ||
const { item } = ctx | ||
const { mutate } = useCart() | ||
const removeItem: RemoveItemFn<LineItem> = 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<T>, [fetch, mutate]) | ||
}, | ||
} |