forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces dropdowns above the product grid to filter the results by facets defined in Vendure
- Loading branch information
Showing
33 changed files
with
2,698 additions
and
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"$schema": "https://ui.shadcn.com/schema.json", | ||
"style": "new-york", | ||
"rsc": true, | ||
"tsx": true, | ||
"tailwind": { | ||
"config": "tailwind.config.ts", | ||
"css": "app/globals.css", | ||
"baseColor": "neutral", | ||
"cssVariables": true, | ||
"prefix": "" | ||
}, | ||
"aliases": { | ||
"components": "@/ui-components", | ||
"utils": "@/ui-components/lib/utils", | ||
"ui": "@/ui-components/ui", | ||
"lib": "@/ui-components/lib", | ||
"hooks": "@/ui-components/hooks" | ||
}, | ||
"iconLibrary": "lucide" | ||
} |
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,26 @@ | ||
'use client'; | ||
|
||
import { createContext, useContext } from 'react'; | ||
import { CollectionFragment } from '@/lib/vendure/types'; | ||
|
||
const CollectionContext = createContext<CollectionFragment | undefined | null>(undefined); | ||
|
||
export function CollectionProvider({ | ||
children, | ||
collection | ||
}: { | ||
children: any; | ||
collection: CollectionFragment | undefined | null; | ||
}) { | ||
return <CollectionContext.Provider value={collection}>{children}</CollectionContext.Provider>; | ||
} | ||
|
||
export const useCollection = () => { | ||
const context = useContext(CollectionContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('useCollection must be used within a CollectionProvider'); | ||
} | ||
|
||
return context; | ||
}; |
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,29 @@ | ||
import { Facet_ValueFragment, FacetFragment } from '@/lib/vendure/types'; | ||
import FacetsFilterItem from './item'; | ||
|
||
export default function FacetsFilter({ | ||
list, | ||
collectionFacetValues | ||
}: { | ||
list: FacetFragment[]; | ||
collectionFacetValues: Pick<Facet_ValueFragment, 'code' | 'name' | 'facetId' | 'id'>[]; | ||
}) { | ||
return ( | ||
<div className="flex flex-wrap justify-start gap-4 md:items-center"> | ||
{list | ||
.filter( | ||
(facet) => | ||
collectionFacetValues.findIndex((facetValue) => facetValue.facetId == facet.id) > -1 | ||
) | ||
.map((facet) => { | ||
return ( | ||
<FacetsFilterItem | ||
item={facet} | ||
key={facet.id} | ||
collectionFacetValues={collectionFacetValues} | ||
></FacetsFilterItem> | ||
); | ||
})} | ||
</div> | ||
); | ||
} |
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,55 @@ | ||
'use client'; | ||
|
||
import { Facet_ValueFragment, FacetFragment } from '@/lib/vendure/types'; | ||
import { usePathname, useRouter, useSearchParams } from 'next/navigation'; | ||
import { MultiSelect } from '@/ui-components/multi-select'; | ||
import { useMemo } from 'react'; | ||
|
||
export default function FacetsFilterItem({ | ||
item, | ||
collectionFacetValues | ||
}: { | ||
item: FacetFragment; | ||
collectionFacetValues: Pick<Facet_ValueFragment, 'code' | 'name' | 'id'>[]; | ||
}) { | ||
const searchParams = useSearchParams(); | ||
const { replace } = useRouter(); | ||
const pathname = usePathname(); | ||
|
||
function onFilterChange(group: string, value: string[]) { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
if (value.length > 0) { | ||
params.set(group, value.join(',')); | ||
} else { | ||
params.delete(group); | ||
} | ||
|
||
replace(`${pathname}?${params.toString()}`); | ||
} | ||
|
||
const defaultValue = useMemo(() => { | ||
return searchParams.get(item.code)?.split(',') ?? []; | ||
}, [searchParams]); | ||
|
||
return ( | ||
<div className="max-w-[50%] md:max-w-[250px] shrink-0 grow"> | ||
<h3 className="mb-2 block text-xs text-neutral-500 dark:text-neutral-400">{item.name}</h3> | ||
<div> | ||
<MultiSelect | ||
defaultValue={defaultValue} | ||
options={item.values | ||
.filter( | ||
(itemValue) => | ||
collectionFacetValues.findIndex((facetValue) => facetValue.id === itemValue.id) > -1 | ||
) | ||
.map((itemValue) => ({ | ||
label: itemValue.name, | ||
value: itemValue.id | ||
}))} | ||
onValueChange={(value) => onFilterChange(item.code, value)} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
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,37 @@ | ||
import clsx from 'clsx'; | ||
import { Suspense } from 'react'; | ||
import { getCollectionFacetValues, getFacets } from '@/lib/vendure'; | ||
import FacetsFilter from './facets-filter'; | ||
import { FacetFragment } from '@/lib/vendure/types'; | ||
|
||
const skeleton = 'h-10 w-full animate-pulse rounded'; | ||
const items = 'bg-neutral-400 dark:bg-neutral-700'; | ||
|
||
async function FacetsList({ collection, facets }: { collection: string; facets: FacetFragment[] }) { | ||
const collectionFacetValues = collection ? await getCollectionFacetValues({ collection }) : []; | ||
|
||
return <FacetsFilter list={facets} collectionFacetValues={collectionFacetValues}></FacetsFilter>; | ||
} | ||
|
||
export default function Facets({ | ||
collection, | ||
facets | ||
}: { | ||
collection: string; | ||
facets: FacetFragment[]; | ||
}) { | ||
return ( | ||
<Suspense | ||
fallback={ | ||
<div className="gap-4 hidden w-full py-4 lg:flex"> | ||
<div className={clsx(skeleton, items)} /> | ||
<div className={clsx(skeleton, items)} /> | ||
<div className={clsx(skeleton, items)} /> | ||
<div className={clsx(skeleton, items)} /> | ||
</div> | ||
} | ||
> | ||
<FacetsList facets={facets} collection={collection} /> | ||
</Suspense> | ||
); | ||
} |
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.