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

Remove useCart and useCountries hooks #126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions app/components/CountrySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import {Listbox} from '@headlessui/react';
import {IconCaret, IconCheck} from './Icon';
import {useRef} from 'react';
import {getLocalizationFromLang} from '~/lib/utils';
import {useCountries} from '~/hooks/useCountries';
import {Heading} from '~/components';
import {Country} from '@shopify/hydrogen-ui-alpha/storefront-api-types';

export function CountrySelector() {
const countries = useCountries();
export function CountrySelector({countries}: {countries: Country[]}) {
const closeRef = useRef<HTMLButtonElement>(null);
const {pathname, search} = useLocation();
const {lang} = useParams();
Expand Down
64 changes: 48 additions & 16 deletions app/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ import {
CartEmpty,
Link,
} from '~/components';
import {useFetcher, useParams, Form, useFetchers} from '@remix-run/react';
import {
useFetcher,
useParams,
Form,
useFetchers,
Await,
useMatches,
} from '@remix-run/react';
import {useWindowScroll} from 'react-use';
import {Disclosure} from '@headlessui/react';
import type {LayoutData} from '~/data';
import {Suspense, useEffect, useMemo} from 'react';
import {useCart} from '~/hooks/useCart';
import {useIsHydrated} from '~/hooks/useIsHydrated';
import {Cart} from '@shopify/hydrogen-ui-alpha/storefront-api-types';

export function Layout({
children,
Expand Down Expand Up @@ -63,6 +70,7 @@ export function Layout({
function Header({title, menu}: {title: string; menu?: EnhancedMenu}) {
const isHome = useIsHomePath();
const fetchers = useFetchers();
const [root] = useMatches();

const {
isOpen: isCartOpen,
Expand Down Expand Up @@ -90,7 +98,11 @@ function Header({title, menu}: {title: string; menu?: EnhancedMenu}) {
return (
<>
<Suspense fallback={null}>
<CartDrawer isOpen={isCartOpen} onClose={closeCart} />
<Await resolve={root.data.cart}>
{(cart) => (
<CartDrawer cart={cart} isOpen={isCartOpen} onClose={closeCart} />
)}
</Await>
</Suspense>
{menu && (
<MenuDrawer isOpen={isMenuOpen} onClose={closeMenu} menu={menu} />
Expand All @@ -111,8 +123,15 @@ function Header({title, menu}: {title: string; menu?: EnhancedMenu}) {
);
}

function CartDrawer({isOpen, onClose}: {isOpen: boolean; onClose: () => void}) {
const cart = useCart();
function CartDrawer({
cart,
isOpen,
onClose,
}: {
cart: Cart;
isOpen: boolean;
onClose: () => void;
}) {
/**
* Whenever a component that uses a fetcher is _unmounted_, that fetcher is removed
* from the internal Remix cache. By defining the fetcher outside of the component,
Expand Down Expand Up @@ -217,6 +236,7 @@ function MobileHeader({
openMenu: () => void;
}) {
const {y} = useWindowScroll();
const [root] = useMatches();

const styles = {
button: 'relative flex items-center justify-center w-8 h-8',
Expand Down Expand Up @@ -274,7 +294,15 @@ function MobileHeader({
<Suspense
fallback={<Badge count={0} dark={isHome} openCart={openCart} />}
>
<CartBadge dark={isHome} openCart={openCart} />
<Await resolve={root.data.cart}>
{(cart) => (
<Badge
dark={isHome}
openCart={openCart}
count={cart.totalQuantity || 0}
/>
)}
</Await>
</Suspense>
</div>
</header>
Expand All @@ -294,6 +322,7 @@ function DesktopHeader({
}) {
const {y} = useWindowScroll();
const params = useParams();
const [root] = useMatches();

const styles = {
link: 'pb-1',
Expand Down Expand Up @@ -359,7 +388,15 @@ function DesktopHeader({
<Suspense
fallback={<Badge count={0} dark={isHome} openCart={openCart} />}
>
<CartBadge dark={isHome} openCart={openCart} />
<Await resolve={root.data.cart}>
{(cart) => (
<Badge
dark={isHome}
openCart={openCart}
count={cart.totalQuantity || 0}
/>
)}
</Await>
</Suspense>
</div>
</header>
Expand Down Expand Up @@ -416,15 +453,8 @@ function Badge({
);
}

function CartBadge({openCart, dark}: {dark: boolean; openCart: () => void}) {
const cart = useCart();

return (
<Badge openCart={openCart} count={cart?.totalQuantity || 0} dark={dark} />
);
}

function Footer({menu}: {menu?: EnhancedMenu}) {
const [root] = useMatches();
const isHome = useIsHomePath();
const itemsCount = menu
? menu?.items?.length + 1 > 4
Expand All @@ -443,7 +473,9 @@ function Footer({menu}: {menu?: EnhancedMenu}) {
>
<FooterMenu menu={menu} />
<Suspense fallback="Loading countries...">
<CountrySelector />
<Await resolve={root.data.countries}>
{(countries) => <CountrySelector countries={countries} />}
</Await>
</Suspense>
<div
className={`self-end pt-8 opacity-50 md:col-span-2 lg:col-span-${itemsCount}`}
Expand Down
20 changes: 0 additions & 20 deletions app/hooks/useCart.tsx

This file was deleted.

20 changes: 0 additions & 20 deletions app/hooks/useCountries.tsx

This file was deleted.

8 changes: 0 additions & 8 deletions app/hooks/useRouteData.tsx

This file was deleted.