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.
* GetProduct Initial Commit * addItemToCart function implemneted * Add Item to cart functionality implemented * ICKY-166-getProducts-and-getProduct (vercel#6) * GetProduct Initial Commit * Passed productCode as Slug to get-product * Moved currencyCode in Config file * Icky 173 (vercel#9) * Initial commit related to getAllPages * Initial Changes * Making documentListName configurable * fixing dynamic page rendering and adding typescript code Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com> Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com> * addItemToCart function implemneted Conflicts resolved * Add Item to cart functionality implemented * booleans removed from query * cart size option enabled * updated addItem for with and without variants products Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com> Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com> Co-authored-by: kibo-sushant <89385472+kibo-sushant@users.noreply.github.com> Co-authored-by: kibo-kevinwatts <85258296+kibo-kevinwatts@users.noreply.github.com> Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com>
- Loading branch information
1 parent
4993708
commit f32b4c9
Showing
10 changed files
with
258 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Product } from './../../../schema.d' | ||
import { normalizeCart } from '../../../lib/normalize' | ||
import type { CartEndpoint } from '.' | ||
import addToCurrentCartMutation from '@framework/api/mutations/addToCart-mutation' | ||
import getAnonymousShopperToken from '@framework/api/utils/get-anonymous-shopper-token' | ||
import { getCookieExpirationDate } from '@framework/lib/getCookieExpirationDate' | ||
import { prepareSetCookie } from '@framework/lib/prepareSetCookie' | ||
import { setCookies } from '@framework/lib/setCookie' | ||
import { getProductQuery } from '@framework/api/queries/get-product-query' | ||
import { getCartQuery } from '@framework/api/queries/getCartQuery' | ||
|
||
const buildAddToCartVariables = ({ | ||
productId, | ||
variantId, | ||
quantity = 1, | ||
productResponse, | ||
}: { | ||
productId: string | ||
variantId: string | ||
quantity: number | ||
productResponse: any | ||
}) => { | ||
const { product } = productResponse.data | ||
|
||
const selectedValue = product.variations?.find( | ||
(v: any) => v.productCode === variantId | ||
).options[0].value | ||
|
||
const options = product.options?.map((po: any) => ({ | ||
attributeFQN: po.attributeFQN, | ||
name: po.attributeDetail.name, | ||
value: po.values.find((v: any) => v.value == selectedValue).value, | ||
})) | ||
|
||
return { | ||
productToAdd: { | ||
product: { | ||
productCode: productId, | ||
variationProductCode: variantId ? variantId : null, | ||
options, | ||
}, | ||
quantity, | ||
fulfillmentMethod: 'Ship', | ||
}, | ||
} | ||
} | ||
|
||
const addItem: CartEndpoint['handlers']['addItem'] = async ({ | ||
req, | ||
res, | ||
body: { cartId, item }, | ||
config, | ||
}) => { | ||
if (!item) { | ||
return res.status(400).json({ | ||
data: null, | ||
errors: [{ message: 'Missing item' }], | ||
}) | ||
} | ||
if (!item.quantity) item.quantity = 1 | ||
|
||
const productResponse = await config.fetch(getProductQuery, { | ||
variables: { productCode: item?.productId }, | ||
}) | ||
|
||
const token = req.cookies[config.customerCookie] | ||
|
||
let accessToken = token ? JSON.parse(token).accessToken : null | ||
|
||
if (!accessToken) { | ||
const response: any = await getAnonymousShopperToken({ config }) | ||
accessToken = response?.accessToken | ||
const cookieExpirationDate = getCookieExpirationDate( | ||
config.customerCookieMaxAgeInDays | ||
) | ||
|
||
const authCookie = prepareSetCookie( | ||
config.customerCookie, | ||
JSON.stringify(response), | ||
response?.accessTokenExpiration ? { expires: cookieExpirationDate } : {} | ||
) | ||
setCookies(res, [authCookie]) | ||
} | ||
|
||
const addToCartResponse = await config.fetch( | ||
addToCurrentCartMutation, | ||
{ | ||
variables: buildAddToCartVariables({ ...item, productResponse }), | ||
}, | ||
{ headers: { 'x-vol-user-claims': accessToken } } | ||
) | ||
let currentCart = null | ||
if (addToCartResponse.data.addItemToCurrentCart) { | ||
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 addItem |
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
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,32 @@ | ||
export const cartItemDetails = /*GraphQL*/` | ||
fragment cartItemDetails on CartItem { | ||
id | ||
product { | ||
productCode | ||
name | ||
description | ||
imageUrl | ||
options { | ||
attributeFQN | ||
name | ||
value | ||
} | ||
properties { | ||
attributeFQN | ||
name | ||
values { | ||
value | ||
} | ||
} | ||
sku | ||
price { | ||
price | ||
salePrice | ||
} | ||
categories { | ||
id | ||
} | ||
} | ||
quantity | ||
} | ||
`; |
12 changes: 12 additions & 0 deletions
12
framework/kibocommerce/api/mutations/addToCart-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,12 @@ | ||
import { cartItemDetails } from './../fragments/cartItemDetails' | ||
|
||
const addToCurrentCartMutation = /*GraphQL*/ ` | ||
${cartItemDetails} | ||
mutation addToCart($productToAdd:CartItemInput!){ | ||
addItemToCurrentCart(cartItemInput: $productToAdd) { | ||
...cartItemDetails | ||
} | ||
}` | ||
|
||
export default addToCurrentCartMutation |
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
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,17 +1,44 @@ | ||
import { useCallback } from 'react' | ||
import type { MutationHook } from '@commerce/utils/types' | ||
import { CommerceError } from '@commerce/utils/errors' | ||
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' | ||
import { MutationHook } from '@commerce/utils/types' | ||
import type { AddItemHook } from '@commerce/types/cart' | ||
import useCart from './use-cart' | ||
|
||
export default useAddItem as UseAddItem<typeof handler> | ||
export const handler: MutationHook<any> = { | ||
|
||
export const handler: MutationHook<AddItemHook> = { | ||
fetchOptions: { | ||
query: '', | ||
url: '/api/cart', | ||
method: 'POST', | ||
}, | ||
async fetcher({ input: item, options, fetch }) { | ||
if ( | ||
item.quantity && | ||
(!Number.isInteger(item.quantity) || item.quantity! < 1) | ||
) { | ||
throw new CommerceError({ | ||
message: 'The item quantity has to be a valid integer greater than 0', | ||
}) | ||
} | ||
|
||
const data = await fetch({ | ||
...options, | ||
body: { item }, | ||
}) | ||
|
||
return data | ||
}, | ||
useHook: ({ fetch }) => () => { | ||
const { mutate } = useCart() | ||
|
||
return useCallback( | ||
async function addItem(input) { | ||
const data = await fetch({ input }) | ||
await mutate(data, false) | ||
return data | ||
}, | ||
[fetch, mutate] | ||
) | ||
}, | ||
async fetcher({ input, options, fetch }) {}, | ||
useHook: | ||
({ fetch }) => | ||
() => { | ||
return async function addItem() { | ||
return {} | ||
} | ||
}, | ||
} |
Oops, something went wrong.