Skip to content

Commit

Permalink
refaktor
Browse files Browse the repository at this point in the history
trekker komponenter ut i egne filer
  • Loading branch information
toresbe committed Dec 3, 2024
1 parent 1b10b1d commit 2cf0416
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
12 changes: 12 additions & 0 deletions src/lib/components/TranslatedError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {FieldError} from "react-hook-form";
import {useTranslation} from "react-i18next";
import {DigisosLanguageKey} from "../i18n/common.ts";
import * as React from "react";

export const TranslatedError = ({error}: {error: Pick<FieldError, "message">}) => {
const {t} = useTranslation("skjema");

if (!error?.message) return null;

return <>{t(error.message as DigisosLanguageKey)}</>;
};
38 changes: 4 additions & 34 deletions src/sider/02-begrunnelse/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as React from "react";
import * as z from "zod";
import {Alert, BodyShort} from "@navikt/ds-react";
import {FieldError, useForm} from "react-hook-form";
import {useForm} from "react-hook-form";
import {useTranslation} from "react-i18next";
import {zodResolver} from "@hookform/resolvers/zod";
import {SkjemaHeadings, SkjemaSteg} from "../../lib/components/SkjemaSteg/SkjemaSteg.tsx";
import {useBegrunnelse} from "../../lib/hooks/data/useBegrunnelse";
import {ApplicationSpinner} from "../../lib/components/animasjoner/ApplicationSpinner";
import {DigisosLanguageKey} from "../../lib/i18n/common.ts";
import useKategorier from "../../lib/hooks/data/useKategorier";
import KategorierChips from "../../lib/components/KategorierChips";
import {SkjemaStegErrorSummary} from "../../lib/components/SkjemaSteg/SkjemaStegErrorSummary.tsx";
Expand All @@ -20,36 +18,8 @@ import {SkjemaStegButtons} from "../../lib/components/SkjemaSteg/SkjemaStegButto
import {useNavigate} from "react-router";
import {logAmplitudeSkjemaStegFullfort} from "../../lib/logAmplitudeSkjemaStegFullfort.ts";
import {useContextFeatureToggles} from "../../lib/providers/useContextFeatureToggles.ts";

const MAX_LEN_HVA = 500;
const MAX_LEN_HVORFOR = 600;

const feilmeldinger: Record<string, DigisosLanguageKey> = {
maksLengde: "validering.maksLengde",
} as const;

export interface FormValues {
hvaSokesOm?: string | null;
hvorforSoke?: string | null;
}

const begrunnelseSchema = z.object({
hvaSokesOm: z.string().max(MAX_LEN_HVA, feilmeldinger.maksLengde).optional(),
hvorforSoke: z.string().max(MAX_LEN_HVORFOR, feilmeldinger.maksLengde),
});

export const TranslatedError = ({error}: {error: Pick<FieldError, "message">}) => {
const {t} = useTranslation("skjema");

if (!error?.message) return null;

return <>{t(error.message as DigisosLanguageKey)}</>;
};

const Feilmelding = () => {
const {t} = useTranslation("skjema");
return <Alert variant={"error"}>{t("skjema.navigering.feil")}</Alert>;
};
import {TranslatedError} from "../../lib/components/TranslatedError.tsx";
import {begrunnelseSchema, FormValues, MAX_LEN_HVA, MAX_LEN_HVORFOR} from "./schema.ts";

export const Begrunnelse = () => {
const {get: defaultValues} = useBegrunnelse();
Expand Down Expand Up @@ -98,7 +68,7 @@ export const Begrunnelse = () => {
<ApplicationSpinner />
) : (
<form className={"space-y-12 lg:space-y-24"} onSubmit={(e) => e.preventDefault()}>
{isError && <Feilmelding />}
{isError && <Alert variant={"error"}>{t("skjema.navigering.feil")}</Alert>}
{isKategorierEnabled ? (
<KategorierChips
errors={errors}
Expand Down
18 changes: 18 additions & 0 deletions src/sider/02-begrunnelse/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as z from "zod";
import {DigisosLanguageKey} from "../../lib/i18n/common.ts";

export const MAX_LEN_HVA = 500;
export const MAX_LEN_HVORFOR = 600;
const feilmeldinger: Record<string, DigisosLanguageKey> = {
maksLengde: "validering.maksLengde",
} as const;

export interface FormValues {
hvaSokesOm?: string | null;
hvorforSoke?: string | null;
}

export const begrunnelseSchema = z.object({
hvaSokesOm: z.string().max(MAX_LEN_HVA, feilmeldinger.maksLengde).optional(),
hvorforSoke: z.string().max(MAX_LEN_HVORFOR, feilmeldinger.maksLengde),
});
2 changes: 1 addition & 1 deletion src/sider/03-arbeidUtdanning/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {ArbeidsforholdResponse, UtdanningFrontend} from "../../generated/model";
import {zodResolver} from "@hookform/resolvers/zod";
import {z} from "zod";
import {ArbeidsforholdListe} from "./ArbeidsforholdListe";
import {TranslatedError} from "../02-begrunnelse";
import {YesNoInput} from "../../lib/components/form/YesNoInput";
import {UnmountClosed} from "react-collapse";
import {faro} from "@grafana/faro-react";
Expand All @@ -20,6 +19,7 @@ import {SkjemaStegStepper} from "../../lib/components/SkjemaSteg/SkjemaStegStepp
import {useNavigate} from "react-router";
import {SkjemaStegButtons} from "../../lib/components/SkjemaSteg/SkjemaStegButtons.tsx";
import {logAmplitudeSkjemaStegFullfort} from "../../lib/logAmplitudeSkjemaStegFullfort.ts";
import {TranslatedError} from "../../lib/components/TranslatedError.tsx";

const MAX_LENGTH = 500;

Expand Down

0 comments on commit 2cf0416

Please sign in to comment.