-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChildIncomeStatementAttachments.tsx
65 lines (57 loc) · 1.77 KB
/
ChildIncomeStatementAttachments.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-FileCopyrightText: 2017-2022 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later
import React, { useCallback } from 'react'
import { Attachment } from 'lib-common/generated/api-types/attachment'
import {
AttachmentId,
IncomeStatementId
} from 'lib-common/generated/api-types/shared'
import { IncomeStatementAttachments } from 'lib-common/income-statements/attachments'
import { FixedSpaceColumn } from 'lib-components/layout/flex-helpers'
import FileUpload from 'lib-components/molecules/FileUpload'
import { getAttachmentUrl, incomeStatementAttachment } from '../attachments'
import { SetStateCallback } from './IncomeStatementComponents'
export default React.memo(function Attachments({
incomeStatementId,
attachments,
onChange
}: {
incomeStatementId: IncomeStatementId | undefined
attachments: IncomeStatementAttachments
onChange: SetStateCallback<IncomeStatementAttachments>
}) {
const onUploaded = useCallback(
(attachment: Attachment) =>
onChange((prev) =>
prev.typed
? prev
: { ...prev, attachments: [...prev.untypedAttachments, attachment] }
),
[onChange]
)
const onDeleted = useCallback(
(id: AttachmentId) =>
onChange((prev) =>
prev.typed
? prev
: {
...prev,
attachments: prev.untypedAttachments.filter((a) => a.id !== id)
}
),
[onChange]
)
if (attachments.typed) return null
return (
<FixedSpaceColumn spacing="zero">
<FileUpload
files={attachments.untypedAttachments}
uploadHandler={incomeStatementAttachment(incomeStatementId, null)}
onUploaded={onUploaded}
onDeleted={onDeleted}
getDownloadUrl={getAttachmentUrl}
/>
</FixedSpaceColumn>
)
})