-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fkytter filter for oppgavebenk til egen komponent og flytter filterst…
…ate til egen context
- Loading branch information
Showing
8 changed files
with
283 additions
and
69 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
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,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 } |
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,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> | ||
) | ||
} |
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
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,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> | ||
) | ||
} |
Oops, something went wrong.