Skip to content

Commit

Permalink
ZOD til parsing av saksfilter sessionstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ebelegu committed Apr 15, 2024
1 parent d1fc99d commit 8aa247e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Pages/Saksoversikt/useOversiktSessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OppgaveTilstand, SakSortering } from '../../api/graphql-types';
import { Set } from 'immutable';
import { Organisasjon } from '../../altinn/organisasjon';
import amplitude from '../../utils/amplitude';
import { z } from 'zod';

const SESSION_STORAGE_KEY = 'saksoversiktfilter';

Expand Down Expand Up @@ -112,14 +113,30 @@ const defaultSessionState: SessionStateSaksoversikt = {
valgtFilterId: undefined,
};

const FilterFromSessionState = z.object({
route: z.literal('/saksoversikt'),
side: z.number(),
tekstsoek: z.string(),
virksomhetsnumre: z.union([z.array(z.string()), z.literal('ALLEBEDRIFTER')]),
sortering: z.nativeEnum(SakSortering),
sakstyper: z.array(z.string()),
oppgaveTilstand: z.array(z.nativeEnum(OppgaveTilstand)),
valgtFilterId: z.string(),
});

export const useSessionStateOversikt = (alleVirksomheter: Organisasjon[]): UseSessionState => {
const [sessionStorage, setSessionStorage] = useSessionStorage<SessionState>(
SESSION_STORAGE_KEY,
defaultSessionState
);
const [sessionState, setSessionState] = useState<SessionStateSaksoversikt>(() =>
sessionStorage.route === '/saksoversikt' ? sessionStorage : defaultSessionState
);
const [sessionState, setSessionState] = useState<SessionStateSaksoversikt>(() => {
try {
return FilterFromSessionState.parse(sessionStorage);
} catch (e) {
return defaultSessionState;
}
});

useEffect(() => {
setSessionStorage(sessionState);
}, [sessionState]);
Expand Down

0 comments on commit 8aa247e

Please sign in to comment.