Skip to content

Commit

Permalink
Fkytter filter for oppgavebenk til egen komponent og flytter filterst…
Browse files Browse the repository at this point in the history
…ate til egen context
  • Loading branch information
matsbyfl committed Dec 16, 2024
1 parent 18096f6 commit e44d7e3
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 69 deletions.
7 changes: 5 additions & 2 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComponentType, lazy, Suspense } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import { BrowserRouter, Route, Routes } from 'react-router-dom'
import { Helmet } from 'react-helmet'
import { useParams } from 'react-router'
import { BrowserRouter, Route, Routes } from 'react-router-dom'
import { RecoilRoot } from 'recoil'
import { SWRConfig } from 'swr'

Expand All @@ -12,6 +12,7 @@ import { Feilside } from './feilsider/Feilside'
import { GlobalFeilside } from './feilsider/GlobalFeilside'
import { Eksperiment } from './felleskomponenter/Eksperiment'
import { Toppmeny } from './header/Toppmeny'
import { FilterProvider } from './oppgavebenk/FilterContext'
import { PersonProvider } from './personoversikt/PersonContext'
import { useAuthentication } from './state/authentication'
import { amplitude_taxonomy, logAmplitudeEvent } from './utils/amplitude'
Expand Down Expand Up @@ -94,7 +95,9 @@ function App() {
<RequireAuth>
<Helmet title="Hotsak - Oppgavebenk" />
<Eksperiment>
<Oppgavebenk />
<FilterProvider>
<Oppgavebenk />
</FilterProvider>
</Eksperiment>
</RequireAuth>
}
Expand Down
79 changes: 79 additions & 0 deletions client/src/oppgavebenk/FilterContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { createContext, ReactNode, useContext } from 'react'
import { useLocalStorageState } from '../oppgaveliste/useLocalStorageState'
import { OppgaveGjelderFilter, TildeltFilter } from '../types/experimentalTypes'
import { SortState } from '@navikt/ds-react'

interface FilterContextType {
tildeltFilter: TildeltFilter
setTildeltFilter: (tildelt: TildeltFilter) => void
gjelderFilter: OppgaveGjelderFilter
setGjelderFilter: (gjelder: OppgaveGjelderFilter) => void
currentPage: number
setCurrentPage: (currentPage: number) => void
sort: SortState
setSort: (sort: SortState) => void
clearFilters: () => void
}

const initialSortState: SortState = {
orderBy: 'OPPRETTET_TIDSPUNKT',
direction: 'descending',
}

const initialState: FilterContextType = {
tildeltFilter: TildeltFilter.INGEN,
setTildeltFilter: () => {},
gjelderFilter: OppgaveGjelderFilter.ALLE,
setGjelderFilter: () => {},
currentPage: 1,
setCurrentPage: () => {},
sort: initialSortState,
setSort: () => {},
clearFilters: () => {},
}

const FilterContext = createContext<FilterContextType>(initialState)

function FilterProvider({ children }: { children: ReactNode }) {
const [tildeltFilter, setTildeltFilter] = useLocalStorageState('oppgaverFilter', initialState.tildeltFilter)
const [gjelderFilter, setGjelderFilter] = useLocalStorageState('oppgavebenkGjelderFilter', initialState.gjelderFilter)
const [currentPage, setCurrentPage] = useLocalStorageState('currentPage', initialState.currentPage)
const [sort, setSort] = useLocalStorageState<SortState>('oppgavebenkSortState', initialSortState)

function clearFilters() {
setGjelderFilter(OppgaveGjelderFilter.ALLE)
setTildeltFilter(TildeltFilter.INGEN)
setSort(initialSortState)
setCurrentPage(1)
}

return (
<FilterContext.Provider
value={{
tildeltFilter,
setTildeltFilter,
gjelderFilter,
setGjelderFilter,
currentPage,
setCurrentPage,
sort,
setSort,
clearFilters,
}}
>
{children}
</FilterContext.Provider>
)
}

function useFilterContext(): FilterContextType {
const context = useContext(FilterContext)

if (!context) {
throw new Error('usePersonContext must be used within a PersonProvider')
}

return context
}

export { FilterContext, FilterProvider, useFilterContext }
53 changes: 53 additions & 0 deletions client/src/oppgavebenk/OppgaveFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Box, Button, HStack, ToggleGroup } from '@navikt/ds-react'
import { FilterDropdown } from '../oppgaveliste/filter/filter'
import { OppgaveGjelderFilter, OppgavetemaLabel, TildeltFilter } from '../types/experimentalTypes'
import { useFilterContext } from './FilterContext'

