Skip to content

Commit

Permalink
fix link in link error
Browse files Browse the repository at this point in the history
  • Loading branch information
lillijo committed Jun 20, 2024
1 parent 424bc2f commit 1e720d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
36 changes: 14 additions & 22 deletions src/components/filterTag.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use client';
import { useCallback } from 'react';
import Link from 'next/link';
import { useSearchParams, useSelectedLayoutSegment } from 'next/navigation';
import { Filter } from '@/types/types';
import cx from 'classnames';
import useQuery from '@/lib/useQuery';

export type FilterTagProps = {
filter: Filter;
Expand All @@ -17,29 +19,19 @@ export default function FilterTag({
isReverse = false,
disabled = false,
}: FilterTagProps) {
const selectedLayoutSegment = useSelectedLayoutSegment(); // todo: why are we using this instead of usePathname() ?
const searchParams = useSearchParams();
const { changeParameter } = useQuery(filter.searchParam);

const handleClickFilter = useCallback(() => {
changeParameter(filter?.id);
}, [filter?.id, changeParameter]);

return (
<Link
href={{
pathname: selectedLayoutSegment,
query: {
// this should be wrapped nicely in some utility function or so?
...(filter.searchParam != 'language' && searchParams.get('language')
? { language: searchParams.get('language') }
: {}),
...(filter.searchParam != 'faculty' && searchParams.get('faculty')
? { faculty: searchParams.get('faculty') }
: {}),
...(filter.searchParam != 'format' && searchParams.get('format')
? { format: searchParams.get('format') }
: {}),
...(isSelected ? {} : { [filter.searchParam]: filter?.id }),
//[filter.searchParam]: filter?.id,
},
}}
className={cx('pl-1 pt-1', disabled && 'pointer-events-none')}
<div
onClick={handleClickFilter}
className={cx(
'cursor-pointer pl-1 pt-1',
disabled && 'pointer-events-none',
)}
>
<div
className={cx(
Expand All @@ -52,6 +44,6 @@ export default function FilterTag({
>
{filter.name}
</div>
</Link>
</div>
);
}
7 changes: 6 additions & 1 deletion src/lib/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const useQuery = (param: string) => {
const createQueryString = useCallback(
(name: string, value: string) => {
const params = new URLSearchParams(searchParams.toString());
params.set(name, value);
const oldValue = params.get(name) ?? '';
if (decodeURIComponent(oldValue) == value) {
params.delete(name);
} else {
params.set(name, value);
}

return params.toString();
},
Expand Down

0 comments on commit 1e720d9

Please sign in to comment.