|
1 |
| -import { Heading } from '@navikt/ds-react'; |
| 1 | +import { Veiledning } from 'components/pageComponents/standard/Veiledning/Veiledning'; |
| 2 | +import { useEffect, useRef, useState } from 'react'; |
| 3 | +import { Navn, SøkerView } from 'context/sokerOppslagContext'; |
| 4 | +import React from 'react'; |
| 5 | +import { useRouter } from 'next/router'; |
| 6 | +import { GetServerSidePropsResult, NextPageContext } from 'next/types'; |
| 7 | +import { beskyttetSide } from 'auth/beskyttetSide'; |
| 8 | +import { getAccessToken } from 'auth/accessToken'; |
| 9 | +import { fetchPOST } from 'api/fetch'; |
| 10 | +import { lesBucket } from 'pages/api/buckets/les'; |
| 11 | +import { StepType } from 'components/StepWizard/Step'; |
| 12 | +import { SØKNAD_CONTEXT_VERSION } from 'context/soknadContextCommon'; |
| 13 | +import { isLabs } from 'utils/environments'; |
| 14 | +import { logSkjemaStartetEvent } from 'utils/amplitude'; |
| 15 | +import metrics from 'utils/metrics'; |
| 16 | +import { scrollRefIntoView } from 'utils/dom'; |
| 17 | +import { getSøkerUtenBarn } from 'pages/api/oppslag/soekerUtenBarn'; |
| 18 | +import { Alert, Link } from '@navikt/ds-react'; |
| 19 | +import { useFeatureToggleIntl } from 'hooks/useFeatureToggleIntl'; |
| 20 | +interface PageProps { |
| 21 | + søker: { |
| 22 | + navn: Navn; |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +export enum StepNames { |
| 27 | + STARTDATO = 'STARTDATO', |
| 28 | + FASTLEGE = 'FASTLEGE', |
| 29 | + MEDLEMSKAP = 'MEDLEMSKAP', |
| 30 | + YRKESSKADE = 'YRKESSKADE', |
| 31 | + TILLEGGSOPPLYSNINGER = 'TILLEGGSOPPLYSNINGER', |
| 32 | + STUDENT = 'STUDENT', |
| 33 | + ANDRE_UTBETALINGER = 'ANDRE_UTBETALINGER', |
| 34 | + BARNETILLEGG = 'BARNETILLEGG', |
| 35 | + VEDLEGG = 'VEDLEGG', |
| 36 | + OPPSUMMERING = 'OPPSUMMERING', |
| 37 | +} |
| 38 | +export const defaultStepList = [ |
| 39 | + { stepIndex: 1, name: StepNames.STARTDATO, active: true }, |
| 40 | + { stepIndex: 2, name: StepNames.MEDLEMSKAP }, |
| 41 | + { stepIndex: 3, name: StepNames.YRKESSKADE }, |
| 42 | + { stepIndex: 4, name: StepNames.FASTLEGE }, |
| 43 | + { stepIndex: 5, name: StepNames.BARNETILLEGG }, |
| 44 | + { stepIndex: 6, name: StepNames.STUDENT }, |
| 45 | + { stepIndex: 7, name: StepNames.ANDRE_UTBETALINGER }, |
| 46 | + { stepIndex: 8, name: StepNames.TILLEGGSOPPLYSNINGER }, |
| 47 | + { stepIndex: 9, name: StepNames.VEDLEGG }, |
| 48 | + { stepIndex: 10, name: StepNames.OPPSUMMERING }, |
| 49 | +]; |
| 50 | + |
| 51 | +const Introduksjon = ({ søker }: PageProps) => { |
| 52 | + const router = useRouter(); |
| 53 | + const { formatMessage } = useFeatureToggleIntl(); |
| 54 | + |
| 55 | + const [isLoading, setIsLoading] = useState(false); |
| 56 | + const [hasError, setHasError] = useState(false); |
| 57 | + |
| 58 | + const errorMessageRef = useRef(null); |
| 59 | + |
| 60 | + const [soker, setSoker] = useState({}); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (søker?.navn) { |
| 64 | + const _søker: SøkerView = { |
| 65 | + fulltNavn: `${søker.navn.fornavn ?? ''} ${søker.navn.mellomnavn ?? ''} ${ |
| 66 | + søker.navn.etternavn ?? '' |
| 67 | + }`, |
| 68 | + }; |
| 69 | + setSoker(_søker); |
| 70 | + } |
| 71 | + }, [søker, setSoker]); |
| 72 | + |
| 73 | + const startSoknad = async () => { |
| 74 | + setIsLoading(true); |
| 75 | + setHasError(false); |
| 76 | + logSkjemaStartetEvent(); |
| 77 | + const result = await fetchPOST('/aap/soknad/api/buckets/lagre/?type=STANDARD', { |
| 78 | + type: 'STANDARD', |
| 79 | + version: SØKNAD_CONTEXT_VERSION, |
| 80 | + søknad: {}, |
| 81 | + lagretStepList: defaultStepList, |
| 82 | + }); |
| 83 | + if (!result.ok) { |
| 84 | + setIsLoading(false); |
| 85 | + setHasError(true); |
| 86 | + } else { |
| 87 | + router.push('/1'); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + if (hasError) { |
| 93 | + if (errorMessageRef?.current != null) scrollRefIntoView(errorMessageRef); |
| 94 | + } |
| 95 | + }, [hasError]); |
2 | 96 |
|
3 |
| -const Home = () => { |
4 | 97 | return (
|
5 |
| - <Heading level="1" size="xlarge"> |
6 |
| - Next app |
7 |
| - </Heading> |
| 98 | + <> |
| 99 | + <Alert variant={'error'}> |
| 100 | + {formatMessage('midlertidigAlertMelding', { |
| 101 | + // @ts-ignore |
| 102 | + a: (chunks: string[]) => ( |
| 103 | + <Link target="_blank" href="https://tjenester.nav.no/soknadaap/app/start"> |
| 104 | + {chunks} |
| 105 | + </Link> |
| 106 | + ), |
| 107 | + })} |
| 108 | + </Alert> |
| 109 | + <Veiledning |
| 110 | + søker={soker} |
| 111 | + isLoading={isLoading} |
| 112 | + hasError={hasError} |
| 113 | + errorMessageRef={errorMessageRef} |
| 114 | + onSubmit={async () => { |
| 115 | + await startSoknad(); |
| 116 | + }} |
| 117 | + /> |
| 118 | + </> |
8 | 119 | );
|
9 | 120 | };
|
10 | 121 |
|
11 |
| -export default Home; |
| 122 | +export const getServerSideProps = beskyttetSide( |
| 123 | + async (ctx: NextPageContext): Promise<GetServerSidePropsResult<{}>> => { |
| 124 | + const stopTimer = metrics.getServersidePropsDurationHistogram.startTimer({ path: '/standard' }); |
| 125 | + const bearerToken = getAccessToken(ctx); |
| 126 | + const søker = await getSøkerUtenBarn(bearerToken); |
| 127 | + const mellomlagretSøknad = await lesBucket('STANDARD', bearerToken); |
| 128 | + const activeStep = mellomlagretSøknad?.lagretStepList?.find((e: StepType) => e.active); |
| 129 | + const activeIndex = activeStep?.stepIndex; |
| 130 | + |
| 131 | + stopTimer(); |
| 132 | + if (activeIndex && !isLabs()) { |
| 133 | + return { |
| 134 | + redirect: { |
| 135 | + destination: `/${activeIndex}`, |
| 136 | + permanent: false, |
| 137 | + }, |
| 138 | + }; |
| 139 | + } |
| 140 | + return { |
| 141 | + props: { søker }, |
| 142 | + }; |
| 143 | + } |
| 144 | +); |
| 145 | + |
| 146 | +export default Introduksjon; |
0 commit comments