From 0ebf071826c6c33a804bcc8a99f970e856cb12f2 Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Thu, 25 Jul 2024 13:56:53 -0500 Subject: [PATCH] Optimistic cart (#1364) --- components/cart/actions.ts | 6 ++ components/cart/edit-item-quantity-button.tsx | 31 +++--- components/cart/modal.tsx | 98 ++++++++++++++++--- 3 files changed, 109 insertions(+), 26 deletions(-) diff --git a/components/cart/actions.ts b/components/cart/actions.ts index fa2c34d37d..ea6d82dd79 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -4,6 +4,7 @@ import { TAGS } from 'lib/constants'; import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify'; import { revalidateTag } from 'next/cache'; import { cookies } from 'next/headers'; +import { redirect } from 'next/navigation'; export async function addItem(prevState: any, selectedVariantId: string | undefined) { let cartId = cookies().get('cartId')?.value; @@ -81,3 +82,8 @@ export async function updateItemQuantity( return 'Error updating item quantity'; } } + +export async function redirectToCheckout(formData: FormData) { + const url = formData.get('url') as string; + redirect(url); +} diff --git a/components/cart/edit-item-quantity-button.tsx b/components/cart/edit-item-quantity-button.tsx index b743ab7042..d3c753dd7a 100644 --- a/components/cart/edit-item-quantity-button.tsx +++ b/components/cart/edit-item-quantity-button.tsx @@ -3,32 +3,22 @@ import { MinusIcon, PlusIcon } from '@heroicons/react/24/outline'; import clsx from 'clsx'; import { updateItemQuantity } from 'components/cart/actions'; -import LoadingDots from 'components/loading-dots'; import type { CartItem } from 'lib/shopify/types'; -import { useFormState, useFormStatus } from 'react-dom'; +import { useFormState } from 'react-dom'; function SubmitButton({ type }: { type: 'plus' | 'minus' }) { - const { pending } = useFormStatus(); - return ( @@ -140,11 +192,19 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) { currencyCode={item.cost.totalAmount.currencyCode} />
- +

{item.quantity}

- +
@@ -174,12 +234,9 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) { /> - - Proceed to Checkout - + + + )} @@ -189,3 +246,20 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) { ); } + +function CheckoutButton({ cart }: { cart: Cart }) { + const { pending } = useFormStatus(); + + return ( + <> + + + + ); +}