Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add loading state to remove button in Cart #750

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-guests-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Add loading state to remvoe item button in Cart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove*

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Trash } from 'lucide-react';
import { getTranslations } from 'next-intl/server';

import { getCart } from '~/client/queries/get-cart';
Expand All @@ -8,6 +7,7 @@ import { BcImage } from '~/components/bc-image';
import { removeProduct } from '../_actions/remove-products';

import { CartItemCounter } from './cart-item-counter';
import { RemoveFromCartButton } from './remove-from-cart-button';

export type Product =
| ExistingResultType<typeof getCart>['lineItems']['physicalItems'][number]
Expand Down Expand Up @@ -109,9 +109,7 @@ export const CartItem = async ({

<form action={removeProduct}>
<input name="lineItemEntityId" type="hidden" value={product.entityId} />
<button aria-label={t('removeFromCart')} type="submit">
<Trash aria-hidden="true" />
</button>
<RemoveFromCartButton label={t('removeFromCart')} spinnerLabel={t('spinnerText')} />
</form>
</div>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use client';

import { Loader2 as Spinner, Trash } from 'lucide-react';
import { useFormStatus } from 'react-dom';

export const RemoveFromCartButton = ({
label,
spinnerLabel,
}: {
label: string;
spinnerLabel: string;
}) => {
const { pending } = useFormStatus();

return (
<button
aria-label={label}
className="items-center hover:text-primary focus-visible:outline-none focus-visible:ring-4 focus-visible:ring-primary/20"
disabled={pending}
type="submit"
>
{pending ? (
<>
<Spinner aria-hidden="true" className="animate-spin text-primary " />
<span className="sr-only">{spinnerLabel}</span>
</>
) : (
<Trash aria-hidden="true" />
)}
</button>
);
};
Loading