Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kan sende med adresse for fritekstbrev på sak #3545

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/sakApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export async function lagreOgSendFritekstDokument(arg: OpprettDokumentBody): Pro
body: {
tittel: arg.tittel,
fritekst: arg.fritekst,
adresse: arg.adresse,
},
});
}
Expand All @@ -120,6 +121,7 @@ export async function opprettFritekstDokument(arg: OpprettDokumentBody): Promise
body: {
tittel: arg.tittel,
fritekst: arg.fritekst,
adresse: arg.adresse,
},
bodyTransformer: (res) => res.blob(),
});
Expand Down

This file was deleted.

103 changes: 0 additions & 103 deletions src/components/forms/dokument/dokumentForm/DokumentForm.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
margin-top: @spacing;
}

.box {
display: flex;
flex-direction: column;
gap: @spacing-s;

//tvinger frem hr til å vises i en flex-container
hr {
margin-left: 0;
margin-right: 0;
}
}

.buttonContainer {
display: flex;
gap: @spacing;
Expand Down
127 changes: 125 additions & 2 deletions src/pages/saksbehandling/brev/BrevPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,130 @@
import DokumentForm from '../../../components/forms/dokument/dokumentForm/DokumentForm';
import * as RemoteData from '@devexperts/remote-data-ts';
import { yupResolver } from '@hookform/resolvers/yup';
import { Box, Button, Heading, Radio, RadioGroup, TextField } from '@navikt/ds-react';
import { Controller, useForm } from 'react-hook-form';
import { useNavigate, useOutletContext } from 'react-router-dom';

import * as SakApi from '~src/api/sakApi';
import ApiErrorAlert from '~src/components/apiErrorAlert/ApiErrorAlert';
import DokumentDistribusjonForm from '~src/components/forms/dokument/distribusjon/DokumentDistribusjonForm';
import { BrevInput } from '~src/components/inputs/brevInput/BrevInput';
import LinkAsButton from '~src/components/linkAsButton/LinkAsButton';
import { SaksoversiktContext } from '~src/context/SaksoversiktContext';
import { useApiCall } from '~src/lib/hooks';
import * as Routes from '~src/lib/routes';

import styles from './BrevPage.module.less';
import { DokumentFormData, dokumentSchema } from './BrevPageUtils';

const BrevPage = () => {
return <DokumentForm />;
const navigate = useNavigate();

const context = useOutletContext<SaksoversiktContext>();
const [sendBrevStatus, sendBrev] = useApiCall(SakApi.lagreOgSendFritekstDokument);

const form = useForm<DokumentFormData>({
defaultValues: {
tittel: '',
fritekst: '',
skalSendeTilAnnenAdresse: false,
adresse: {
adresser: [{ adresselinje: '' }],
postnummer: '',
poststed: '',
},
},
resolver: yupResolver(dokumentSchema),
});

return (
<form
className={styles.pageContainer}
onSubmit={form.handleSubmit((values) =>
sendBrev(
{
sakId: context.sak.id,
tittel: values.tittel!,
fritekst: values.fritekst!,
adresse: values.skalSendeTilAnnenAdresse
? {
adresselinje1: values.adresse!.adresser[0]!.adresselinje,
adresselinje2: values.adresse?.adresser[1]?.adresselinje
? values.adresse.adresser[1].adresselinje
: null,
adresselinje3: values.adresse?.adresser[2]?.adresselinje
? values.adresse.adresser[2].adresselinje
: null,
postnummer: values.adresse!.postnummer,
poststed: values.adresse!.poststed,
}
: null,
},
() => {
navigate(Routes.alleDokumenterForSak.createURL({ sakId: context.sak.id }));
},
),
)}
>
<Heading level="2" size={'large'}>
Opprett og send nytt fritekst brev
</Heading>
<Box className={styles.box} background="bg-default" padding="6">
<Controller
control={form.control}
name={'skalSendeTilAnnenAdresse'}
render={({ field }) => (
<RadioGroup legend="Skal brevet sendes til en annen adresse?" {...field}>
<Radio value={true}>Ja</Radio>
<Radio value={false}>Nei</Radio>
</RadioGroup>
)}
/>
{form.watch('skalSendeTilAnnenAdresse') && (
<DokumentDistribusjonForm prependNames="adresse" control={form.control} />
)}
<hr />

<Controller
control={form.control}
name={'tittel'}
render={({ field, fieldState }) => (
<TextField label={'Tittel'} onChange={field.onChange} error={fieldState.error?.message} />
)}
/>
<Controller
control={form.control}
name={'fritekst'}
render={({ field, fieldState }) => (
<BrevInput
tekst={field.value}
onVisBrevClick={() =>
SakApi.opprettFritekstDokument({
sakId: context.sak.id,
tittel: form.watch('tittel'),
fritekst: form.watch('fritekst'),
//adresse har ikke noe å si for visning av brevet
adresse: null,
})
}
feil={fieldState.error}
onChange={field.onChange}
/>
)}
/>
</Box>

{RemoteData.isFailure(sendBrevStatus) && <ApiErrorAlert error={sendBrevStatus.error} />}
<div className={styles.buttonContainer}>
<LinkAsButton
variant="secondary"
href={Routes.saksoversiktValgtSak.createURL({ sakId: context.sak.id })}
>
Tilbake
</LinkAsButton>
<Button loading={RemoteData.isPending(sendBrevStatus)}>Lagre og send brev</Button>
</div>
</form>
);
};

export default BrevPage;
23 changes: 23 additions & 0 deletions src/pages/saksbehandling/brev/BrevPageUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
DokumentDistribusjonFormData,
dokumentDistribusjonFormSchema,
} from '~src/components/forms/dokument/distribusjon/DokumentDistribusjonFormUtils';
import { Nullable } from '~src/lib/types';
import yup from '~src/lib/validering';

export interface DokumentFormData {
tittel: string;
fritekst: string;
skalSendeTilAnnenAdresse: boolean;
adresse: Nullable<DokumentDistribusjonFormData>;
}

export const dokumentSchema = yup.object<DokumentFormData>({
tittel: yup.string().required(),
fritekst: yup.string().required(),
skalSendeTilAnnenAdresse: yup.boolean().required(),
adresse: yup.object<DokumentDistribusjonFormData>().nullable().defined().when('skalSendeTilAnnenAdresse', {
is: true,
then: dokumentDistribusjonFormSchema,
}),
});
11 changes: 11 additions & 0 deletions src/types/dokument/Dokument.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Nullable } from '~src/lib/types';

export interface Dokument {
id: string;
tittel: string;
Expand All @@ -19,4 +21,13 @@ export interface OpprettDokumentBody {
sakId: string;
tittel: string;
fritekst: string;
adresse: Nullable<AdresseRequest>;
}

interface AdresseRequest {
adresselinje1: string;
adresselinje2: Nullable<string>;
adresselinje3: Nullable<string>;
postnummer: string;
poststed: string;
}
Loading