3
3
// SPDX-License-Identifier: LGPL-2.1-or-later
4
4
5
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
6
- import React , { useCallback , useState } from 'react'
6
+ import React , { useCallback , useMemo , useState } from 'react'
7
7
import { useNavigate } from 'react-router'
8
8
import styled from 'styled-components'
9
9
@@ -16,7 +16,7 @@ import {
16
16
Gross ,
17
17
IncomeStatement ,
18
18
IncomeStatementAttachment ,
19
- IncomeStatementAttachmentType ,
19
+ incomeStatementAttachmentTypes ,
20
20
SetIncomeStatementHandledBody
21
21
} from 'lib-common/generated/api-types/incomestatement'
22
22
import {
@@ -27,6 +27,10 @@ import {
27
27
numAttachments ,
28
28
toIncomeStatementAttachments
29
29
} from 'lib-common/income-statements/attachments'
30
+ import {
31
+ computeRequiredAttachments ,
32
+ fromIncomeStatement
33
+ } from 'lib-common/income-statements/form'
30
34
import { UUID } from 'lib-common/types'
31
35
import { useIdRouteParam } from 'lib-common/useRouteParams'
32
36
import { useApiState } from 'lib-common/utils/useRestApi'
@@ -189,6 +193,7 @@ function IncomeInfo({
189
193
/>
190
194
< HorizontalLine />
191
195
< CitizenAttachments
196
+ incomeStatement = { incomeStatement }
192
197
attachments = { incomeStatement . attachments . filter (
193
198
( attachment ) => ! attachment . uploadedByEmployee
194
199
) }
@@ -405,6 +410,7 @@ function ChildIncomeInfo({
405
410
/>
406
411
< HorizontalLine />
407
412
< CitizenAttachments
413
+ incomeStatement = { incomeStatement }
408
414
attachments = { incomeStatement . attachments . filter (
409
415
( attachment ) => ! attachment . uploadedByEmployee
410
416
) }
@@ -414,12 +420,18 @@ function ChildIncomeInfo({
414
420
}
415
421
416
422
const CitizenAttachments = React . memo ( function CitizenAttachments ( {
423
+ incomeStatement,
417
424
attachments
418
425
} : {
426
+ incomeStatement : IncomeStatement
419
427
attachments : IncomeStatementAttachment [ ]
420
428
} ) {
421
429
const { i18n } = useTranslation ( )
422
430
const incomeStatementAttachments = toIncomeStatementAttachments ( attachments )
431
+ const requiredAttachments = useMemo (
432
+ ( ) => computeRequiredAttachments ( fromIncomeStatement ( incomeStatement ) ) ,
433
+ [ incomeStatement ]
434
+ )
423
435
const noAttachments = numAttachments ( incomeStatementAttachments ) === 0
424
436
return (
425
437
< >
@@ -437,21 +449,34 @@ const CitizenAttachments = React.memo(function CitizenAttachments({
437
449
) : (
438
450
< Table >
439
451
< Tbody >
440
- { Object . entries ( incomeStatementAttachments . attachmentsByType ) . map (
441
- ( [ type , attachments ] ) => {
442
- const attachmentType = type as IncomeStatementAttachmentType
452
+ { incomeStatementAttachmentTypes . map ( ( attachmentType ) => {
453
+ const attachments =
454
+ incomeStatementAttachments . attachmentsByType [ attachmentType ]
455
+ const attachmentMissing = ! attachments ?. length
456
+
457
+ if (
458
+ attachmentMissing &&
459
+ ! requiredAttachments . has ( attachmentType )
460
+ ) {
461
+ return null
462
+ } else {
443
463
return (
444
464
< Tr key = { attachmentType } >
445
465
< Td >
446
466
{ i18n . incomeStatement . attachmentNames [ attachmentType ] }
447
467
</ Td >
448
468
< Td >
449
- < UploadedFiles files = { attachments } />
469
+ { attachmentMissing ? (
470
+ i18n . incomeStatement . citizenAttachments
471
+ . attachmentMissing
472
+ ) : (
473
+ < UploadedFiles files = { attachments } />
474
+ ) }
450
475
</ Td >
451
476
</ Tr >
452
477
)
453
478
}
454
- ) }
479
+ } ) }
455
480
</ Tbody >
456
481
</ Table >
457
482
) }
0 commit comments