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

[skeleton] Make cart banner optimistic #2502

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Changes from 3 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
19 changes: 13 additions & 6 deletions templates/skeleton/app/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Suspense} from 'react';
import {Await, NavLink} from '@remix-run/react';
import {type CartViewPayload, useAnalytics} from '@shopify/hydrogen';
import {Await, NavLink, useAsyncValue} from '@remix-run/react';
import {
type CartViewPayload,
useAnalytics,
useOptimisticCart,
} from '@shopify/hydrogen';
import type {HeaderQuery, CartApiQueryFragment} from 'storefrontapi.generated';
import {useAside} from '~/components/Aside';

Expand Down Expand Up @@ -159,15 +163,18 @@ function CartToggle({cart}: Pick<HeaderProps, 'cart'>) {
return (
<Suspense fallback={<CartBadge count={null} />}>
<Await resolve={cart}>
{(cart) => {
if (!cart) return <CartBadge count={0} />;
return <CartBadge count={cart.totalQuantity || 0} />;
}}
<CartBanner />
</Await>
</Suspense>
);
}

function CartBanner() {
const originalCart = useAsyncValue() as CartApiQueryFragment | null;
const cart = useOptimisticCart(originalCart);
return <CartBadge count={cart?.totalQuantity ?? null} />;
frontsideair marked this conversation as resolved.
Show resolved Hide resolved
}

const FALLBACK_HEADER_MENU = {
id: 'gid://shopify/Menu/199655587896',
items: [
Expand Down
Loading