export function Oppgavefilter() {
const { setCurrentPage, gjelderFilter, setGjelderFilter, tildeltFilter, setTildeltFilter, clearFilters } =
useFilterContext()

const handleFilter = (handler: (...args: any[]) => any, value: OppgaveGjelderFilter | string) => {
handler(value)
setCurrentPage(1)
}

return (
<Box
marginInline="4"
marginBlock="4"
padding="4"
background="surface-subtle"
borderWidth="1"
borderColor="border-subtle"
>
<HStack gap="4" align="end">
<ToggleGroup
label="Oppgaver"
value={tildeltFilter}
size="small"
onChange={(filterValue) => {
handleFilter(setTildeltFilter, filterValue)
}}
style={{ background: 'var(--a-bg-default)' }}
>
<ToggleGroup.Item value={TildeltFilter.INGEN} label="Ufordelte" />
<ToggleGroup.Item value={TildeltFilter.MEG} label="Mine oppgaver" />
<ToggleGroup.Item value={TildeltFilter.ALLE} label="Enhetens oppgaver" />
</ToggleGroup>
<FilterDropdown
handleChange={(filterValue: OppgaveGjelderFilter) => {
handleFilter(setGjelderFilter, filterValue)
}}
label="Gjelder"
value={gjelderFilter}
options={OppgavetemaLabel}
/>
{/*<CheckboxFilter selected={['Digital søknad']} options={[...OppgavetemaLabel.keys()]} />*/}
<Button variant="tertiary-neutral" size="small" onClick={() => clearFilters()}>
Tilbakestill filtre
</Button>
</HStack>
</Box>
)
}
74 changes: 8 additions & 66 deletions client/src/oppgavebenk/Oppgavebenk.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Button, HStack, Link, SortState, Table, ToggleGroup } from '@navikt/ds-react'
import { Box, Link, Table } from '@navikt/ds-react'
import styled from 'styled-components'

import { IngentingFunnet } from '../felleskomponenter/IngenOppgaver'
Expand All @@ -7,49 +7,26 @@ import { EllipsisCell, TekstCell } from '../felleskomponenter/table/Celle'
import { DataCell, KolonneHeader } from '../felleskomponenter/table/KolonneHeader'
import { Toast } from '../felleskomponenter/Toast'
import { Skjermlesertittel } from '../felleskomponenter/typografi'
import { FilterDropdown } from '../oppgaveliste/filter'
import { OppgavelisteTabs } from '../oppgaveliste/OppgavelisteTabs'
import { useLocalStorageState } from '../oppgaveliste/useLocalStorageState'
import { OppgaveApiOppgave, OppgaveGjelderFilter, TildeltFilter, OppgavetemaLabel } from '../types/experimentalTypes'
import { Paging } from '../oppgaveliste/paging/Paging'
import { OppgaveApiOppgave } from '../types/experimentalTypes'
import { Oppgavetype } from '../types/types.internal'
import { formaterDato, formaterTidsstempel } from '../utils/dato'
import { formaterFødselsnummer, formaterNavn, storForbokstavIAlleOrd, storForbokstavIOrd } from '../utils/formater'
import { isError } from '../utils/type'
import { useOppgavelisteV2 } from './useOppgavelisteV2'
import { useFilterContext } from './FilterContext'
import { Oppgavefilter } from './OppgaveFilter'
import { Oppgavetildeling } from './Oppgavetildeling'
import { Paging } from '../oppgaveliste/paging/Paging'
import { useOppgavelisteV2 } from './useOppgavelisteV2'

