4
0
forked from crowetic/commerce

Add checkout button

This commit is contained in:
Luis Alvarez 2020-10-09 22:40:12 -05:00
parent a92dcb851b
commit 2030255c5e
3 changed files with 21 additions and 4 deletions

View File

@ -6,10 +6,11 @@ import { ArrowLeft, Bag, Cross, Check } from '@components/icon'
import { useUI } from '@components/ui/context' import { useUI } from '@components/ui/context'
import useCart from '@lib/bigcommerce/cart/use-cart' import useCart from '@lib/bigcommerce/cart/use-cart'
import CartItem from '../CartItem' import CartItem from '../CartItem'
import Link from '@components/ui/Link' import useOpenCheckout from '@lib/bigcommerce/cart/use-open-checkout'
const CartSidebarView: FC = () => { const CartSidebarView: FC = () => {
const { data, isEmpty } = useCart() const { data, isEmpty } = useCart()
const openCheckout = useOpenCheckout()
const { closeSidebar } = useUI() const { closeSidebar } = useUI()
const items = data?.line_items.physical_items ?? [] const items = data?.line_items.physical_items ?? []
const handleClose = () => closeSidebar() const handleClose = () => closeSidebar()
@ -90,7 +91,12 @@ const CartSidebarView: FC = () => {
</ul> </ul>
</div> </div>
<div className="flex-shrink-0 px-4 border-t border-gray-200 py-5 sm:px-6"> <div className="flex-shrink-0 px-4 border-t border-gray-200 py-5 sm:px-6">
<Button href="/checkout" Component={Link} width="100%"> <Button
width="100%"
onClick={() => {
openCheckout()
}}
>
Proceed to Checkout Proceed to Checkout
</Button> </Button>
</div> </div>

View File

@ -35,7 +35,7 @@ const Button: React.FC<ButtonProps> = forwardRef((props, buttonRef) => {
const ref = useRef<typeof Component>(null) const ref = useRef<typeof Component>(null)
const { buttonProps, isPressed } = useButton( const { buttonProps, isPressed } = useButton(
{ {
...props, ...rest,
// @ts-ignore onClick === onPress for our purposes // @ts-ignore onClick === onPress for our purposes
onPress: onClick, onPress: onClick,
isDisabled: disabled, isDisabled: disabled,
@ -59,7 +59,6 @@ const Button: React.FC<ButtonProps> = forwardRef((props, buttonRef) => {
aria-pressed={active} aria-pressed={active}
data-variant={variant} data-variant={variant}
ref={mergeRefs([ref, buttonRef])} ref={mergeRefs([ref, buttonRef])}
{...rest}
{...buttonProps} {...buttonProps}
style={{ style={{
width, width,

View File

@ -0,0 +1,12 @@
import useCart from './use-cart'
export default function useOpenCheckout() {
const { data, mutate } = useCart()
return async function openCheckout() {
window.open(data?.redirect_urls.checkout_url)
console.log('URL1', data?.redirect_urls.checkout_url)
// Get new redirect urls
await mutate()
}
}