-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChildIncomeStatementForm.tsx
341 lines (314 loc) · 9.93 KB
/
ChildIncomeStatementForm.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// SPDX-FileCopyrightText: 2017-2022 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later
import React, { useCallback, useImperativeHandle, useMemo, useRef } from 'react'
import styled from 'styled-components'
import { Result } from 'lib-common/api'
import { IncomeStatementStatus } from 'lib-common/generated/api-types/incomestatement'
import { IncomeStatementId } from 'lib-common/generated/api-types/shared'
import { numAttachments } from 'lib-common/income-statements/attachments'
import * as Form from 'lib-common/income-statements/form'
import LocalDate from 'lib-common/local-date'
import { scrollToRef } from 'lib-common/utils/scrolling'
import { AsyncButton } from 'lib-components/atoms/buttons/AsyncButton'
import { Button } from 'lib-components/atoms/buttons/Button'
import Checkbox from 'lib-components/atoms/form/Checkbox'
import TextArea from 'lib-components/atoms/form/TextArea'
import Container, { ContentArea } from 'lib-components/layout/Container'
import {
FixedSpaceColumn,
FixedSpaceRow
} from 'lib-components/layout/flex-helpers'
import { AlertBox } from 'lib-components/molecules/MessageBoxes'
import DatePicker from 'lib-components/molecules/date-picker/DatePicker'
import { H1, H2, Label } from 'lib-components/typography'
import { Gap } from 'lib-components/white-space'
import { errorToInputInfo } from '../input-info-helper'
import { useLang, useTranslation } from '../localization'
import ChildIncomeStatementAttachments from './ChildIncomeStatementAttachments'
import {
AttachmentSection,
useAttachmentHandler
} from './IncomeStatementAttachments'
import {
ActionContainer,
AssureCheckbox,
IncomeStatementFormAPI,
LabelError,
SetStateCallback,
useFieldDispatch,
useFieldSetState
} from './IncomeStatementComponents'
const OtherInfoContainer = styled.div`
max-width: 716px;
`
const ChildIncome = React.memo(function ChildIncome({
incomeStatementId,
formData,
showFormErrors,
onChange
}: {
incomeStatementId: IncomeStatementId | undefined
showFormErrors: boolean
formData: Form.IncomeStatementForm
onChange: SetStateCallback<Form.IncomeStatementForm>
}) {
const t = useTranslation()
const onAttachmentChange = useFieldSetState(onChange, 'attachments')
const attachmentHandler = useAttachmentHandler(
incomeStatementId,
formData.attachments,
onAttachmentChange
)
const onOtherInfoChanged = useFieldDispatch(onChange, 'otherInfo')
return (
<>
{attachmentHandler ? (
<AttachmentSection
attachmentType="CHILD_INCOME"
showFormErrors={showFormErrors}
attachmentHandler={attachmentHandler}
/>
) : (
<>
<Label>{t.income.childIncome.childAttachments}</Label>
<Gap size="s" />
{formData.childIncome &&
numAttachments(formData.attachments) === 0 && (
<>
<LabelError text={t.components.fileUpload.input.title} />
<Gap size="L" />
</>
)}
<ChildIncomeStatementAttachments
incomeStatementId={incomeStatementId}
attachments={formData.attachments}
onChange={onAttachmentChange}
/>
</>
)}
<Gap size="L" />
<Label>{t.income.childIncome.additionalInfo}</Label>
<OtherInfoContainer>
<TextArea
placeholder={t.income.childIncome.write}
value={formData.otherInfo}
onChange={onOtherInfoChanged}
data-qa="other-info"
/>
</OtherInfoContainer>
</>
)
})
interface ChildIncomeTypeSelectionData {
startDate: LocalDate | null
endDate: LocalDate | null
highestFeeSelected: boolean
grossSelected: boolean
entrepreneurSelected: boolean
}
const ChildIncomeTimeRangeSelection = React.memo(
React.forwardRef(function ChildIncomeTypeSelection(
{
formData,
isValidStartDate,
showFormErrors,
onChange
}: {
formData: ChildIncomeTypeSelectionData
isValidStartDate: (date: LocalDate) => boolean
showFormErrors: boolean
onChange: SetStateCallback<Form.IncomeStatementForm>
},
ref: React.ForwardedRef<HTMLDivElement>
) {
const t = useTranslation()
const [lang] = useLang()
const onStartDateChanged = useFieldDispatch(onChange, 'startDate')
const startDateInputInfo = useMemo(
() =>
errorToInputInfo(
formData.startDate ? undefined : 'validDate',
t.validationErrors
),
[formData.startDate, t]
)
const onEndDateChanged = useFieldDispatch(onChange, 'endDate')
return (
<FixedSpaceColumn spacing="zero" ref={ref}>
<H2 noMargin>{t.income.incomeInfo}</H2>
<Gap size="s" />
<Label>{t.income.childIncomeInfo}</Label>
<Gap size="s" />
{showFormErrors && (
<>
<AlertBox noMargin message={t.income.errors.invalidForm} />
<Gap size="s" />
</>
)}
<FixedSpaceRow spacing="XL">
<div>
<Label htmlFor="start-date">
{t.income.incomeType.startDate} *
</Label>
<Gap size="xs" />
<DatePicker
id="start-date"
date={formData.startDate}
onChange={onStartDateChanged}
info={startDateInputInfo}
hideErrorsBeforeTouched={!showFormErrors}
locale={lang}
isInvalidDate={(d) =>
isValidStartDate(d) ? null : t.validationErrors.unselectableDate
}
data-qa="start-date"
required={true}
/>
</div>
<div>
<Label htmlFor="end-date">{t.income.incomeType.endDate}</Label>
<Gap size="xs" />
<DatePicker
id="end-date"
date={formData.endDate}
onChange={onEndDateChanged}
minDate={formData.startDate ?? undefined}
hideErrorsBeforeTouched
locale={lang}
data-qa="end-date"
/>
</div>
</FixedSpaceRow>
</FixedSpaceColumn>
)
})
)
const ResponsiveFixedSpaceRow = styled(FixedSpaceRow)`
@media (max-width: 900px) {
display: block;
}
`
const Confidential = styled.div`
flex: 0 0 auto;
`
interface Props {
incomeStatementId: IncomeStatementId | undefined
status: IncomeStatementStatus
formData: Form.IncomeStatementForm
showFormErrors: boolean
otherStartDates: LocalDate[]
draftSaveEnabled: boolean
onChange: SetStateCallback<Form.IncomeStatementForm>
onSave: (draft: boolean) => Promise<Result<unknown>> | undefined
onSuccess: () => void
onCancel: () => void
}
export default React.memo(
React.forwardRef(function ChildIncomeStatementForm(
{
incomeStatementId,
status,
formData,
showFormErrors,
otherStartDates,
draftSaveEnabled,
onChange,
onSave,
onSuccess,
onCancel
}: Props,
ref: React.ForwardedRef<IncomeStatementFormAPI>
) {
const t = useTranslation()
const scrollTarget = useRef<HTMLDivElement>(null)
const isValidStartDate = useCallback(
(date: LocalDate) => otherStartDates.every((d) => !d.isEqual(date)),
[otherStartDates]
)
const incomeTypeSelectionFormData = useMemo(
() => ({
startDate: formData.startDate,
endDate: formData.endDate,
highestFeeSelected: formData.highestFee,
grossSelected: formData.gross.selected,
entrepreneurSelected: formData.entrepreneur.selected
}),
[
formData.endDate,
formData.entrepreneur.selected,
formData.gross.selected,
formData.highestFee,
formData.startDate
]
)
useImperativeHandle(ref, () => ({
scrollToErrors() {
scrollToRef(scrollTarget)
}
}))
const sendButtonEnabled = formData.assure
return (
<>
<Container>
<Gap size="s" />
<ContentArea opaque paddingVertical="L">
<ResponsiveFixedSpaceRow>
<FixedSpaceColumn spacing="zero">
<H1 noMargin>{t.income.childFormTitle}</H1>
{t.income.childFormDescription}
</FixedSpaceColumn>
<Confidential>{t.income.confidential}</Confidential>
</ResponsiveFixedSpaceRow>
</ContentArea>
<Gap size="s" />
<ContentArea opaque>
<ChildIncomeTimeRangeSelection
formData={incomeTypeSelectionFormData}
isValidStartDate={isValidStartDate}
showFormErrors={showFormErrors}
onChange={onChange}
ref={scrollTarget}
/>
<Gap size="L" />
<ChildIncome
incomeStatementId={incomeStatementId}
showFormErrors={showFormErrors}
formData={formData}
onChange={onChange}
/>
</ContentArea>
<ActionContainer>
<AssureCheckbox>
<Checkbox
label={t.income.assure}
checked={formData.assure}
data-qa="assure-checkbox"
onChange={useFieldDispatch(onChange, 'assure')}
/>
</AssureCheckbox>
<FixedSpaceRow>
<Button text={t.common.cancel} onClick={onCancel} />
{status === 'DRAFT' && draftSaveEnabled && (
<AsyncButton
text={t.income.saveAsDraft}
onClick={() => onSave(true)}
onSuccess={onSuccess}
data-qa="save-draft-btn"
/>
)}
<AsyncButton
text={status === 'DRAFT' ? t.income.send : t.income.updateSent}
primary
onClick={() => onSave(false)}
disabled={!sendButtonEnabled}
onSuccess={onSuccess}
data-qa="save-btn"
/>
</FixedSpaceRow>
</ActionContainer>
</Container>
</>
)
})
)