|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { FileUpload, Heading, List, VStack } from '@navikt/ds-react'; |
| 4 | + |
| 5 | +import { useApp } from '../../../../context/AppContext'; |
| 6 | +import { IDokumentasjon, IVedlegg } from '../../../../typer/dokumentasjon'; |
| 7 | +import { Dokumentasjonsbehov } from '../../../../typer/kontrakt/dokumentasjon'; |
| 8 | +import { uppercaseFørsteBokstav } from '../../../../utils/visning'; |
| 9 | + |
| 10 | +import { ECustomFileRejectionReasons, useFilopplaster2 } from './useFilopplaster2'; |
| 11 | + |
| 12 | +interface IFilopplasterProps { |
| 13 | + dokumentasjon: IDokumentasjon; |
| 14 | + oppdaterDokumentasjon: ( |
| 15 | + dokumentasjonsBehov: Dokumentasjonsbehov, |
| 16 | + opplastedeVedlegg: IVedlegg[], |
| 17 | + harSendtInn: boolean |
| 18 | + ) => void; |
| 19 | +} |
| 20 | + |
| 21 | +const Filopplaster2: React.FC<IFilopplasterProps> = ({ dokumentasjon, oppdaterDokumentasjon }) => { |
| 22 | + const { tekster, plainTekst } = useApp(); |
| 23 | + |
| 24 | + const dokumentasjonTekster = tekster().DOKUMENTASJON; |
| 25 | + const frittståendeOrdTekster = tekster().FELLES.frittståendeOrd; |
| 26 | + |
| 27 | + const { |
| 28 | + filerUnderOpplastning, |
| 29 | + avvisteFiler, |
| 30 | + MAKS_FILSTØRRELSE_MB, |
| 31 | + MAKS_FILSTØRRELSE_BYTES, |
| 32 | + MAKS_ANTALL_FILER, |
| 33 | + STØTTEDE_FILTYPER, |
| 34 | + feilmeldinger, |
| 35 | + leggTilVedlegg, |
| 36 | + fjernVedlegg, |
| 37 | + fjernAvvistFil, |
| 38 | + prøvOpplastingAvAvvistFilPåNytt, |
| 39 | + } = useFilopplaster2(dokumentasjon, oppdaterDokumentasjon, dokumentasjonTekster, plainTekst); |
| 40 | + |
| 41 | + return ( |
| 42 | + <FileUpload |
| 43 | + translations={{ |
| 44 | + dropzone: { |
| 45 | + button: plainTekst(dokumentasjonTekster.velgFil), |
| 46 | + buttonMultiple: plainTekst(dokumentasjonTekster.velgFiler), |
| 47 | + dragAndDrop: plainTekst(dokumentasjonTekster.draOgSlippFilenHer), |
| 48 | + dragAndDropMultiple: plainTekst(dokumentasjonTekster.draOgSlippFilerHer), |
| 49 | + drop: uppercaseFørsteBokstav(plainTekst(frittståendeOrdTekster.slipp)), |
| 50 | + or: plainTekst(frittståendeOrdTekster.eller), |
| 51 | + disabled: plainTekst( |
| 52 | + filerUnderOpplastning.length > 0 |
| 53 | + ? dokumentasjonTekster.filopplastingDeaktivertFilerErUnderOpplastning |
| 54 | + : dokumentasjonTekster.filopplastingDeaktivert |
| 55 | + ), |
| 56 | + disabledFilelimit: plainTekst( |
| 57 | + dokumentasjonTekster.filopplastingDeaktivertMaksAntallFiler |
| 58 | + ), |
| 59 | + }, |
| 60 | + item: { |
| 61 | + retryButtonTitle: plainTekst(dokumentasjonTekster.lastOppFilenPaNytt), |
| 62 | + deleteButtonTitle: plainTekst(dokumentasjonTekster.slettFilen), |
| 63 | + uploading: plainTekst(dokumentasjonTekster.lasterOpp), |
| 64 | + downloading: plainTekst(dokumentasjonTekster.lasterNed), |
| 65 | + }, |
| 66 | + }} |
| 67 | + > |
| 68 | + <VStack gap="6"> |
| 69 | + <FileUpload.Dropzone |
| 70 | + label={plainTekst(dokumentasjonTekster.lastOppFiler)} |
| 71 | + description={ |
| 72 | + <List as="ul" size="small"> |
| 73 | + <List.Item>{`${plainTekst(dokumentasjonTekster.stottedeFiltyper)} ${STØTTEDE_FILTYPER.join(' ')}`}</List.Item> |
| 74 | + <List.Item>{`${plainTekst(dokumentasjonTekster.maksFilstorrelse)} ${MAKS_FILSTØRRELSE_MB} MB`}</List.Item> |
| 75 | + <List.Item>{`${plainTekst(dokumentasjonTekster.maksAntallFiler)} ${MAKS_ANTALL_FILER}`}</List.Item> |
| 76 | + </List> |
| 77 | + } |
| 78 | + accept={STØTTEDE_FILTYPER.join(',')} |
| 79 | + maxSizeInBytes={MAKS_FILSTØRRELSE_BYTES} |
| 80 | + fileLimit={{ |
| 81 | + max: MAKS_ANTALL_FILER, |
| 82 | + current: dokumentasjon.opplastedeVedlegg.length, |
| 83 | + }} |
| 84 | + // Dersom disabled blir satt til false her vil dette overskrive disabled-staten som blir satt av "fileLimit" prop-en. Derfor settes den til undefined slik at "fileLimit" fungerer som forventet. |
| 85 | + disabled={filerUnderOpplastning.length > 0 ? true : undefined} |
| 86 | + onSelect={nyeFiler => leggTilVedlegg(nyeFiler)} |
| 87 | + /> |
| 88 | + |
| 89 | + {(dokumentasjon.opplastedeVedlegg.length > 0 || |
| 90 | + filerUnderOpplastning.length > 0) && ( |
| 91 | + <VStack gap="2"> |
| 92 | + <Heading level="4" size="xsmall"> |
| 93 | + {`${plainTekst(frittståendeOrdTekster.vedlegg)} (${dokumentasjon.opplastedeVedlegg.length + filerUnderOpplastning.length})`} |
| 94 | + </Heading> |
| 95 | + <VStack as="ul" gap="3"> |
| 96 | + {dokumentasjon.opplastedeVedlegg.map((opplastetVedlegg, index) => ( |
| 97 | + <FileUpload.Item |
| 98 | + as="li" |
| 99 | + key={index} |
| 100 | + file={{ |
| 101 | + name: opplastetVedlegg.navn, |
| 102 | + size: opplastetVedlegg.størrelse, |
| 103 | + }} |
| 104 | + button={{ |
| 105 | + action: 'delete', |
| 106 | + onClick: () => fjernVedlegg(opplastetVedlegg), |
| 107 | + }} |
| 108 | + /> |
| 109 | + ))} |
| 110 | + {filerUnderOpplastning.map((filUnderOpplastning, index) => ( |
| 111 | + <FileUpload.Item |
| 112 | + as="li" |
| 113 | + key={index} |
| 114 | + file={{ |
| 115 | + name: filUnderOpplastning.file.name, |
| 116 | + size: filUnderOpplastning.file.size, |
| 117 | + }} |
| 118 | + status="uploading" |
| 119 | + /> |
| 120 | + ))} |
| 121 | + </VStack> |
| 122 | + </VStack> |
| 123 | + )} |
| 124 | + |
| 125 | + {avvisteFiler.length > 0 && ( |
| 126 | + <VStack gap="2"> |
| 127 | + <Heading level="4" size="xsmall"> |
| 128 | + {`${plainTekst(frittståendeOrdTekster.vedleggMedFeil)} (${avvisteFiler.length})`} |
| 129 | + </Heading> |
| 130 | + <VStack as="ul" gap="3"> |
| 131 | + {avvisteFiler.map((fil, index) => ( |
| 132 | + <FileUpload.Item |
| 133 | + as="li" |
| 134 | + key={index} |
| 135 | + file={{ |
| 136 | + name: fil.file.name, |
| 137 | + size: fil.file.size, |
| 138 | + }} |
| 139 | + error={feilmeldinger[fil.reasons[0]]} |
| 140 | + button={ |
| 141 | + fil.reasons[0] === ECustomFileRejectionReasons.UKJENT_FEIL |
| 142 | + ? { |
| 143 | + action: 'retry', |
| 144 | + onClick: () => |
| 145 | + prøvOpplastingAvAvvistFilPåNytt(fil), |
| 146 | + } |
| 147 | + : { |
| 148 | + action: 'delete', |
| 149 | + onClick: () => fjernAvvistFil(fil), |
| 150 | + } |
| 151 | + } |
| 152 | + /> |
| 153 | + ))} |
| 154 | + </VStack> |
| 155 | + </VStack> |
| 156 | + )} |
| 157 | + </VStack> |
| 158 | + </FileUpload> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export default Filopplaster2; |
0 commit comments