Skip to content

Commit

Permalink
feat: Add buyNowMode and checkoutUrl props to AddToCartButton
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Apr 14, 2022
1 parent 39d03a9 commit c6a8535
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
38 changes: 27 additions & 11 deletions src/components/AddToCartButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import React, {
FunctionComponent,
useContext,
PropsWithoutRef,
ReactNode,
} from 'react'
import React, { useContext, PropsWithoutRef, ReactNode } from 'react'
import Parent from './utils/Parent'
import OrderContext from '#context/OrderContext'
import { isEmpty, has } from 'lodash'
Expand All @@ -16,8 +11,9 @@ import SkuListsContext from '#context/SkuListsContext'
import ExternalFunctionContext from '#context/ExternalFunctionContext'
import { VariantOption } from '#components/VariantSelector'
import isFunction from 'lodash/isFunction'
import SkuChildrenContext from '#context/SkuChildrenContext'

const propTypes = components.AddToCartButton.propTypes
const propTypes = components.AddToCartButton.propTypes as any
const defaultProps = components.AddToCartButton.defaultProps
const displayName = components.AddToCartButton.displayName

Expand All @@ -29,6 +25,16 @@ type AddToCartButtonChildrenProps = FunctionChildren<ChildrenProps>

export type AddToCartButtonType = ChildrenProps

type BuyNowMode =
| {
buyNowMode: true
checkoutUrl?: string
}
| {
buyNowMode: false
checkoutUrl: never
}

type AddToCartButtonProps = {
children?: AddToCartButtonChildrenProps
label?: string | ReactNode
Expand All @@ -37,9 +43,12 @@ type AddToCartButtonProps = {
disabled?: boolean
skuListId?: string
lineItem?: VariantOption['lineItem']
} & PropsWithoutRef<JSX.IntrinsicElements['button']>
} & BuyNowMode &
PropsWithoutRef<JSX.IntrinsicElements['button']>

const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
const AddToCartButton: React.FunctionComponent<AddToCartButtonProps> = (
props
) => {
const {
label = 'Add to cart',
children,
Expand All @@ -48,6 +57,8 @@ const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
disabled,
skuListId,
lineItem,
buyNowMode,
checkoutUrl,
...p
} = props
const { addToCart, orderId, getOrder, setOrderErrors } =
Expand All @@ -64,10 +75,11 @@ const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
skuCode: itemSkuCode,
} = useContext(ItemContext)
const { skuLists } = useContext(SkuListsContext)
const { sku } = useContext(SkuChildrenContext)
const sCode = (
!isEmpty(items) && skuCode
? items[skuCode]?.code
: skuCode || getCurrentItemKey(item) || itemSkuCode
: sku?.code || skuCode || getCurrentItemKey(item) || itemSkuCode
) as string
const availabilityQuantity = item[sCode]?.inventory?.quantity
const handleClick = () => {
Expand All @@ -81,7 +93,7 @@ const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
if (has(skuLists, skuListId)) {
const lineItems =
skuLists &&
skuLists[skuListId].map((skuCode: any) => {
skuLists[skuListId].map((skuCode: string) => {
return {
skuCode,
quantity: slQty,
Expand Down Expand Up @@ -119,6 +131,8 @@ const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
quantity: qty,
option: opt,
lineItem: customLineItem,
buyNowMode,
checkoutUrl,
})
: callExternalFunction({
url,
Expand All @@ -129,6 +143,8 @@ const AddToCartButton: FunctionComponent<AddToCartButtonProps> = (props) => {
quantity: qty,
option: opt,
lineItem: customLineItem,
buyNowMode,
checkoutUrl,
},
})
.then(async (res) => {
Expand Down
34 changes: 33 additions & 1 deletion src/reducers/OrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
OrderUpdate,
QueryParamsRetrieve,
} from '@commercelayer/sdk'
import getOrganizationSlug from '#utils/organization'

export type GetOrderParams = Partial<{
clearWhenPlaced: boolean
Expand Down Expand Up @@ -66,6 +67,8 @@ export type AddToCartParams = Partial<{
orderAttributes: Record<string, any>
errors: BaseError[]
setLocalOrder: SetLocalOrder
buyNowMode: boolean
checkoutUrl: string
}>

export interface AddToCartImportParams
Expand Down Expand Up @@ -120,7 +123,14 @@ export interface OrderPayload {

export type AddToCartValues = Pick<
AddToCartParams,
'bundleCode' | 'lineItem' | 'quantity' | 'skuCode' | 'skuId' | 'option'
| 'bundleCode'
| 'lineItem'
| 'quantity'
| 'skuCode'
| 'skuId'
| 'option'
| 'buyNowMode'
| 'checkoutUrl'
>

export type AddToCartImportValues = Pick<AddToCartImportParams, 'lineItems'>
Expand Down Expand Up @@ -354,8 +364,22 @@ export const addToCart: AddToCart = async (params) => {
lineItem,
state,
errors = [],
buyNowMode,
checkoutUrl,
} = params
try {
if (!config)
throw {
errors: [
{
code: 'INVALID_RESOURCE',
resource: 'orders',
title: 'Markup error',
message:
'You are trying to place an order outside the OrderContainer component',
},
] as BaseError[],
}
const sdk = getSdk(config as CommerceLayerConfig)
const id = await createOrder(params)
if (id) {
Expand Down Expand Up @@ -404,6 +428,14 @@ export const addToCart: AddToCart = async (params) => {
},
})
}
if (buyNowMode) {
const { organization } = getOrganizationSlug(config.endpoint)
const params = `${id}?accessToken=${config.accessToken}`
const redirectUrl = checkoutUrl
? `${checkoutUrl}/${params}`
: `https://${organization}.checkout.commercelayer.app/${params}`
location.href = redirectUrl
}
return { success: true }
}
return { success: false }
Expand Down

0 comments on commit c6a8535

Please sign in to comment.