-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sort and filter to products page (#143)
* burst hack * more stuff * fix dev logs * Add to collections query * Update SortFilter.tsx * minor fixup * daniel lint
- Loading branch information
Showing
3 changed files
with
171 additions
and
5 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,119 @@ | ||
import {useState} from 'react'; | ||
import {Heading, Button, Drawer as DrawerComponent} from '~/components'; | ||
import {Link, useLocation} from '@remix-run/react'; | ||
import type { | ||
FilterType, | ||
Filter, | ||
} from '@shopify/hydrogen-react/storefront-api-types'; | ||
|
||
type Props = { | ||
filters: Filter[]; | ||
}; | ||
|
||
export function SortFilter({filters}: {filters: Filter[]}) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center justify-end"> | ||
<Button variant="secondary" onClick={() => setIsOpen(true)}> | ||
Filter and sort | ||
</Button> | ||
</div> | ||
<Drawer | ||
filters={filters} | ||
isOpen={isOpen} | ||
onClose={() => setIsOpen(false)} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export function Drawer({ | ||
isOpen, | ||
onClose, | ||
filters = [], | ||
}: { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
filters: Filter[]; | ||
}) { | ||
const location = useLocation(); | ||
|
||
return ( | ||
<DrawerComponent | ||
open={isOpen} | ||
onClose={onClose} | ||
heading="Filter and sort" | ||
openFrom="right" | ||
> | ||
<> | ||
<nav className="py-8 px-8 md:px-12 "> | ||
{filters.map((filter: Filter) => ( | ||
<> | ||
<Heading as="h4" size="lead" className="pb-2"> | ||
{filter.label} | ||
</Heading> | ||
<ul key={filter.id} className="pb-8"> | ||
{filter.values?.map((option) => { | ||
const params = new URLSearchParams(location.search); | ||
|
||
const newParams = filterInputToParams( | ||
filter.type, | ||
option.input as string, | ||
params, | ||
); | ||
|
||
const to = `${location.pathname}?${newParams.toString()}`; | ||
return ( | ||
<li key={option.id}> | ||
<Link | ||
className="focus:underline hover:underline whitespace-nowrap" | ||
prefetch="intent" | ||
onClick={onClose} | ||
reloadDocument | ||
to={to} | ||
> | ||
{option.label} | ||
</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</> | ||
))} | ||
</nav> | ||
</> | ||
</DrawerComponent> | ||
); | ||
} | ||
|
||
function filterInputToParams( | ||
type: FilterType, | ||
rawInput: string | Record<string, any>, | ||
params: URLSearchParams, | ||
) { | ||
const input = typeof rawInput === 'string' ? JSON.parse(rawInput) : rawInput; | ||
switch (type) { | ||
case 'PRICE_RANGE': | ||
params.set('minPrice', input.min); | ||
params.set('maxPrice', input.max); | ||
break; | ||
case 'LIST': | ||
Object.entries(input).forEach(([key, value]) => { | ||
if (typeof value === 'string') { | ||
params.set(key, value); | ||
} else if (typeof value === 'boolean') { | ||
params.set(key, value.toString()); | ||
} else { | ||
const {name, value: val} = value as {name: string; value: string}; | ||
const newInput = {[name]: val}; | ||
|
||
filterInputToParams(type, newInput, params); | ||
} | ||
}); | ||
break; | ||
} | ||
|
||
return params; | ||
} |
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