Skip to content

Commit

Permalink
chore(filter): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
purusott committed Jul 3, 2024
1 parent 68be8d6 commit 8a45bf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
40 changes: 19 additions & 21 deletions next-tavla/app/(admin)/boards/components/FilterButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ import { Heading4 } from '@entur/typography'
import { CloseIcon, FilterIcon } from '@entur/icons'
import { TTag } from 'types/meta'
import { FilterChip } from '@entur/chip'
import { uniq, xor } from 'lodash'
import { NotificationBadge } from '@entur/layout'
import { TBoardWithOrganizaion } from 'types/settings'
import { useSearchParamReplacer } from '../../hooks/useSearchParamReplacer'
import { useEffect, useState } from 'react'
import { xor } from 'lodash'

function FilterButton({
boardsWithOrg,
}: {
boardsWithOrg: TBoardWithOrganizaion[]
}) {
function FilterButton({ filterOptions }: { filterOptions?: TTag[] }) {
const [value, replace] = useSearchParamReplacer('tags')
const activeTags = value?.split(',') ?? []
const allTags = uniq(
boardsWithOrg.flatMap((boardWithOrg) => {
return boardWithOrg?.board.meta?.tags ?? []
}),
)

const filterTags: TTag[] = activeTags.filter((tag: TTag) =>
allTags.includes(tag),
const [activeTags, setActiveTags] = useState<TTag[]>(
value?.split(',') ?? [],
)

useEffect(() => {
replace(activeTags.join(','))
}, [activeTags, replace])

const handleFilterChipChange = (tag: TTag) => {
const newTags = xor(activeTags, [tag])
setActiveTags(newTags)
}

return (
<Popover>
<PopoverTrigger>
Expand All @@ -44,7 +43,7 @@ function FilterButton({
<FilterIcon aria-hidden="true" />
</SecondaryButton>
<NotificationBadge variant="primary" max={10}>
{filterTags.length}
{activeTags.length}
</NotificationBadge>
<span className="visuallyHidden">merkelapper valgt</span>
</div>
Expand All @@ -62,13 +61,12 @@ function FilterButton({
</PopoverCloseButton>
</div>
<div className="max-w-96 flex flex-wrap gap-1">
{allTags.sort().map((tag: TTag) => (
{filterOptions?.sort().map((tag: TTag) => (
<FilterChip
type="checkbox"
key={tag}
checked={filterTags.includes(tag)}
onChange={() =>
replace(xor(activeTags, [tag]).join(','))
}
checked={activeTags.includes(tag)}
onChange={() => handleFilterChipChange(tag)}
value={tag}
>
{tag}
Expand Down
36 changes: 16 additions & 20 deletions next-tavla/app/(admin)/boards/components/TableRows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,24 @@ function TableRows({
filter.length === 0 ||
filter.every((tag) => (board?.meta?.tags ?? []).includes(tag))

const filteredBoards = boardsWithOrg
.filter(filterByTitleAndOrgName)
.filter((board) => filterByTags(board.board))
.sort(sortFunction)
return (
<>
{boardsWithOrg
.filter((boardWithOrg: TBoardWithOrganizaion) =>
filterByTitleAndOrgName(boardWithOrg),
)
.filter((boardWithOrg: TBoardWithOrganizaion) =>
filterByTags(boardWithOrg.board),
)
.sort(sortFunction)
.map((boardWithOrg: TBoardWithOrganizaion) => (
<TableRow
key={boardWithOrg.board.id}
boardWithOrg={boardWithOrg}
tags={uniq(
boardsWithOrg.flatMap(
(boardWithOrg) =>
boardWithOrg.board?.meta?.tags ?? [],
),
)}
/>
))}
{filteredBoards.map((boardWithOrg: TBoardWithOrganizaion) => (
<TableRow
key={boardWithOrg.board.id}
boardWithOrg={boardWithOrg}
tags={uniq(
boardsWithOrg.flatMap(
(boardWithOrg) =>
boardWithOrg.board?.meta?.tags ?? [],
),
)}
/>
))}
</>
)
}
Expand Down
10 changes: 8 additions & 2 deletions next-tavla/app/(admin)/boards/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ async function OrganizationsBoardsPage() {
if (!user) redirect('/')

const boardsWithOrg = await getAllBoardsForUser()

const tags = [
...new Set(
boardsWithOrg
.map((boardWithOrg) => boardWithOrg.board.meta?.tags ?? [])
.flat(),
),
]
return (
<div className="flex flex-col gap-8">
<Heading1>Tavler</Heading1>
<div className="flex flex-col sm:flex-row md:items-center gap-3">
<Search />
<FilterButton boardsWithOrg={boardsWithOrg} />
<FilterButton filterOptions={tags} />
</div>
<BoardTable boardsWithOrg={boardsWithOrg} />
</div>
Expand Down

0 comments on commit 8a45bf0

Please sign in to comment.