Skip to content

Commit

Permalink
add prefetch to sort, pagination resets and fix params mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchinoe committed Jan 13, 2025
1 parent b18caa7 commit 74f87a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions app/components/SortFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ function getAppliedFilterLink(
const fullKey = FILTER_URL_PREFIX + key;
paramsClone.delete(fullKey, JSON.stringify(value));
});
// Restart pagination.
paramsClone.delete('direction');
paramsClone.delete('cursor');
return `${location.pathname}?${paramsClone.toString()}`;
}

Expand All @@ -194,17 +197,26 @@ function getSortLink(
params: URLSearchParams,
location: Location,
) {
params.set('sort', sort);
return `${location.pathname}?${params.toString()}`;
const paramsClone = new URLSearchParams(params);
paramsClone.set('sort', sort);
// Restart pagination.
paramsClone.delete('direction');
paramsClone.delete('cursor');
return `${location.pathname}?${paramsClone.toString()}`;
}

function getFilterLink(
rawInput: string | ProductFilter,
params: URLSearchParams,
location: ReturnType<typeof useLocation>,
location: Location,
) {
const paramsClone = new URLSearchParams(params);
// Restart pagination.
// Otherwise applying filters after paginating to the end results in an empty page.
paramsClone.delete('direction');
paramsClone.delete('cursor');
const newParams = filterInputToParams(rawInput, paramsClone);

return `${location.pathname}?${newParams.toString()}`;
}

Expand Down Expand Up @@ -350,10 +362,11 @@ export default function SortMenu() {
<Menu.Item key={item.label}>
{() => (
<Link
to={getSortLink(item.key, params, location)}
prefetch="intent"
className={`block text-sm pb-2 px-3 ${
activeItem?.key === item.key ? 'font-bold' : 'font-normal'
}`}
to={getSortLink(item.key, params, location)}
>
{item.label}
</Link>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74f87a9

Please sign in to comment.