-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultipassCheckoutButton.tsx
54 lines (47 loc) · 1.32 KB
/
MultipassCheckoutButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, {useCallback, forwardRef} from 'react';
import {multipass} from '~/lib/multipass/multipass';
type MultipassCheckoutButtonProps = {
as?: keyof React.ElementType;
checkoutUrl: string;
children: React.ReactNode;
onClick?: () => void;
redirect?: boolean;
};
/*
This component attempts to persist the customer session
state in the checkout by using multipass.
Note: multipass checkout is a Shopify Plus+ feature only.
*/
export const MultipassCheckoutButton = forwardRef(
(props: MultipassCheckoutButtonProps, ref: React.ReactElement) => {
const {
children,
onClick,
checkoutUrl,
redirect = true,
as = 'button',
} = props;
const Element: keyof React.ElementType = as;
const checkoutHandler = useCallback(
async (event) => {
event.preventDefault();
if (!checkoutUrl) return;
if (typeof onClick === 'function') {
onClick();
}
// If they user is logged in we persist it in the checkout,
// otherwise we log them out of the checkout too.
return await multipass({
return_to: checkoutUrl,
redirect,
});
},
[redirect, checkoutUrl, onClick],
);
return (
<Element ref={ref} onClick={checkoutHandler}>
{children}
</Element>
);
},
);