diff --git a/components/cart/edit-item-quantity-button.tsx b/components/cart/edit-item-quantity-button.tsx
index b743ab704..7046ff64f 100644
--- a/components/cart/edit-item-quantity-button.tsx
+++ b/components/cart/edit-item-quantity-button.tsx
@@ -5,6 +5,7 @@ import clsx from 'clsx';
import { updateItemQuantity } from 'components/cart/actions';
import LoadingDots from 'components/loading-dots';
import type { CartItem } from 'lib/shopify/types';
+import { useOptimistic, useState } from 'react';
import { useFormState, useFormStatus } from 'react-dom';
function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
@@ -29,29 +30,67 @@ function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
{pending ? (
) : type === 'plus' ? (
-
+
) : (
-
+
)}
);
}
-
-export function EditItemQuantityButton({ item, type }: { item: CartItem; type: 'plus' | 'minus' }) {
+export function EditItemQuantityButton({ item, type, onQuantityChange }: { item: CartItem; type: 'plus' | 'minus'; onQuantityChange: (quantity: number) => void }) {
const [message, formAction] = useFormState(updateItemQuantity, null);
- const payload = {
- lineId: item.id,
- variantId: item.merchandise.id,
- quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
- };
- const actionWithVariant = formAction.bind(null, payload);
+
+ const [optimisticQuantity, setOptimisticQuantity] = useOptimistic(item.quantity, (state: number, change: number) => state + change);
+
+ // const handleSubmit = async (event: React.FormEvent) => {
+ // event.preventDefault();
+ // const change = type === 'plus' ? 1 : -1;
+ // setOptimisticQuantity(change);
+ // onQuantityChange(optimisticQuantity + change);
+
+ // const updatedPayload = {
+ // lineId: item.id,
+ // variantId: item.merchandise.id,
+ // quantity: optimisticQuantity + change
+ // };
+ // await formAction(updatedPayload);
+ // };
+
return (
-
);
}
+
+export function EditItemQuantity({ item }: { item: CartItem }) {
+ const [quantity, setQuantity] = useState(item.quantity);
+
+ const handleQuantityChange = (newQuantity: number) => setQuantity(newQuantity);
+
+ return (
+
+ );
+}
diff --git a/components/cart/modal.tsx b/components/cart/modal.tsx
index a30818940..4fd54278e 100644
--- a/components/cart/modal.tsx
+++ b/components/cart/modal.tsx
@@ -11,7 +11,7 @@ import Link from 'next/link';
import { Fragment, useEffect, useRef, useState } from 'react';
import CloseCart from './close-cart';
import { DeleteItemButton } from './delete-item-button';
-import { EditItemQuantityButton } from './edit-item-quantity-button';
+import { EditItemQuantity } from './edit-item-quantity-button';
import OpenCart from './open-cart';
type MerchandiseSearchParams = {
@@ -74,13 +74,13 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
{!cart || cart.lines.length === 0 ? (
-
+
-
Your cart is empty.
+
Your cart is empty.
) : (
-
-
+
+
{cart.lines.map((item, i) => {
const merchandiseSearchParams = {} as MerchandiseSearchParams;
@@ -98,9 +98,9 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
return (
-
-
+
@@ -109,9 +109,9 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
onClick={closeCart}
className="z-30 flex flex-row space-x-4"
>
-
+
-
+
{item.merchandise.product.title}
@@ -133,19 +133,13 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
) : null}
-
+
-
-
-
- {item.quantity}
-
-
-
+
@@ -153,22 +147,22 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
})}
-
+
-
+
Shipping
Calculated at checkout
-
+
Total
@@ -176,7 +170,7 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
Proceed to Checkout