Skip to content

Commit

Permalink
[KAIZEN-0] Skrive om logikk for feature toggles, fjerne toggle for ar…
Browse files Browse the repository at this point in the history
…beidsregistrering
  • Loading branch information
Jesperpaulsen committed Dec 21, 2023
1 parent 42eecf0 commit 3a4c541
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useOnMount } from './hooks/use-on-mount';
import { useOnChanged } from './hooks/use-on-changed';
import { getContextvalueValue, isContextvalueControlled, RESET_VALUE } from './redux/utils';
import { DecoratorHotkeysProvider } from './components/hurtigtaster/hurtigtaster';
import FeatureToggleProvider from './featureToggle/FeatureToggleProvider';
import FeatureToggleProvider from './featureToggle/FeatureToggleManager';

function Application(props: ApplicationProps) {
const dispatch = useDispatch<Dispatch<SagaActions>>();
Expand Down
30 changes: 9 additions & 21 deletions src/components/lenker.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import React, {useEffect} from 'react';
import React, { useEffect } from 'react';
import {
finnMiljoStreng,
finnNaisInternNavMiljoStreng,
finnNaisMiljoStreng,
hentMiljoFraUrl
} from '../utils/url-utils';
import {WrappedState} from "../hooks/use-wrapped-state";
import {useInitializedState} from "../hooks/use-initialized-state";
import {useEnhetContextvalueState, useFnrContextvalueState} from "../hooks/use-contextvalue-state";
import {Hotkey, ProxyConfig} from "../domain";
import {lagModiacontextholderUrl} from "../redux/api";
import {useDecoratorHotkeys} from "./hurtigtaster/hurtigtaster";
import { WrappedState } from "../hooks/use-wrapped-state";
import { useInitializedState } from "../hooks/use-initialized-state";
import { useEnhetContextvalueState, useFnrContextvalueState } from "../hooks/use-contextvalue-state";
import { Hotkey, ProxyConfig } from "../domain";
import { lagModiacontextholderUrl } from "../redux/api";
import { useDecoratorHotkeys } from "./hurtigtaster/hurtigtaster";
import './lenker.css';
import { FeatureToggles } from '../featureToggle/FeatureToggles';
import { useFeatureToggle } from '../featureToggle/FeatureToggleProvider';

function Lenke(props: { href: string; children: string; target?: string; }) {
/* eslint-disable jsx-a11y/anchor-has-content */
Expand Down Expand Up @@ -106,28 +104,18 @@ interface Props {
proxyConfig: ProxyConfig;
}

const useArbeidssokerRegistreringUrl = (fnr: string, enhet: string) => {
const isOn = useFeatureToggle(FeatureToggles.NY_ARBEIDSSOKER_REGISTRERING_URL)
if (isOn) {
return `https://arbeidssokerregistrering-for-veileder${finnNaisInternNavMiljoStreng()}/`
}
return arbeidssokerregistreringURL(fnr, enhet)
}

function Lenker(props: Props) {
const fnr = useFnrContextvalueState().withDefault('');
const enhet = useEnhetContextvalueState().withDefault('');
const aktorId: string = useInitializedState((state) => state.data.aktorId)
.map((resp) => resp.aktorId)
.withDefault('');

const {register} = useDecoratorHotkeys();
const { register } = useDecoratorHotkeys();
useEffect(() => {
lagHotkeys(fnr, aktorId).forEach(register);
}, [register, fnr, aktorId])

const arbeidssokerregistreringURL = useArbeidssokerRegistreringUrl(fnr, enhet)

if (!props.apen.value) {
return null;
}
Expand Down Expand Up @@ -178,7 +166,7 @@ function Lenker(props: Props) {
<Lenke href={`https://veilarbpersonflate${finnNaisInternNavMiljoStreng()}/${fnr ? fnr : ''}?enhet=${enhet}`}>
Aktivitetsplan
</Lenke>
<Lenke href={arbeidssokerregistreringURL}>
<Lenke href={`https://arbeidssokerregistrering-for-veileder${finnNaisInternNavMiljoStreng()}/`}>
Registrer person
</Lenke>
<Lenke href={`https://tiltaksgjennomforing${finnNaisInternNavMiljoStreng()}/tiltaksgjennomforing`}>
Expand Down
39 changes: 39 additions & 0 deletions src/featureToggle/FeatureToggleManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FeatureTogglesResponse, hentFeatureToggles } from '../redux/api';
import { FeatureToggles } from './FeatureToggles';


const TEN_SECONDS = 10000

class FeatureToggleManager {
private lastFetch?: number
private toggles: FeatureTogglesResponse

constructor() {
this.toggles = this.defaultToggles()
}

private defaultToggles = (): FeatureTogglesResponse => {
return {
'modiacontextholder.ikke-fnr-i-path': false
};
};

private getToggles = async () => {
const res = await hentFeatureToggles()
if (res.data) {
this.toggles = res.data
this.lastFetch = Date.now()
}
}

private shouldFetchToggles = (): boolean => !this.lastFetch || this.lastFetch < Date.now() - TEN_SECONDS

getToggle = async (toggleId: FeatureToggles): Promise<boolean> => {
if (this.shouldFetchToggles()) {
await this.getToggles()
}
return !!this.toggles[toggleId]
}
}

export default new FeatureToggleManager()
41 changes: 0 additions & 41 deletions src/featureToggle/FeatureToggleProvider.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/featureToggle/FeatureToggles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export enum FeatureToggles {
NY_ARBEIDSSOKER_REGISTRERING_URL = 'modiacontextholder.ny-arbeidssoker-registrering-url',
IKKE_FNR_I_PATH = 'modiacontextholder.ikke-fnr-i-path'
}
16 changes: 7 additions & 9 deletions src/redux/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AktivBruker, AktivEnhet, AktorIdResponse, Saksbehandler } from '../inte
import failureConfig from './../mock/mock-error-config';
import { ProxyConfig } from '../domain';
import { FeatureToggles } from '../featureToggle/FeatureToggles';
import { useFeatureToggle } from '../featureToggle/FeatureToggleProvider';
import FeatureToggleManager from '../featureToggle/FeatureToggleManager';

export enum ContextApiType {
NY_AKTIV_ENHET = 'NY_AKTIV_ENHET',
Expand Down Expand Up @@ -52,21 +52,19 @@ export function lagModiacontextholderUrl(proxyConfig: ProxyConfig = false): stri
}
}
function lagUrls(proxyConfig: ProxyConfig) {
const { isOn } = useFeatureToggle(FeatureToggles.IKKE_FNR_I_PATH)
const modiacontextholderUrl = lagModiacontextholderUrl(proxyConfig);
const config = {
aktivEnhetUrl: `${modiacontextholderUrl}/api/context/aktivenhet`,
aktivBrukerUrl: `${modiacontextholderUrl}/api/context/aktivbruker`,
contextUrl: `${modiacontextholderUrl}/api/context`,
saksbehandlerUrl: `${modiacontextholderUrl}/api/decorator`,
featureTogglesUrl: `${modiacontextholderUrl}/api/featuretoggle`,
aktorIdUrl: (fnr: string) => `${modiacontextholderUrl}/api/decorator/aktor/${fnr}`
aktorIdUrl: (fnr: string) => `${modiacontextholderUrl}/api/decorator/aktor/${fnr}`,
modiacontextholderUrl
};
if (isOn) {
config.aktorIdUrl = () => `${modiacontextholderUrl}/api/v2/decorator/aktor/hent-fnr`
}
return config
}

export let urls = lagUrls(false);

export type ResponseError = { data: undefined; error: string };
Expand Down Expand Up @@ -143,17 +141,17 @@ async function deleteJson(url: string, options?: RequestInit): Promise<FetchResp
}
}

export function hentAktorId(fnr: string): Promise<FetchResponse<AktorIdResponse>> {
export async function hentAktorId(fnr: string): Promise<FetchResponse<AktorIdResponse>> {
const feilmelding = lagFnrFeilmelding(fnr);

if (feilmelding.isJust()) {
return Promise.reject('Ugyldig fødselsnummer, kan ikke hente aktørId');
}

const { isOn } = useFeatureToggle(FeatureToggles.IKKE_FNR_I_PATH)
const isOn = await FeatureToggleManager.getToggle(FeatureToggles.IKKE_FNR_I_PATH)

if (isOn) {
return postJson<AktorIdResponse, String>(urls.aktorIdUrl(fnr), fnr)
return postJson<AktorIdResponse, String>(`${urls.modiacontextholderUrl}/api/v2/decorator/aktor/hent-fnr`, fnr)
}

return getJson<AktorIdResponse>(urls.aktorIdUrl(fnr));
Expand Down

0 comments on commit 3a4c541

Please sign in to comment.