Skip to content
Open
Changes from all commits
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
28 changes: 2 additions & 26 deletions templates/skeleton/app/components/CartSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {CartLayout} from '~/components/CartMain';
import {CartForm, Money, type OptimisticCart} from '@shopify/hydrogen';
import {useEffect, useRef} from 'react';
import {useFetcher} from 'react-router';
import type {FetcherWithComponents} from 'react-router';

type CartSummaryProps = {
cart: OptimisticCart<CartApiQueryFragment | null>;
Expand Down Expand Up @@ -110,27 +109,17 @@ function CartGiftCard({
}: {
giftCardCodes: CartApiQueryFragment['appliedGiftCards'] | undefined;
}) {
const appliedGiftCardCodes = useRef<string[]>([]);
const giftCardCodeInput = useRef<HTMLInputElement>(null);
const giftCardAddFetcher = useFetcher({key: 'gift-card-add'});

// Clear the gift card code input after the gift card is added
useEffect(() => {
if (giftCardAddFetcher.data) {
giftCardCodeInput.current!.value = '';
}
}, [giftCardAddFetcher.data]);
Comment on lines 115 to 119
Copy link
Contributor

Choose a reason for hiding this comment

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

im not a fan of this either but i guess there aren’t callbacks for fetcher to reset the form

we could pass a ref to the form and call form.reset() that would give me some peace of mind but overall they do the same so whatever, lets not change for the sake of changing it


function saveAppliedCode(code: string) {
const formattedCode = code.replace(/\s/g, ''); // Remove spaces
if (!appliedGiftCardCodes.current.includes(formattedCode)) {
appliedGiftCardCodes.current.push(formattedCode);
}
}

return (
<div>
{/* Display applied gift cards with individual remove buttons */}
{giftCardCodes && giftCardCodes.length > 0 && (
<dl>
<dt>Applied Gift Card(s)</dt>
Expand All @@ -148,11 +137,7 @@ function CartGiftCard({
</dl>
)}

{/* Show an input to apply a gift card */}
<AddGiftCardForm
saveAppliedCode={saveAppliedCode}
fetcherKey="gift-card-add"
>
<AddGiftCardForm fetcherKey="gift-card-add">
<div>
<input
type="text"
Expand All @@ -171,11 +156,9 @@ function CartGiftCard({
}

function AddGiftCardForm({
saveAppliedCode,
fetcherKey,
children,
}: {
saveAppliedCode?: (code: string) => void;
fetcherKey?: string;
children: React.ReactNode;
}) {
Expand All @@ -185,13 +168,7 @@ function AddGiftCardForm({
route="/cart"
action={CartForm.ACTIONS.GiftCardCodesAdd}
>
{(fetcher: FetcherWithComponents<any>) => {
const code = fetcher.formData?.get('giftCardCode');
if (code && saveAppliedCode) {
saveAppliedCode(code as string);
}
return children;
}}
{children}
</CartForm>
);
}
Expand All @@ -215,4 +192,3 @@ function RemoveGiftCardForm({
</CartForm>
);
}

Loading