Skip to content

Commit

Permalink
Merge pull request #558 from commercelayer/fix/checkout-links-config
Browse files Browse the repository at this point in the history
Checkout link provided by organization config
  • Loading branch information
acasazza authored Jul 30, 2024
2 parents c32a755 + 11724e0 commit 3a9eed0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useNx": false,
"npmClient": "pnpm",
"version": "4.15.2",
"version": "4.15.3-beta.3",
"command": {
"version": {
"preid": "beta"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@commercelayer/react-components",
"version": "4.15.2",
"version": "4.15.3-beta.3",
"description": "The Official Commerce Layer React Components",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function MyAccountLink(props: Props): JSX.Element {
function handleClick(
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>
): void {
e.preventDefault()
if (!disabled && accessToken && endpoint) {
void getOrganizationConfig({
accessToken,
Expand Down
31 changes: 29 additions & 2 deletions packages/react-components/src/components/orders/CheckoutLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type ChildrenFunction } from '#typings/index'
import CommerceLayerContext from '#context/CommerceLayerContext'
import { getApplicationLink } from '#utils/getApplicationLink'
import { getDomain } from '#utils/getDomain'
import { getOrganizationConfig } from '#utils/organization'

interface ChildrenProps extends Omit<Props, 'children'> {
checkoutUrl: string
Expand Down Expand Up @@ -33,7 +34,7 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
* found in the `order` object.
*/
export function CheckoutLink(props: Props): JSX.Element {
const { label, hostedCheckout = true, children, ...p } = props
const { label, hostedCheckout = true, children, onClick, ...p } = props
const { order } = useContext(OrderContext)
const { accessToken, endpoint } = useContext(CommerceLayerContext)
if (accessToken == null || endpoint == null)
Expand All @@ -56,10 +57,36 @@ export function CheckoutLink(props: Props): JSX.Element {
href,
...p
}
function handleClick(
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>
): void {
e.preventDefault()
e.stopPropagation()
console.log('e.currentTarget.href', e.currentTarget.href)
const currentHref = e.currentTarget.href
if (accessToken && endpoint && order?.id) {
void getOrganizationConfig({
accessToken,
endpoint,
params: {
accessToken,
orderId: order?.id
}
}).then((config) => {
if (config?.links?.checkout) {
location.href = config.links.checkout
} else {
location.href = currentHref
}
})
} else {
location.href = currentHref
}
}
return children ? (
<Parent {...parentProps}>{children}</Parent>
) : (
<a href={href} {...p}>
<a href={href} onClick={handleClick} {...p}>
{label}
</a>
)
Expand Down

0 comments on commit 3a9eed0

Please sign in to comment.