-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
233 additions
and
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { ModalFiltersMobile } from "views/ModalFiltersMobile"; | ||
import { useDeviceSize } from "utils/useDeviceSize"; | ||
import { CategoryFilterItem, FlavourFilterItem, StyleFilterItem } from "utils/groqTypes/ProductList"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
const noop = () => {}; | ||
|
||
const initialValues = { | ||
handleOpenModal: noop, | ||
handleCloseModal: noop, | ||
isModalOpen: false, | ||
}; | ||
|
||
const ModalFiltersContext = React.createContext(initialValues); | ||
|
||
export const useModalFilters = () => { | ||
const context = React.useContext(ModalFiltersContext); | ||
|
||
if (!context) { | ||
throw new Error("useModalFilters must be used within a ModalFiltersContext.Provider"); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
type Props = { | ||
categoryFilters: CategoryFilterItem[]; | ||
flavourFilters: FlavourFilterItem[]; | ||
styleFilters: StyleFilterItem[]; | ||
}; | ||
|
||
const ModalFiltersProvider = ({ | ||
children, | ||
categoryFilters, | ||
flavourFilters, | ||
styleFilters, | ||
}: React.PropsWithChildren<Props>) => { | ||
const { isSm } = useDeviceSize(); | ||
const [isModalOpen, setIsModalOpen] = React.useState(false); | ||
|
||
const handleOpenModal = React.useCallback(() => { | ||
setIsModalOpen(true); | ||
}, []); | ||
|
||
const handleCloseModal = React.useCallback(() => { | ||
setIsModalOpen(false); | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
// If modal is open and the window size changes to tablet/desktop viewport, | ||
// then closes the modal | ||
if (!isSm) { | ||
setIsModalOpen(false); | ||
} | ||
}, [isSm]); | ||
|
||
const value = React.useMemo( | ||
() => ({ | ||
handleOpenModal, | ||
handleCloseModal, | ||
isModalOpen, | ||
}), | ||
[handleCloseModal, handleOpenModal, isModalOpen] | ||
); | ||
|
||
return ( | ||
<ModalFiltersContext.Provider value={value}> | ||
{children} | ||
{/* Modal UI for filters (mobile only) */} | ||
<ModalFiltersMobile | ||
flavourFilters={flavourFilters} | ||
styleFilters={styleFilters} | ||
categoryFilters={categoryFilters} | ||
isOpen={isModalOpen} | ||
onClose={handleCloseModal} | ||
/> | ||
</ModalFiltersContext.Provider> | ||
); | ||
}; | ||
|
||
export default ModalFiltersProvider; |
4 changes: 3 additions & 1 deletion
4
packages/nextjs/components/Pagination.tsx → ...ages/nextjs/app/components/Pagination.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
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,35 @@ | ||
"use client"; | ||
|
||
import classNames from "classnames"; | ||
import { FadeInOut } from "../ui/shared-ui"; | ||
import { useSearchParams } from "next/navigation"; | ||
import { pluralize } from "utils/pluralize"; | ||
import { PLPVariant } from "utils/groqTypes/ProductList"; | ||
|
||
type Props = { | ||
variants: PLPVariant[]; | ||
}; | ||
|
||
const PaginationFade = ({ children, variants }: React.PropsWithChildren<Props>) => { | ||
const query = useSearchParams(); | ||
const productNames = pluralize(variants.map((prod) => prod.name)); | ||
|
||
return ( | ||
<FadeInOut | ||
className={classNames( | ||
"w-full grid grid-cols-2 sm:grid-cols-3 md:grid-cols-2 lg:grid-cols-3 gap-x-3 gap-y-9 mb-9", | ||
+(query?.get("page") || 1) > 1 && "grid-rows-2" | ||
)} | ||
key={productNames} | ||
> | ||
{children} | ||
{/* Add padder items when on page > 1 so pagination bar isn't moving around */} | ||
{+(query?.get("page") || 1) > 1 && | ||
Array.from({ length: 6 - variants.length }) | ||
.fill(undefined) | ||
.map((_, i) => <div key={i} className="invisible" />)} | ||
</FadeInOut> | ||
); | ||
}; | ||
|
||
export default PaginationFade; |
4 changes: 2 additions & 2 deletions
4
packages/nextjs/components/Product.tsx → packages/nextjs/app/components/Product.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
4 changes: 3 additions & 1 deletion
4
packages/nextjs/components/ProductSort.tsx → ...ges/nextjs/app/components/ProductSort.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
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,87 @@ | ||
import * as React from "react"; | ||
import { AnimatePresence } from "../ui/framer"; | ||
|
||
import { H6, WeDontSellBreadBanner } from "../ui/shared-ui"; | ||
import { CategoryFilterItem, FlavourFilterItem, PLPVariant, StyleFilterItem } from "utils/groqTypes/ProductList"; | ||
|
||
import { ProductFilters } from "components/ProductFilters/ProductFilters"; | ||
import { Product } from "app/components/Product"; | ||
import { Pagination } from "app/components/Pagination"; | ||
import { Breadcrumbs } from "components/Breadcrumbs"; | ||
import { SortAndFiltersToolbarMobile } from "app/components/SortAndFiltersToolbarMobile"; | ||
import { ProductSort } from "app/components/ProductSort"; | ||
import PaginationFade from "./PaginationFade"; | ||
|
||
interface ProductListProps { | ||
variants: PLPVariant[]; | ||
itemCount: number; | ||
pageSize: number; | ||
pageCount: number; | ||
currentPage?: number; | ||
categoryFilters: CategoryFilterItem[]; | ||
flavourFilters: FlavourFilterItem[]; | ||
styleFilters: StyleFilterItem[]; | ||
} | ||
|
||
const ProductList = ({ | ||
variants, | ||
pageCount, | ||
currentPage, | ||
categoryFilters, | ||
flavourFilters, | ||
styleFilters, | ||
}: ProductListProps) => { | ||
return ( | ||
<> | ||
<div> | ||
<WeDontSellBreadBanner /> | ||
<div className="py-9 container"> | ||
<h1 className="text-h1 text-primary mb-9">Products</h1> | ||
<section className="flex gap-9 flex-col md:flex-row"> | ||
<div className="hidden w-full md:w-72 order-2 md:order-1 md:flex flex-col gap-9"> | ||
<ProductSort showTitle /> | ||
<ProductFilters | ||
flavourFilters={flavourFilters} | ||
styleFilters={styleFilters} | ||
categoryFilters={categoryFilters} | ||
/> | ||
</div> | ||
|
||
<div className="flex-1 order-1 md:order-2"> | ||
<div className="mb-4"> | ||
<Breadcrumbs /> | ||
</div> | ||
|
||
{/** | ||
* | ||
* Product Sort (select) and product filters (mobile only). | ||
* See Modal component below | ||
* | ||
*/} | ||
<SortAndFiltersToolbarMobile /> | ||
|
||
<AnimatePresence mode="wait"> | ||
{variants.length > 0 && ( | ||
<PaginationFade variants={variants}> | ||
{variants.map((variant, index) => ( | ||
<Product key={variant._id} item={variant} priorityImage={index === 0} /> | ||
))} | ||
</PaginationFade> | ||
)} | ||
|
||
{variants.length === 0 && ( | ||
<div className="flex-1 flex flex-col justify-center items-center"> | ||
<H6 className="text-center">No products found</H6> | ||
</div> | ||
)} | ||
</AnimatePresence> | ||
{variants.length > 0 && <Pagination key="pagination" pageCount={pageCount} currentPage={currentPage} />} | ||
</div> | ||
</section> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default ProductList; |
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.