Skip to content

Commit 49fca5e

Browse files
authored
Merge pull request #35 from navikt/ereg_fetch
Ereg fetch
2 parents 8936981 + fd6d487 commit 49fca5e

5 files changed

Lines changed: 95 additions & 4 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import useSWR from "swr";
2+
import { fetchJson } from "../utils/api";
3+
import { HttpError } from "../utils/http";
4+
import { EKSPERTBISTAND_EREG_ADRESSE_PATH } from "../utils/constants";
5+
6+
type AdresseResponse = {
7+
adresse: string;
8+
};
9+
10+
const fetchAdresse = async (url: string): Promise<string | null> => {
11+
try {
12+
const data = await fetchJson<AdresseResponse>(url);
13+
return data?.adresse ?? null;
14+
} catch (error) {
15+
if (error instanceof HttpError && error.status === 404) {
16+
return null;
17+
}
18+
throw error;
19+
}
20+
};
21+
22+
export const useVirksomhetAdresse = (orgnr: string | null | undefined) => {
23+
const shouldFetch = Boolean(orgnr);
24+
const { data, error, isLoading } = useSWR<string | null>(
25+
shouldFetch ? `${EKSPERTBISTAND_EREG_ADRESSE_PATH}/${orgnr}/adresse` : null,
26+
fetchAdresse
27+
);
28+
29+
return {
30+
adresse: data ?? null,
31+
isLoading,
32+
error,
33+
};
34+
};

‎frontend/client/src/mocks/handlers.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { http, HttpResponse } from "msw";
22
import { createEmptyInputs, type SoknadInputs } from "../features/soknad/schema";
33
import { ensureIsoDateString } from "../utils/date";
44
import type { Organisasjon } from "@navikt/virksomhetsvelger";
5-
import { EKSPERTBISTAND_API_PATH } from "../utils/constants";
5+
import { EKSPERTBISTAND_API_PATH, EKSPERTBISTAND_EREG_ADRESSE_PATH } from "../utils/constants";
66

