-
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 #82 from buildheadless/collection
Add collection template
- Loading branch information
Showing
25 changed files
with
863 additions
and
333 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
templates/hydrogen-theme/app/components/sections/CollectionBannerSection.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,47 @@ | ||
import type {TypeFromSelection} from 'groqd'; | ||
|
||
import {useLoaderData} from '@remix-run/react'; | ||
import {Image} from '@shopify/hydrogen'; | ||
|
||
import type {SectionDefaultProps} from '~/lib/type'; | ||
import type {COLLECTION_BANNER_SECTION_FRAGMENT} from '~/qroq/sections'; | ||
import type {loader} from '~/routes/($locale).collections.$collectionHandle'; | ||
|
||
type CollectionBannerSectionProps = TypeFromSelection< | ||
typeof COLLECTION_BANNER_SECTION_FRAGMENT | ||
>; | ||
|
||
export function CollectionBannerSection( | ||
props: SectionDefaultProps & {data: CollectionBannerSectionProps}, | ||
) { | ||
const loaderData = useLoaderData<typeof loader>(); | ||
const collection = loaderData.collection; | ||
|
||
return collection ? ( | ||
<section> | ||
{/* Todo => add settings for banner height */} | ||
{/* Todo => add setting to add overlay */} | ||
{/* Todo => add settings for text and content alignment */} | ||
<div className="relative h-80 w-full overflow-hidden"> | ||
{props.data.showImage && collection.image && ( | ||
<Image | ||
aspectRatio="16/9" | ||
className="h-auto" | ||
crop="center" | ||
data={collection.image} | ||
loading="eager" | ||
sizes="100vw" | ||
/> | ||
)} | ||
<div className="absolute inset-0"> | ||
<div className="flex h-full items-center justify-center"> | ||
<div className="flex flex-col gap-1 text-center"> | ||
<h1>{collection.title}</h1> | ||
{props.data.showDescription && <p>{collection.description}</p>} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
) : null; | ||
} |
152 changes: 152 additions & 0 deletions
152
templates/hydrogen-theme/app/components/sections/CollectionProductGridSection.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,152 @@ | ||
import type {Filter} from '@shopify/hydrogen/storefront-api-types'; | ||
import type {TypeFromSelection} from 'groqd'; | ||
import type { | ||
CollectionProductGridQuery, | ||
ProductCardFragment, | ||
} from 'storefrontapi.generated'; | ||
|
||
import { | ||
Await, | ||
useLoaderData, | ||
useNavigate, | ||
useSearchParams, | ||
} from '@remix-run/react'; | ||
import {Pagination, flattenConnection} from '@shopify/hydrogen'; | ||
import {Suspense, useEffect} from 'react'; | ||
|
||
import type {SectionDefaultProps} from '~/lib/type'; | ||
import type {COLLECTION_PRODUCT_GRID_SECTION_FRAGMENT} from '~/qroq/sections'; | ||
import type {loader} from '~/routes/($locale).collections.$collectionHandle'; | ||
|
||
import {useLocale} from '~/hooks/useLocale'; | ||
import {getAppliedFilters} from '~/lib/shopifyCollection'; | ||
|
||
import {SortFilter} from '../collection/SortFilter'; | ||
import {ProductCardGrid} from '../product/ProductCardGrid'; | ||
|
||
type CollectionProductGridSectionProps = TypeFromSelection< | ||
typeof COLLECTION_PRODUCT_GRID_SECTION_FRAGMENT | ||
>; | ||
|
||
export type ShopifyCollection = CollectionProductGridQuery['collection']; | ||
|
||
export function CollectionProductGridSection( | ||
props: SectionDefaultProps & {data: CollectionProductGridSectionProps}, | ||
) { | ||
const locale = useLocale(); | ||
const [searchParams] = useSearchParams(); | ||
const loaderData = useLoaderData<typeof loader>(); | ||
const collectionProductGridPromise = loaderData?.collectionProductGridPromise; | ||
const columns = props.data.desktopColumns; | ||
const mobileColumns = props.data.mobileColumns; | ||
|
||
// Todo => Add skeleton and errorElement | ||
return ( | ||
<Suspense fallback="loading..."> | ||
<Await | ||
errorElement={<div>Error</div>} | ||
resolve={collectionProductGridPromise} | ||
> | ||
{(result) => { | ||
const collection = result?.collection as ShopifyCollection; | ||
|
||
if (!collection) { | ||
return null; | ||
} | ||
|
||
const appliedFilters = getAppliedFilters({ | ||
collection, | ||
locale, | ||
searchParams, | ||
}); | ||
|
||
return ( | ||
<div className="container"> | ||
<SortFilter | ||
appliedFilters={appliedFilters} | ||
filters={collection?.products.filters as Filter[]} | ||
> | ||
<Pagination connection={collection?.products}> | ||
{({ | ||
NextLink, | ||
PreviousLink, | ||
hasNextPage, | ||
isLoading, | ||
nextPageUrl, | ||
nodes, | ||
state, | ||
}) => ( | ||
<> | ||
<div className="mb-6 flex items-center justify-center"> | ||
<PreviousLink> | ||
{isLoading ? 'Loading...' : 'Load previous'} | ||
</PreviousLink> | ||
</div> | ||
<ProductsLoadedOnScroll | ||
columns={{ | ||
desktop: columns, | ||
mobile: mobileColumns, | ||
}} | ||
hasNextPage={hasNextPage} | ||
inView={true} | ||
nextPageUrl={nextPageUrl} | ||
nodes={nodes} | ||
state={state} | ||
/> | ||
<div className="mt-6 flex items-center justify-center"> | ||
<NextLink> | ||
{isLoading ? 'Loading...' : 'Load more products'} | ||
</NextLink> | ||
</div> | ||
</> | ||
)} | ||
</Pagination> | ||
</SortFilter> | ||
</div> | ||
); | ||
}} | ||
</Await> | ||
</Suspense> | ||
); | ||
} | ||
|
||
function ProductsLoadedOnScroll({ | ||
columns, | ||
hasNextPage, | ||
inView, | ||
nextPageUrl, | ||
nodes, | ||
state, | ||
}: { | ||
columns?: { | ||
desktop?: null | number; | ||
mobile?: null | number; | ||
}; | ||
hasNextPage: boolean; | ||
inView: boolean; | ||
nextPageUrl: string; | ||
nodes: ProductCardFragment[]; | ||
state: unknown; | ||
}) { | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (inView && hasNextPage) { | ||
navigate(nextPageUrl, { | ||
preventScrollReset: true, | ||
replace: true, | ||
state, | ||
}); | ||
} | ||
}, [inView, navigate, state, nextPageUrl, hasNextPage]); | ||
|
||
return ( | ||
<ProductCardGrid | ||
columns={{ | ||
desktop: columns?.desktop, | ||
mobile: columns?.mobile, | ||
}} | ||
products={nodes} | ||
/> | ||
); | ||
} |
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.