Chris Vibert d8b42acfa2
Commerce.js Provider (#548)
* commercejs: Initial commit with basic product list

* ui: Handle no variants on product

* commercejs: Support individual product pages

* commercejs: Use separate sdkFetch function

* commercejs: Show option hex colors based on option name

* commercejs: Support product search and filter

* commercejs: Enable carts feature

* commercejs: Remove unused API endpoints

* commercejs: Fix adding variants to cart

* commercejs: Fix types for update cart hook

* commercejs: Update README

* commercejs: Add sorting to product search

* commercejs: Add generic types to cart actions

* commercejs: Better cart normalization

* commercejs: Provide typing for sdkFetch function

* commercejs: Refactor product search logic

* commercejs: Update commercejs types package and export types from local directory

* commercejs: Remove unused checkout hooks

* commercejs: Enhance fetcher to allow custom API routes

* commercejs: Fix product types

* commercejs: Add checkout functionality

* commercejs: Add commercejs to README list of providers

* commercejs: Add login/logout auth hooks

* commercejs: Adds comment to sdkFetch function

* commercejs: Bring back empty useSignup hook to fix build

* commercejs: Refactor useCheckout hook logic

* commercejs: Add errors to fetcher function if using invalid resource/method

* commercejs: Remove use of hex colors for color variants

* ui: Fix undefined error when no variants

* commercejs: Handle add to cart when no variants

* commercejs: Enable customer auth feature

* commercejs: Rename public key env variable as commercejs

* commercejs: Remove duplicate customer fields

* commercejs: Use variants API to generate product variants

* commercejs: Fetch all products using sort order

* commercejs: Fix use of normalizeProduct function

* commercejs: Disable customer auth

* commercejs: Show selected variant details in cart view

* commercejs: Update to latest commercejs types

* commercejs: Fix login email

* commercejs: Remove unnecessary ts-ignore

* api: Allow parameter to be passed to login API

* api: Allow login handler to accept GET requests

* commercejs: Add login API for login callback email link

* commercejs: Remove unused argument to API

* commercejs: Add hook to fetch logged in customer

* commercejs: Rename token to match SDK name

* commercejs: Enable logout

* commercejs: Fix VERCEL_URL env variable

* commercejs: Fix using vercel deployment url

* commercejs: Add deployment url env vars to templates

* Replace yarn with npm

* commercejs: Allow checkout submit even without card/address details

* ui: Add loading and cart refresh to checkout

* commercejs: Leave link to issue on TODO comment

* Update docs/README/env.template for commercejs provider

* ui: Prevent toggle loading after component unmount

* commercejs: Handle product without images

* ui: Explicity set loading to false after checkout

* Revert "api: Allow parameter to be passed to login API"

This reverts commit c3713ec6e23f1b423a071a31221069995d419486.

* commercejs: Handle login using API redirect

* commercejs: Adds shipping and billing details to checkout data

* commercejs: Fix types for fetcher and submit checkout

* commercejs: Update README with demo url

* commercejs: Update checkout hooks to use checkout context

* commercejs: Update checkout logic to use customer fields

* ui: Clear checkout fields context after checkout

* commercejs: Remove unused clear checkout function

* commercejs: Import constants directly
2021-12-16 16:06:52 -03:00

123 lines
3.7 KiB
TypeScript

import Link from 'next/link'
import { FC, useState } from 'react'
import CartItem from '@components/cart/CartItem'
import { Button, Text } from '@components/ui'
import { useUI } from '@components/ui/context'
import SidebarLayout from '@components/common/SidebarLayout'
import useCart from '@framework/cart/use-cart'
import usePrice from '@framework/product/use-price'
import useCheckout from '@framework/checkout/use-checkout'
import ShippingWidget from '../ShippingWidget'
import PaymentWidget from '../PaymentWidget'
import s from './CheckoutSidebarView.module.css'
import { useCheckoutContext } from '../context'
const CheckoutSidebarView: FC = () => {
const [loadingSubmit, setLoadingSubmit] = useState(false)
const { setSidebarView, closeSidebar } = useUI()
const { data: cartData, revalidate: refreshCart } = useCart()
const { data: checkoutData, submit: onCheckout } = useCheckout()
const { clearCheckoutFields } = useCheckoutContext()
async function handleSubmit(event: React.ChangeEvent<HTMLFormElement>) {
try {
setLoadingSubmit(true)
event.preventDefault()
await onCheckout()
clearCheckoutFields()
setLoadingSubmit(false)
refreshCart()
closeSidebar()
} catch {
// TODO - handle error UI here.
setLoadingSubmit(false)
}
}
const { price: subTotal } = usePrice(
cartData && {
amount: Number(cartData.subtotalPrice),
currencyCode: cartData.currency.code,
}
)
const { price: total } = usePrice(
cartData && {
amount: Number(cartData.totalPrice),
currencyCode: cartData.currency.code,
}
)
return (
<SidebarLayout
className={s.root}
handleBack={() => setSidebarView('CART_VIEW')}
>
<div className="px-4 sm:px-6 flex-1">
<Link href="/cart">
<a>
<Text variant="sectionHeading">Checkout</Text>
</a>
</Link>
<PaymentWidget
isValid={checkoutData?.hasPayment}
onClick={() => setSidebarView('PAYMENT_VIEW')}
/>
<ShippingWidget
isValid={checkoutData?.hasShipping}
onClick={() => setSidebarView('SHIPPING_VIEW')}
/>
<ul className={s.lineItemsList}>
{cartData!.lineItems.map((item: any) => (
<CartItem
key={item.id}
item={item}
currencyCode={cartData!.currency.code}
variant="display"
/>
))}
</ul>
</div>
<form
onSubmit={handleSubmit}
className="flex-shrink-0 px-6 py-6 sm:px-6 sticky z-20 bottom-0 w-full right-0 left-0 bg-accent-0 border-t text-sm"
>
<ul className="pb-2">
<li className="flex justify-between py-1">
<span>Subtotal</span>
<span>{subTotal}</span>
</li>
<li className="flex justify-between py-1">
<span>Taxes</span>
<span>Calculated at checkout</span>
</li>
<li className="flex justify-between py-1">
<span>Shipping</span>
<span className="font-bold tracking-wide">FREE</span>
</li>
</ul>
<div className="flex justify-between border-t border-accent-2 py-3 font-bold mb-2">
<span>Total</span>
<span>{total}</span>
</div>
<div>
{/* Once data is correctly filled */}
<Button
type="submit"
width="100%"
disabled={!checkoutData?.hasPayment || !checkoutData?.hasShipping}
loading={loadingSubmit}
>
Confirm Purchase
</Button>
</div>
</form>
</SidebarLayout>
)
}
export default CheckoutSidebarView