|
1 |
| -import DokumentForm from '../../../components/forms/dokument/dokumentForm/DokumentForm'; |
| 1 | +import * as RemoteData from '@devexperts/remote-data-ts'; |
| 2 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 3 | +import { Box, Button, Heading, Radio, RadioGroup, TextField } from '@navikt/ds-react'; |
| 4 | +import { Controller, useForm } from 'react-hook-form'; |
| 5 | +import { useNavigate, useOutletContext } from 'react-router-dom'; |
| 6 | + |
| 7 | +import * as SakApi from '~src/api/sakApi'; |
| 8 | +import ApiErrorAlert from '~src/components/apiErrorAlert/ApiErrorAlert'; |
| 9 | +import DokumentDistribusjonForm from '~src/components/forms/dokument/distribusjon/DokumentDistribusjonForm'; |
| 10 | +import { BrevInput } from '~src/components/inputs/brevInput/BrevInput'; |
| 11 | +import LinkAsButton from '~src/components/linkAsButton/LinkAsButton'; |
| 12 | +import { SaksoversiktContext } from '~src/context/SaksoversiktContext'; |
| 13 | +import { useApiCall } from '~src/lib/hooks'; |
| 14 | +import * as Routes from '~src/lib/routes'; |
| 15 | + |
| 16 | +import styles from './BrevPage.module.less'; |
| 17 | +import { DokumentFormData, dokumentSchema } from './BrevPageUtils'; |
2 | 18 |
|
3 | 19 | const BrevPage = () => {
|
4 |
| - return <DokumentForm />; |
| 20 | + const navigate = useNavigate(); |
| 21 | + |
| 22 | + const context = useOutletContext<SaksoversiktContext>(); |
| 23 | + const [sendBrevStatus, sendBrev] = useApiCall(SakApi.lagreOgSendFritekstDokument); |
| 24 | + |
| 25 | + const form = useForm<DokumentFormData>({ |
| 26 | + defaultValues: { |
| 27 | + tittel: '', |
| 28 | + fritekst: '', |
| 29 | + skalSendeTilAnnenAdresse: false, |
| 30 | + adresse: { |
| 31 | + adresser: [{ adresselinje: '' }], |
| 32 | + postnummer: '', |
| 33 | + poststed: '', |
| 34 | + }, |
| 35 | + }, |
| 36 | + resolver: yupResolver(dokumentSchema), |
| 37 | + }); |
| 38 | + |
| 39 | + return ( |
| 40 | + <form |
| 41 | + className={styles.pageContainer} |
| 42 | + onSubmit={form.handleSubmit((values) => |
| 43 | + sendBrev( |
| 44 | + { |
| 45 | + sakId: context.sak.id, |
| 46 | + tittel: values.tittel!, |
| 47 | + fritekst: values.fritekst!, |
| 48 | + adresse: values.skalSendeTilAnnenAdresse |
| 49 | + ? { |
| 50 | + adresselinje1: values.adresse!.adresser[0]!.adresselinje, |
| 51 | + adresselinje2: values.adresse?.adresser[1]?.adresselinje |
| 52 | + ? values.adresse.adresser[1].adresselinje |
| 53 | + : null, |
| 54 | + adresselinje3: values.adresse?.adresser[2]?.adresselinje |
| 55 | + ? values.adresse.adresser[2].adresselinje |
| 56 | + : null, |
| 57 | + postnummer: values.adresse!.postnummer, |
| 58 | + poststed: values.adresse!.poststed, |
| 59 | + } |
| 60 | + : null, |
| 61 | + }, |
| 62 | + () => { |
| 63 | + navigate(Routes.alleDokumenterForSak.createURL({ sakId: context.sak.id })); |
| 64 | + }, |
| 65 | + ), |
| 66 | + )} |
| 67 | + > |
| 68 | + <Heading level="2" size={'large'}> |
| 69 | + Opprett og send nytt fritekst brev |
| 70 | + </Heading> |
| 71 | + <Box className={styles.box} background="bg-default" padding="6"> |
| 72 | + <Controller |
| 73 | + control={form.control} |
| 74 | + name={'skalSendeTilAnnenAdresse'} |
| 75 | + render={({ field }) => ( |
| 76 | + <RadioGroup legend="Skal brevet sendes til en annen adresse?" {...field}> |
| 77 | + <Radio value={true}>Ja</Radio> |
| 78 | + <Radio value={false}>Nei</Radio> |
| 79 | + </RadioGroup> |
| 80 | + )} |
| 81 | + /> |
| 82 | + {form.watch('skalSendeTilAnnenAdresse') && ( |
| 83 | + <DokumentDistribusjonForm prependNames="adresse" control={form.control} /> |
| 84 | + )} |
| 85 | + <hr /> |
| 86 | + |
| 87 | + <Controller |
| 88 | + control={form.control} |
| 89 | + name={'tittel'} |
| 90 | + render={({ field, fieldState }) => ( |
| 91 | + <TextField label={'Tittel'} onChange={field.onChange} error={fieldState.error?.message} /> |
| 92 | + )} |
| 93 | + /> |
| 94 | + <Controller |
| 95 | + control={form.control} |
| 96 | + name={'fritekst'} |
| 97 | + render={({ field, fieldState }) => ( |
| 98 | + <BrevInput |
| 99 | + tekst={field.value} |
| 100 | + onVisBrevClick={() => |
| 101 | + SakApi.opprettFritekstDokument({ |
| 102 | + sakId: context.sak.id, |
| 103 | + tittel: form.watch('tittel'), |
| 104 | + fritekst: form.watch('fritekst'), |
| 105 | + //adresse har ikke noe å si for visning av brevet |
| 106 | + adresse: null, |
| 107 | + }) |
| 108 | + } |
| 109 | + feil={fieldState.error} |
| 110 | + onChange={field.onChange} |
| 111 | + /> |
| 112 | + )} |
| 113 | + /> |
| 114 | + </Box> |
| 115 | + |
| 116 | + {RemoteData.isFailure(sendBrevStatus) && <ApiErrorAlert error={sendBrevStatus.error} />} |
| 117 | + <div className={styles.buttonContainer}> |
| 118 | + <LinkAsButton |
| 119 | + variant="secondary" |
| 120 | + href={Routes.saksoversiktValgtSak.createURL({ sakId: context.sak.id })} |
| 121 | + > |
| 122 | + Tilbake |
| 123 | + </LinkAsButton> |
| 124 | + <Button loading={RemoteData.isPending(sendBrevStatus)}>Lagre og send brev</Button> |
| 125 | + </div> |
| 126 | + </form> |
| 127 | + ); |
5 | 128 | };
|
6 | 129 |
|
7 | 130 | export default BrevPage;
|
0 commit comments