77
const organisasjoner: Organisasjon[] = [
88
{
@@ -36,6 +36,8 @@ const organisasjoner: Organisasjon[] = [
3636

3737
const eregAdresser: Record<string, string> = {
3838
"123456789": "Testveien 1, 0557 Oslo",
39+
"123456780": "Storgata 12, 0155 Oslo",
40+
"123456781": "Bryggen 5, 5003 Bergen",
3941
"987654321": "Eksempelveien 2, 7010 Trondheim",
4042
"111222333": "Demogata 3, 5003 Bergen",
4143
"444555666": "Mockveien 4, 2317 Hamar",
@@ -249,7 +251,7 @@ export const handlers = [
249251
http.get("/ekspertbistand-backend/api/organisasjoner/v1", () =>
250252
HttpResponse.json({ hierarki: organisasjoner })
251253
),
252-
http.get("/api/ereg/:orgnr/adresse", ({ params }) => {
254+
http.get(`${EKSPERTBISTAND_EREG_ADRESSE_PATH}/:orgnr/adresse`, ({ params }) => {
253255
const orgnr = getParamValue(params.orgnr);
254256
if (!orgnr || !/^\d{9}$/.test(orgnr)) {
255257
return HttpResponse.json({ message: "ugyldig orgnr" }, { status: 400 });

‎frontend/client/src/pages/OppsummeringPage.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from "../components/summaryFormatters";
3434
import type { SoknadInputs } from "../features/soknad/schema";
3535
import { FormSummaryAnswer } from "@navikt/ds-react/FormSummary";
36+
import { useVirksomhetAdresse } from "../hooks/useVirksomhetAdresse";
3637

3738
type SoknadSummaryProps = {
3839
data: SoknadInputs;
@@ -43,6 +44,11 @@ type SoknadSummaryProps = {
4344

4445
function SoknadSummary({ data, editable = false, onEditStep1, onEditStep2 }: SoknadSummaryProps) {
4546
const { virksomhet, ansatt, ekspert, behovForBistand, nav } = data;
47+
const {
48+
adresse,
49+
isLoading: adresseLoading,
50+
error: adresseError,
51+
} = useVirksomhetAdresse(virksomhet.virksomhetsnummer);
4652

4753
return (
4854
<>
@@ -59,6 +65,16 @@ function SoknadSummary({ data, editable = false, onEditStep1, onEditStep2 }: Sok
5965
<FormSummary.Label>Organisasjonsnummer</FormSummary.Label>
6066
<FormSummary.Value>{formatValue(virksomhet.virksomhetsnummer)}</FormSummary.Value>
6167
</FormSummary.Answer>
68+
<FormSummary.Answer>
69+
<FormSummary.Label>Beliggenhetsadresse</FormSummary.Label>
70+
<FormSummary.Value>
71+
{adresseError
72+
? "Kunne ikke hente beliggenhetsadresse."
73+
: adresseLoading
74+
? "Laster ..."
75+
: formatValue(adresse)}
76+
</FormSummary.Value>
77+
</FormSummary.Answer>
6278
<FormSummaryAnswer>
6379
<FormSummary.Label>Kontaktperson i virksomheten</FormSummary.Label>
6480
<FormSummary.Value>

‎frontend/client/src/pages/SkjemaSteg1Page.tsx‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { type FormEvent } from "react";
2-
import { Controller, useFormContext } from "react-hook-form";
2+
import { Controller, useFormContext, useWatch } from "react-hook-form";
33
import { ArrowLeftIcon, ArrowRightIcon } from "@navikt/aksel-icons";
4-
import { Button, HGrid, Heading, TextField, VStack, Fieldset } from "@navikt/ds-react";
4+
import {
5+
BodyShort,
6+
Button,
7+
ErrorMessage,
8+
Fieldset,
9+
HGrid,
10+
Heading,
11+
Label,
12+
Loader,
13+
TextField,
14+
VStack,
15+
} from "@navikt/ds-react";
516
import DecoratedPage from "../components/DecoratedPage";
617
import { FORM_COLUMN_STYLE } from "../styles/forms";
718
import type { SoknadInputs } from "../features/soknad/schema";
@@ -17,6 +28,7 @@ import { SkjemaFormProgress } from "../components/SkjemaFormProgress";
1728
import { useSkjemaNavigation } from "../hooks/useSkjemaNavigation";
1829
import { SOKNADER_PATH } from "../utils/constants";
1930
import { SistLagretInfo } from "../components/SistLagretInfo.tsx";
31+
import { useVirksomhetAdresse } from "../hooks/useVirksomhetAdresse";
2032

2133
export default function SkjemaSteg1Page() {
2234
const form = useFormContext<SoknadInputs>();
@@ -26,6 +38,13 @@ export default function SkjemaSteg1Page() {
2638

2739
const { clearDraft, lastPersistedAt } = useSoknadDraft();
2840
const { goToSoknader, goToStep2, goToSummary, createLinkHandler } = useSkjemaNavigation();
41+
const virksomhetsnummer = useWatch({ control, name: "virksomhet.virksomhetsnummer" });
42+
const {
43+
adresse,
44+
isLoading: adresseLoading,
45+
error: adresseError,
46+
} = useVirksomhetAdresse(virksomhetsnummer);
47+
const hasVirksomhet = Boolean(virksomhetsnummer?.trim());
2948

3049
useAttemptedSubmitRedirect(form, { fields: STEP1_FIELDS, onValidationFailed: bumpFocusKey });
3150

@@ -86,6 +105,25 @@ export default function SkjemaSteg1Page() {
86105
/>
87106
)}
88107
/>
108+
{hasVirksomhet ? (
109+
<VStack gap="1" style={{ marginTop: "1rem" }}>
110+
<Label>Beliggenhetsadresse (hentet fra brreg.no)</Label>
111+
{adresseLoading ? (
112+
<BodyShort size="small">
113+
<Loader size="small" title="Laster beliggenhetsadresse" aria-live="polite" />{" "}
114+
Laster beliggenhetsadresse ...
115+
</BodyShort>
116+
) : null}
117+
{!adresseLoading && !adresseError ? (
118+
<BodyShort size="small">
119+
{adresse ?? "Fant ikke beliggenhetsadresse."}
120+
</BodyShort>
121+
) : null}
122+
{adresseError ? (
123+
<ErrorMessage>Kunne ikke hente beliggenhetsadresse.</ErrorMessage>
124+
) : null}
125+
</VStack>
126+
) : null}
89127
</div>
90128
<input type="hidden" {...register("virksomhet.navn")} />
91129

‎frontend/client/src/utils/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const EKSPERTBISTAND_API_PATH = withBasePath("/ekspertbistand-backend/api
2828
export const EKSPERTBISTAND_ORGANISASJONER_PATH = withBasePath(
2929
"/ekspertbistand-backend/api/organisasjoner/v1"
3030
);
31+
export const EKSPERTBISTAND_EREG_ADRESSE_PATH = withBasePath("/ekspertbistand-backend/api/ereg");
3132

3233
export const SOKNADER_PATH = "/soknader";
3334

0 commit comments

Comments
 (0)