Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout link provided by organization config #558

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading