Skip to content

Commit

Permalink
♻️ Flytter useStepWizard til egen hook
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrognes committed Dec 19, 2023
1 parent 83f008e commit 07d48d4
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion components/StepWizard/Step.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useStepWizard } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';

export type StepType = {
stepIndex?: number;
Expand Down
2 changes: 1 addition & 1 deletion components/StepWizard/StepWizard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Label } from '@navikt/ds-react';
import { useStepWizard } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import * as classes from './StepWizard.module.css';

type StepWizardProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React, { useEffect, useMemo, useState } from 'react';
import { JaEllerNei } from 'types/Generic';
import { Soknad } from 'types/Soknad';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import ColorPanel from 'components/panel/ColorPanel';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
Expand All @@ -33,6 +33,7 @@ import {
updateSøknadData,
} from 'context/soknadcontext/actions';
import { useSoknad } from 'hooks/SoknadHook';
import { useStepWizard } from 'hooks/StepWizardHook';

interface Props {
onBackClick: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Barn, ManuelleBarn, Soknad } from 'types/Soknad';
import * as classes from './Barnetillegg.module.css';
import { Add } from '@navikt/ds-icons';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { AddBarnModal, CreateOrUpdateManuelleBarn, Relasjon } from './AddBarnModal';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/Behandlere/Behandlere.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React, { useEffect, useState } from 'react';
import { Add } from '@navikt/ds-icons';
import { Behandler, Soknad } from 'types/Soknad';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { EndreEllerLeggTilBehandlerModal } from './EndreEllerLeggTilBehandlerModal';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import * as classes from './Behandlere.module.css';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/Medlemskap/Medlemskap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { Add } from '@navikt/ds-icons';
import UtenlandsPeriodeVelger from '..//UtenlandsPeriodeVelger/UtenlandsPeriodeVelger';
import { validate } from 'lib/utils/validationUtils';
import { Soknad, UtenlandsPeriode } from 'types/Soknad';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import ColorPanel from 'components/panel/ColorPanel';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { setFocusOnErrorSummary } from 'components/schema/FormErrorSummary';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import OppsummeringKontaktinfo from './OppsummeringKontaktinfo/OppsummeringKonta
import OppsummeringUtenlandsopphold from './OppsummeringUtenlandsopphold/OppsummeringUtenlandsopphold';
import OppsummeringBehandler from './OppsummeringBehandler/OppsummeringBehandler';
import * as yup from 'yup';
import { goToNamedStep, useStepWizard } from 'context/stepWizardContextV2';
import { goToNamedStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { StepNames } from 'pages';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { OppsummeringVedlegg } from './OppsummeringVedlegg/OppsummeringVedlegg';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/StartDato/StartDato.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Alert, BodyShort, Heading, Label, Radio, RadioGroup, TextField } from '
import { JaEllerNei } from 'types/Generic';
import ColorPanel from 'components/panel/ColorPanel';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
import * as classes from './StartDato.module.css';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/Student/Student.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Alert, BodyShort, Heading, Radio, RadioGroup } from '@navikt/ds-react';
import { JaNeiVetIkke } from 'types/Generic';
import React, { useEffect, useMemo, useState } from 'react';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import ColorPanel from 'components/panel/ColorPanel';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/Vedlegg/Vedlegg.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Soknad } from 'types/Soknad';
import React, { useEffect } from 'react';
import { Alert, BodyLong, BodyShort, Heading, Label, Textarea } from '@navikt/ds-react';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
import { FileInput, LucaGuidePanel } from '@navikt/aap-felles-react';
import { useIntl } from 'react-intl';
Expand Down
3 changes: 2 additions & 1 deletion components/pageComponents/standard/Yrkesskade/Yrkesskade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
import { Soknad } from 'types/Soknad';
import { JaEllerNei } from 'types/Generic';
import * as yup from 'yup';
import { completeAndGoToNextStep, useStepWizard } from 'context/stepWizardContextV2';
import { completeAndGoToNextStep } from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { LucaGuidePanel } from '@navikt/aap-felles-react';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
import { setFocusOnErrorSummary } from 'components/schema/FormErrorSummary';
Expand Down
24 changes: 6 additions & 18 deletions context/stepWizardContextV2.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, Dispatch, ReactNode, useContext, useMemo, useReducer } from 'react';
import React, { createContext, Dispatch, ReactNode, useMemo, useReducer } from 'react';
import { StepType } from 'components/StepWizard/Step';

interface DispatchStepWizardAction {
Expand All @@ -15,6 +15,7 @@ interface DispatchStepWizardAction {
type StepWizardState = {
stepList: Array<StepType>;
};

export type StepWizardContextState = {
stepList: Array<StepType>;
currentStepIndex: number;
Expand Down Expand Up @@ -109,35 +110,22 @@ export const StepWizardProvider = ({ children }: Props) => {
}, [state, dispatch]);
return <StepWizardContext.Provider value={contextValue}>{children}</StepWizardContext.Provider>;
};
export const useStepWizard = () => {
const context = useContext(StepWizardContext);
if (context === undefined) {
throw new Error('useStepWizard must be used within a StepWizardProvider');
}
return context;
};

export const setStepList = async (
stepList: StepType[],
dispatch: Dispatch<DispatchStepWizardAction>
dispatch: Dispatch<DispatchStepWizardAction>,
) => {
dispatch({ type: 'SET_STEP_LIST', payload: stepList });
};
export const setCurrentStepIndex = async (
index: number,
dispatch: Dispatch<DispatchStepWizardAction>
) => {
dispatch({ type: 'SET_ACTIVE_STEP', payload: index });
};

export const completeAndGoToNextStep = async (dispatch: Dispatch<DispatchStepWizardAction>) => {
dispatch({ type: 'COMPLETE_AND_GOTO_NEXT_STEP' });
};

export const goToPreviousStep = async (dispatch: Dispatch<DispatchStepWizardAction>) => {
dispatch({ type: 'GOTO_PREVIOUS_STEP' });
};

export const goToNamedStep = async (dispatch: Dispatch<DispatchStepWizardAction>, name: string) => {
dispatch({ type: 'GOTO_NAMED_STEP', payload: name });
};
export const resetStepWizard = async (dispatch: Dispatch<DispatchStepWizardAction>) => {
dispatch({ type: 'RESET_STEP_WIZARD' });
};
10 changes: 10 additions & 0 deletions hooks/StepWizardHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useContext } from 'react';
import { StepWizardContext } from 'context/stepWizardContextV2';

export const useStepWizard = () => {
const context = useContext(StepWizardContext);
if (context === undefined) {
throw new Error('useStepWizard must be used within a StepWizardProvider');
}
return context;
};
2 changes: 1 addition & 1 deletion pages/[step].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
completeAndGoToNextStep,
goToPreviousStep,
setStepList,
useStepWizard,
} from 'context/stepWizardContextV2';
import { useStepWizard } from 'hooks/StepWizardHook';
import { useDebounceLagreSoknad } from 'hooks/useDebounceLagreSoknad';
import { StepWizard } from 'components/StepWizard';
import {
Expand Down

0 comments on commit 07d48d4

Please sign in to comment.