export function Oppgavebenk() {
const [tildeltFilter, setTildeltFilter] = useLocalStorageState('oppgaverFilter', TildeltFilter.INGEN)
const [gjelderFilter, setGjelderFilter] = useLocalStorageState('oppgavebenkGjelderFilter', OppgaveGjelderFilter.ALLE)
//const [områdeFilter, setOmrådeFilter] = useLocalStorageState('områdeFilter', OmrådeFilter.ALLE)
//const [sakstypeFilter, setSakstypeFilter] = useLocalStorageState('sakstypeFilter', SakstypeFilter.ALLE)
const [currentPage, setCurrentPage] = useLocalStorageState('currentPage', 1)

const initialSortState: SortState = {
orderBy: 'OPPRETTET_TIDSPUNKT',
direction: 'descending',
}

const [sort, setSort] = useLocalStorageState<SortState>('oppgavebenkSortState', initialSortState)
const { tildeltFilter, gjelderFilter, currentPage, setCurrentPage, sort, setSort } = useFilterContext()

const { oppgaver, isLoading, error, totalElements } = useOppgavelisteV2(currentPage, sort, {
tildeltFilter,
gjelderFilter,
})

const handleFilter = (handler: (...args: any[]) => any, value: OppgaveGjelderFilter | string) => {
handler(value)
setCurrentPage(1)
}

const clearFilters = () => {
setGjelderFilter(OppgaveGjelderFilter.ALLE)
setTildeltFilter(TildeltFilter.INGEN)
setSort(initialSortState)
setCurrentPage(1)
}

const kolonner = [
{
key: 'EIER',
Expand Down Expand Up @@ -200,42 +177,7 @@ export function Oppgavebenk() {
<>
<Skjermlesertittel>Oppgaveliste</Skjermlesertittel>
<OppgavelisteTabs />
<Box
marginInline="4"
marginBlock="4"
padding="4"
background="surface-subtle"
borderWidth="1"
borderColor="border-subtle"
>
<HStack gap="4" align="end">
<ToggleGroup
label="Oppgaver"
value={tildeltFilter}
size="small"
onChange={(filterValue) => {
handleFilter(setTildeltFilter, filterValue)
}}
style={{ background: 'var(--a-bg-default)' }}
>
<ToggleGroup.Item value={TildeltFilter.INGEN} label="Ufordelte" />
<ToggleGroup.Item value={TildeltFilter.MEG} label="Mine oppgaver" />
<ToggleGroup.Item value={TildeltFilter.ALLE} label="Enhetens oppgaver" />
</ToggleGroup>
<FilterDropdown
handleChange={(filterValue: OppgaveGjelderFilter) => {
handleFilter(setGjelderFilter, filterValue)
}}
label="Gjelder"
value={gjelderFilter}
options={OppgavetemaLabel}
/>
<Button variant="tertiary-neutral" size="small" onClick={() => clearFilters()}>
Tilbakestill filtre
</Button>
</HStack>
</Box>

<Oppgavefilter />
{isLoading ? (
<Toast>Henter oppgaver</Toast>
) : (
Expand Down
2 changes: 1 addition & 1 deletion client/src/oppgaveliste/Oppgaveliste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { formaterTidsstempel } from '../utils/dato'
import { formaterFødselsnummer, formaterNavn, storForbokstavIAlleOrd } from '../utils/formater'
import { isError } from '../utils/type'
import { FilterDropdown, FilterToggle } from './filter'
import { FilterDropdown, FilterToggle } from './filter/filter.tsx'
import { MenyKnapp } from './kolonner/MenyKnapp'
import { SakstypeEtikett } from './kolonner/SakstypeEtikett'
import { Tildeling } from './kolonner/Tildeling'
Expand Down
68 changes: 68 additions & 0 deletions client/src/oppgaveliste/filter/CheckboxFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Checkbox, CheckboxGroup, VStack } from '@navikt/ds-react'
import { Strek } from '../../felleskomponenter/Strek'
import ShowMore from './ShowMore'

type CheckboxFilterInputProps = {
options: string[]
selected: string[]
}

export const CheckboxFilter = ({ options, selected }: CheckboxFilterInputProps) => {
const notSelectedFilters = options.filter((option) => !selected.includes(option)).sort()

const showMoreLabel = selected.length > 0 ? `Gjelder (${selected.length})` : 'Alle'

return (
<ShowMore title={showMoreLabel} spacing>
<CheckboxGroup legend="Legend" hideLegend size="small" id="filters">
<>
<VStack gap="1" className="checkbox-filter__checkboxes" aria-label="Valgte filtre">
{selected.map((f) => (
<Checkbox
name={f.toString()}
value={f}
key={`${f}`}
onKeyDown={(event) => {
if (event.key === 'Space') {
event.preventDefault()
event.currentTarget?.form?.requestSubmit()
}
}}
onChange={(e) => {
e.currentTarget?.form?.requestSubmit()
}}
>
{f}
</Checkbox>
))}
<Strek />
</VStack>
<VStack
gap="1"
//className="checkbox-filter__checkboxes checkbox-filter__scroll-container"
aria-label="Ikke valgte filtre"
>
{notSelectedFilters.map((f) => (
<Checkbox
name={f}
value={f}
key={`${f}`}
onKeyDown={(event) => {
if (event.key === 'Space') {
event.preventDefault()
// event.currentTarget?.form?.requestSubmit()
}
}}
onChange={(e) => {
// e.currentTarget?.form?.requestSubmit()
}}
>
{f}
</Checkbox>
))}
</VStack>
</>
</CheckboxGroup>
</ShowMore>
)
}
Loading

0 comments on commit e44d7e3

Please sign in to comment.