Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
f legger til profileringsprovider mot nytt api
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Jan 25, 2024
1 parent 5971ef4 commit 6aba9f3
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/contexts/profilering.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { createContext, ReactNode, useContext, useEffect, useState } from 'react';

import { useFeatureToggleData, FeatureToggles } from './feature-toggles';
import { useArbeidssokerperioder } from './arbeidssokerperioder';

import { fetchToJson } from '../ducks/api-utils';
import { PROFILERING_URL, requestConfig } from '../ducks/api';
import { hentSisteArbeidssokerPeriode } from '../lib/hent-siste-arbeidssokerperiode';
import { ArbeidssokerperioderResponse } from '../models/arbeidssokerperiode';
import { ProfileringResponse } from '../models/profilering';

interface ProfileringProviderType {
profilering: ProfileringResponse;
}

export const ProfileringContext = createContext<ProfileringProviderType>({
profilering: [],
});

function ProfileringProvider(props: { children: ReactNode }) {
const featureToggles = useFeatureToggleData();
const { arbeidssokerperioder } = useArbeidssokerperioder();
const [profilering, settProfilering] = useState<ProfileringResponse>([]);

const erToggletPa = featureToggles[FeatureToggles.BRUK_OPPLYSNINGER_API];

const hentProfilering = async (arbeidssokerperioder: ArbeidssokerperioderResponse) => {
const { perioderId } = hentSisteArbeidssokerPeriode(arbeidssokerperioder);
if (perioderId) {
try {
const urlForProfilering = `${PROFILERING_URL}/${perioderId}`;
const oppdatertProfilering = await fetchToJson(urlForProfilering, requestConfig());
if (oppdatertProfilering) {
settProfilering(oppdatertProfilering as ProfileringResponse);
}
} catch (error) {
console.error(error);
}
}
};

useEffect(() => {
if (erToggletPa && arbeidssokerperioder) {
hentProfilering(arbeidssokerperioder);
}
}, [erToggletPa, arbeidssokerperioder]);

const contextValue = {
profilering,
};

return <ProfileringContext.Provider value={contextValue}>{props.children}</ProfileringContext.Provider>;
}

function useProfilering() {
const context = useContext(ProfileringContext);

if (context === undefined) {
throw new Error('useProfilering må brukes under en ProfileringProvider');
}
return context;
}

export { ProfileringProvider, useProfilering };

0 comments on commit 6aba9f3

Please sign in to comment.