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

Fix cart route so it works with no-js #2665

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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: 5 additions & 0 deletions .changeset/nervous-poets-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skeleton': patch
---

Fix cart route so that it works with no-js
37 changes: 17 additions & 20 deletions docs/shopify-dev/analytics-setup/js/app/routes/cart.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Await, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {useLoaderData} from '@remix-run/react';
import {CartForm, Analytics} from '@shopify/hydrogen';
import {json} from '@shopify/remix-oxygen';
import {CartMain} from '~/components/CartMain';
Expand Down Expand Up @@ -95,33 +94,31 @@ export async function action({request, context}) {
);
}

/**
* @param {LoaderFunctionArgs} args
*/
export async function loader({context}) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
/** @type {RootLoader} */
const rootData = useRouteLoaderData('root');
if (!rootData) return null;
/** @type {LoaderFunctionArgs} */
const cart = useLoaderData();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
{/* [START cart] */}
<Analytics.CartView />
{/* [END cart] */}
<CartMain layout="page" cart={cart} />
{/* [START cart] */}
<Analytics.CartView />
{/* [END cart] */}
</div>
);
}

/** @template T @typedef {import('@remix-run/react').MetaFunction<T>} MetaFunction */
/** @typedef {import('@shopify/hydrogen').CartQueryDataReturn} CartQueryDataReturn */
/** @typedef {import('@shopify/remix-oxygen').ActionFunctionArgs} ActionFunctionArgs */
/** @typedef {import('~/root').RootLoader} RootLoader */
/** @typedef {import('@shopify/remix-oxygen').LoaderFunctionArgs} LoaderFunctionArgs */
/** @typedef {import('@shopify/remix-oxygen').SerializeFrom<typeof action>} ActionReturnData */
/** @typedef {import('@shopify/remix-oxygen').SerializeFrom<typeof loader>} LoaderReturnData */
25 changes: 9 additions & 16 deletions docs/shopify-dev/analytics-setup/ts/app/routes/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Await, type MetaFunction, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {type MetaFunction, useLoaderData} from '@remix-run/react';
import type {CartQueryDataReturn} from '@shopify/hydrogen';
import {CartForm, Analytics} from '@shopify/hydrogen';
import {json, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {json, type LoaderFunctionArgs, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {CartMain} from '~/components/CartMain';
import type {RootLoader} from '~/root';

export const meta: MetaFunction = () => {
return [{title: `Hydrogen | Cart`}];
Expand Down Expand Up @@ -95,23 +93,18 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

export async function loader({context}: LoaderFunctionArgs) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
const rootData = useRouteLoaderData<RootLoader>('root');
if (!rootData) return null;
const cart = useLoaderData<typeof loader>();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
<CartMain layout="page" cart={cart} />
{/* [START cart] */}
<Analytics.CartView />
{/* [END cart] */}
Expand Down
24 changes: 9 additions & 15 deletions examples/custom-cart-method/app/routes/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Await, type MetaFunction, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {type MetaFunction, useLoaderData} from '@remix-run/react';
import type {CartQueryDataReturn} from '@shopify/hydrogen';
import {CartForm} from '@shopify/hydrogen';
import {json, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {json, type LoaderFunctionArgs, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import type {
SelectedOptionInput,
CartLineUpdateInput,
Expand Down Expand Up @@ -116,23 +115,18 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

export async function loader({context}: LoaderFunctionArgs) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
const rootData = useRouteLoaderData<RootLoader>('root');
if (!rootData) return null;
const cart = useLoaderData<typeof loader>();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
<CartMain layout="page" cart={cart} />
</div>
);
}
24 changes: 9 additions & 15 deletions examples/legacy-customer-account-flow/app/routes/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Await, type MetaFunction, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {type MetaFunction, useLoaderData} from '@remix-run/react';
import type {CartQueryDataReturn} from '@shopify/hydrogen';
import {CartForm} from '@shopify/hydrogen';
import {json, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {json, type LoaderFunctionArgs, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {CartMain} from '~/components/CartMain';
import type {RootLoader} from '~/root';

Expand Down Expand Up @@ -106,23 +105,18 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

export async function loader({context}: LoaderFunctionArgs) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
const rootData = useRouteLoaderData<RootLoader>('root');
if (!rootData) return null;
const cart = useLoaderData<typeof loader>();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
<CartMain layout="page" cart={cart} />
</div>
);
}
25 changes: 9 additions & 16 deletions examples/multipass/app/routes/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Await, type MetaFunction, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {type MetaFunction, useLoaderData} from '@remix-run/react';
import type {CartQueryDataReturn} from '@shopify/hydrogen';
import {CartForm} from '@shopify/hydrogen';
import {json, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {json, type LoaderFunctionArgs, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {CartMain} from '~/components/Cart';
import type {RootLoader} from '~/root';

export const meta: MetaFunction = () => {
return [{title: `Hydrogen | Cart`}];
Expand Down Expand Up @@ -106,23 +104,18 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

export async function loader({context}: LoaderFunctionArgs) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
const rootData = useRouteLoaderData<RootLoader>('root');
if (!rootData) return null;
const cart = useLoaderData<typeof loader>();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
<CartMain layout="page" cart={cart} />
</div>
);
}
25 changes: 9 additions & 16 deletions templates/skeleton/app/routes/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Await, type MetaFunction, useRouteLoaderData} from '@remix-run/react';
import {Suspense} from 'react';
import {type MetaFunction, useLoaderData} from '@remix-run/react';
import type {CartQueryDataReturn} from '@shopify/hydrogen';
import {CartForm} from '@shopify/hydrogen';
import {json, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {json, type LoaderFunctionArgs, type ActionFunctionArgs} from '@shopify/remix-oxygen';
import {CartMain} from '~/components/CartMain';
import type {RootLoader} from '~/root';

export const meta: MetaFunction = () => {
return [{title: `Hydrogen | Cart`}];
Expand Down Expand Up @@ -95,23 +93,18 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

export async function loader({context}: LoaderFunctionArgs) {
const {cart} = context;
return json(await cart.get());
}

export default function Cart() {
const rootData = useRouteLoaderData<RootLoader>('root');
if (!rootData) return null;
const cart = useLoaderData<typeof loader>();

return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await
resolve={rootData.cart}
errorElement={<div>An error occurred</div>}
>
{(cart) => {
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
<CartMain layout="page" cart={cart} />
</div>
);
}
Loading