-
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.
Merge pull request #38 from buildheadless/thomas/bhead-33-feattheme-a…
…dd-collection-list-section feat:(theme) Add collection list section
- Loading branch information
Showing
20 changed files
with
274 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
templates/hydrogen-theme/app/components/CollectionCard.tsx
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,23 @@ | ||
import {Link} from '@remix-run/react'; | ||
import {Image} from '@shopify/hydrogen'; | ||
import type {CollectionsQuery} from 'storefrontapi.generated'; | ||
|
||
export function CollectionCard(props: { | ||
collection: CollectionsQuery['collections']['nodes'][0]; | ||
className?: string; | ||
}) { | ||
const {collection} = props; | ||
|
||
return ( | ||
<div className="overflow-hidden rounded-lg border"> | ||
<Link to={`/collections/${collection.handle}`}> | ||
{collection.image && ( | ||
<Image aspectRatio="16/9" sizes="33vw" data={collection.image} /> | ||
)} | ||
<div className="p-3"> | ||
<div className="text-lg">{collection.title}</div> | ||
</div> | ||
</Link> | ||
</div> | ||
); | ||
} |
19 changes: 19 additions & 0 deletions
19
templates/hydrogen-theme/app/components/CollectionListGrid.tsx
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,19 @@ | ||
import type {CollectionsQuery} from 'storefrontapi.generated'; | ||
import {flattenConnection} from '@shopify/hydrogen'; | ||
import {CollectionCard} from './CollectionCard'; | ||
|
||
export function CollectionListGrid(props: { | ||
collections?: CollectionsQuery['collections']; | ||
}) { | ||
const collections = flattenConnection(props.collections); | ||
|
||
return collections?.length > 0 ? ( | ||
<ul className="grid grid-cols-3 gap-10"> | ||
{collections.map((collection) => ( | ||
<li key={collection.id}> | ||
<CollectionCard collection={collection} /> | ||
</li> | ||
))} | ||
</ul> | ||
) : null; | ||
} |
44 changes: 44 additions & 0 deletions
44
templates/hydrogen-theme/app/components/sections/CollectionListSection.tsx
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,44 @@ | ||
import type {TypeFromSelection} from 'groqd'; | ||
|
||
import type {COLLECTION_LIST_SECTION_FRAGMENT} from '~/qroq/sections'; | ||
import type {loader as indexLoader} from '../../routes/_index'; | ||
import type {SectionDefaultProps} from '~/lib/type'; | ||
import {Await, useLoaderData} from '@remix-run/react'; | ||
import {Suspense} from 'react'; | ||
import {CollectionListGrid} from '../CollectionListGrid'; | ||
|
||
type CollectionListSectionProps = TypeFromSelection< | ||
typeof COLLECTION_LIST_SECTION_FRAGMENT | ||
>; | ||
|
||
export function CollectionListSection( | ||
props: SectionDefaultProps & {data: CollectionListSectionProps}, | ||
) { | ||
const loaderData = useLoaderData<typeof indexLoader>(); | ||
const collectionListPromise = loaderData?.collectionListPromise; | ||
const sectionGids = props.data.collections | ||
?.map((collection) => collection.store.gid) | ||
.join(','); | ||
|
||
return collectionListPromise ? ( | ||
<Suspense fallback={<div className="container">Loading...</div>}> | ||
<Await resolve={collectionListPromise}> | ||
{(data) => { | ||
// Resolve the collection list data from Shopify with the gids from Sanity | ||
const collections = data.find(({collections}) => { | ||
const gids = collections.nodes | ||
.map((collection) => collection.id) | ||
.join(','); | ||
return sectionGids === gids ? collections : null; | ||
})?.collections; | ||
|
||
return ( | ||
<div className="container"> | ||
<CollectionListGrid collections={collections} /> | ||
</div> | ||
); | ||
}} | ||
</Await> | ||
</Suspense> | ||
) : null; | ||
} |
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
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
Oops, something went wrong.