Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Oct 2, 2023
1 parent 3f0ff19 commit 4687b98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
16 changes: 4 additions & 12 deletions components/cart/add-to-cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { addItem } from 'components/cart/actions';
import LoadingDots from 'components/loading-dots';
import { ProductVariant } from 'lib/shopify/types';
import { useSearchParams } from 'next/navigation';
import { useTransition } from 'react';
import {
// @ts-ignore
experimental_useFormState as useFormState,
Expand Down Expand Up @@ -50,7 +49,9 @@ function SubmitButton({

return (
<button
type="submit"
onClick={(e: React.FormEvent<HTMLButtonElement>) => {
if (pending) e.preventDefault();
}}
aria-label="Add to cart"
aria-disabled={pending}
className={clsx(buttonClasses, {
Expand All @@ -73,7 +74,6 @@ export function AddToCart({
variants: ProductVariant[];
availableForSale: boolean;
}) {
const [isPending, startTransition] = useTransition();
const [message, formAction] = useFormState(addItem, null);
const searchParams = useSearchParams();
const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
Expand All @@ -85,16 +85,8 @@ export function AddToCart({
const selectedVariantId = variant?.id || defaultVariantId;
const actionWithVariant = formAction.bind(null, selectedVariantId);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
if (isPending) {
event.preventDefault();
} else {
startTransition(actionWithVariant);
}
};

return (
<form action={actionWithVariant} onSubmit={handleSubmit}>
<form action={actionWithVariant}>
<SubmitButton availableForSale={availableForSale} selectedVariantId={selectedVariantId} />
<p aria-live="polite" className="sr-only" role="status">
{message}
Expand Down
15 changes: 4 additions & 11 deletions components/cart/delete-item-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import clsx from 'clsx';
import { removeItem } from 'components/cart/actions';
import LoadingDots from 'components/loading-dots';
import type { CartItem } from 'lib/shopify/types';
import { useTransition } from 'react';
import {
// @ts-ignore
experimental_useFormState as useFormState,
Expand All @@ -18,6 +17,9 @@ function SubmitButton() {
return (
<button
type="submit"
onClick={(e: React.FormEvent<HTMLButtonElement>) => {
if (pending) e.preventDefault();
}}
aria-label="Remove cart item"
aria-disabled={pending}
className={clsx(
Expand All @@ -37,21 +39,12 @@ function SubmitButton() {
}

export function DeleteItemButton({ item }: { item: CartItem }) {
const [isPending, startTransition] = useTransition();
const [message, formAction] = useFormState(removeItem, null);
const itemId = item.id;
const actionWithVariant = formAction.bind(null, itemId);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
if (isPending) {
event.preventDefault();
} else {
startTransition(actionWithVariant);
}
};

return (
<form action={actionWithVariant} onSubmit={handleSubmit}>
<form action={actionWithVariant}>
<SubmitButton />
<p aria-live="polite" className="sr-only" role="status">
{message}
Expand Down
15 changes: 4 additions & 11 deletions components/cart/edit-item-quantity-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import clsx from 'clsx';
import { updateItemQuantity } from 'components/cart/actions';
import LoadingDots from 'components/loading-dots';
import type { CartItem } from 'lib/shopify/types';
import { useTransition } from 'react';
import {
// @ts-ignore
experimental_useFormState as useFormState,
Expand All @@ -18,6 +17,9 @@ function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
return (
<button
type="submit"
onClick={(e: React.FormEvent<HTMLButtonElement>) => {
if (pending) e.preventDefault();
}}
aria-label={type === 'plus' ? 'Increase item quantity' : 'Reduce item quantity'}
aria-disabled={pending}
className={clsx(
Expand All @@ -40,7 +42,6 @@ function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
}

export function EditItemQuantityButton({ item, type }: { item: CartItem; type: 'plus' | 'minus' }) {
const [isPending, startTransition] = useTransition();
const [message, formAction] = useFormState(updateItemQuantity, null);
const payload = {
lineId: item.id,
Expand All @@ -49,16 +50,8 @@ export function EditItemQuantityButton({ item, type }: { item: CartItem; type: '
};
const actionWithVariant = formAction.bind(null, payload);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
if (isPending) {
event.preventDefault();
} else {
startTransition(actionWithVariant);
}
};

return (
<form action={actionWithVariant} onSubmit={handleSubmit}>
<form action={actionWithVariant}>
<SubmitButton type={type} />
<p aria-live="polite" className="sr-only" role="status">
{message}
Expand Down

0 comments on commit 4687b98

Please sign in to comment.