-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Shopify base template Ran `npm create @shopify/hydrogen@latest` with: ``` ? Select a language: ✔ TypeScript ? Install dependencies with npm? ✔ Yes ? Create a global `h2` alias to run commands instead of `npx shopify hydrogen` ? ✔ No ? Do you want to scaffold routes and core functionality? ✔ Yes, set up now ? Select a URL structure to support multiple markets: ✔ Set up later (run `npx shopify hydrogen setup markets`) ``` * Fix typo in page title Shopify/hydrogen#2016
- Loading branch information
Showing
17 changed files
with
2,726 additions
and
2,216 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import {Link, useLoaderData, type MetaFunction} from '@remix-run/react'; | ||
import { | ||
Image, | ||
Money, | ||
Pagination, | ||
getPaginationVariables, | ||
} from '@shopify/hydrogen'; | ||
import {json, type LoaderFunctionArgs} from '@shopify/remix-oxygen'; | ||
import type {ProductItemFragment} from 'storefrontapi.generated'; | ||
import {useVariantUrl} from '~/lib/variants'; | ||
|
||
export const meta: MetaFunction<typeof loader> = () => { | ||
return [{title: `Hydrogen | Products`}]; | ||
}; | ||
|
||
export async function loader({request, context}: LoaderFunctionArgs) { | ||
const {storefront} = context; | ||
const paginationVariables = getPaginationVariables(request, { | ||
pageBy: 8, | ||
}); | ||
|
||
const {products} = await storefront.query(CATALOG_QUERY, { | ||
variables: {...paginationVariables}, | ||
}); | ||
|
||
return json({products}); | ||
} | ||
|
||
export default function Collection() { | ||
const {products} = useLoaderData<typeof loader>(); | ||
|
||
return ( | ||
<div className="collection"> | ||
<h1>Products</h1> | ||
<Pagination connection={products}> | ||
{({nodes, isLoading, PreviousLink, NextLink}) => ( | ||
<> | ||
<PreviousLink> | ||
{isLoading ? 'Loading...' : <span>↑ Load previous</span>} | ||
</PreviousLink> | ||
<ProductsGrid products={nodes} /> | ||
<br /> | ||
<NextLink> | ||
{isLoading ? 'Loading...' : <span>Load more ↓</span>} | ||
</NextLink> | ||
</> | ||
)} | ||
</Pagination> | ||
</div> | ||
); | ||
} | ||
|
||
function ProductsGrid({products}: {products: ProductItemFragment[]}) { | ||
return ( | ||
<div className="products-grid"> | ||
{products.map((product, index) => { | ||
return ( | ||
<ProductItem | ||
key={product.id} | ||
product={product} | ||
loading={index < 8 ? 'eager' : undefined} | ||
/> | ||
); | ||
})} | ||
</div> | ||
); | ||
} | ||
|
||
function ProductItem({ | ||
product, | ||
loading, | ||
}: { | ||
product: ProductItemFragment; | ||
loading?: 'eager' | 'lazy'; | ||
}) { | ||
const variant = product.variants.nodes[0]; | ||
const variantUrl = useVariantUrl(product.handle, variant.selectedOptions); | ||
return ( | ||
<Link | ||
className="product-item" | ||
key={product.id} | ||
prefetch="intent" | ||
to={variantUrl} | ||
> | ||
{product.featuredImage && ( | ||
<Image | ||
alt={product.featuredImage.altText || product.title} | ||
aspectRatio="1/1" | ||
data={product.featuredImage} | ||
loading={loading} | ||
sizes="(min-width: 45em) 400px, 100vw" | ||
/> | ||
)} | ||
<h4>{product.title}</h4> | ||
<small> | ||
<Money data={product.priceRange.minVariantPrice} /> | ||
</small> | ||
</Link> | ||
); | ||
} | ||
|
||
const PRODUCT_ITEM_FRAGMENT = `#graphql | ||
fragment MoneyProductItem on MoneyV2 { | ||
amount | ||
currencyCode | ||
} | ||
fragment ProductItem on Product { | ||
id | ||
handle | ||
title | ||
featuredImage { | ||
id | ||
altText | ||
url | ||
width | ||
height | ||
} | ||
priceRange { | ||
minVariantPrice { | ||
...MoneyProductItem | ||
} | ||
maxVariantPrice { | ||
...MoneyProductItem | ||
} | ||
} | ||
variants(first: 1) { | ||
nodes { | ||
selectedOptions { | ||
name | ||
value | ||
} | ||
} | ||
} | ||
} | ||
` as const; | ||
|
||
// NOTE: https://shopify.dev/docs/api/storefront/2024-01/objects/product | ||
const CATALOG_QUERY = `#graphql | ||
query Catalog( | ||
$country: CountryCode | ||
$language: LanguageCode | ||
$first: Int | ||
$last: Int | ||
$startCursor: String | ||
$endCursor: String | ||
) @inContext(country: $country, language: $language) { | ||
products(first: $first, last: $last, before: $startCursor, after: $endCursor) { | ||
nodes { | ||
...ProductItem | ||
} | ||
pageInfo { | ||
hasPreviousPage | ||
hasNextPage | ||
startCursor | ||
endCursor | ||
} | ||
} | ||
} | ||
${PRODUCT_ITEM_FRAGMENT} | ||
` as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.