forked from ustaxes/UsTaxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
399 lines (353 loc) · 11 KB
/
actions.ts
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import {
Person,
IncomeW2,
Refund,
Dependent,
FilingStatus,
PrimaryPerson,
ContactInfo,
Supported1099,
F1098e,
Spouse,
Property,
StateResidency,
Information,
EstimatedTaxPayments,
Responses,
HealthSavingsAccount,
Asset
} from 'ustaxes/core/data'
import {
EditDependentAction,
EditPropertyAction,
Edit1099Action,
EditW2Action,
EditEstimatedTaxesAction,
Edit1098eAction,
EditHSAAction
} from './data'
import ajv, * as validators from 'ustaxes/core/data/validate'
import { TaxYear } from 'ustaxes/data'
import { ValidateFunction } from 'ajv'
import { EditAssetAction } from '.'
const indexSchema = {
type: 'number',
minimum: 0
}
const indexValidator: ValidateFunction<number> = ajv.compile(indexSchema)
export enum ActionName {
SAVE_REFUND_INFO = 'SAVE_REFUND_INFO',
SAVE_PRIMARY_PERSON_INFO = 'SAVE_TAXPAYER_INFO',
SAVE_CONTACT_INFO = 'SAVE_CONTACT_INFO',
SAVE_STATE_RESIDENCY = 'SAVE_STATE_RESIDENCY',
SAVE_FILING_STATUS_INFO = 'SAFE_FILING_STATUS_INFO',
ADD_DEPENDENT = 'TAXPAYER/ADD_DEPENDENT',
EDIT_DEPENDENT = 'TAXPAYER/EDIT_DEPENDENT',
REMOVE_DEPENDENT = 'TAXPAYER/REMOVE_DEPENDENT',
ADD_SPOUSE = 'TAXPAYER/ADD_SPOUSE',
REMOVE_SPOUSE = 'TAXPAYER/REMOVE_SPOUSE',
ADD_W2 = 'ADD_W2',
EDIT_W2 = 'EDIT_W2',
REMOVE_W2 = 'REMOVE_W2',
ADD_ESTIMATED_TAX = 'ADD_ESTIMATED_TAX',
EDIT_ESTIMATED_TAX = 'EDIT_ESTIMATED_TAX',
REMOVE_ESTIMATED_TAX = 'REMOVE_ESTIMATED_TAX',
ADD_1099 = 'ADD_1099',
EDIT_1099 = 'EDIT_1099',
REMOVE_1099 = 'REMOVE_1099',
ADD_PROPERTY = 'ADD_PROPERTY',
EDIT_PROPERTY = 'EDIT_PROPERTY',
REMOVE_PROPERTY = 'REMOVE_PROPERTY',
ANSWER_QUESTION = 'ANSWER_QUESTION',
ADD_1098e = 'ADD_1098e',
EDIT_1098e = 'EDIT_1098e',
REMOVE_1098e = 'REMOVE_1098e',
ADD_HSA = 'ADD_HSA',
EDIT_HSA = 'EDIT_HSA',
REMOVE_HSA = 'REMOVE_HSA',
SET_INFO = 'SET_INFO',
SET_ACTIVE_YEAR = 'SET_ACTIVE_YEAR',
PROPAGATE_YEAR_DATA = 'PROPAGATE_YEAR_DATA',
ADD_ASSET = 'ASSETS/ADD',
EDIT_ASSET = 'ASSETS/EDIT',
REMOVE_ASSET = 'ASSETS/REMOVE'
}
interface Save<T, R> {
type: T
year: TaxYear
formData: R
}
type SaveRefundInfo = Save<typeof ActionName.SAVE_REFUND_INFO, Refund>
type SavePrimaryPersonInfo = Save<
typeof ActionName.SAVE_PRIMARY_PERSON_INFO,
PrimaryPerson
>
type SaveFilingStatusInfo = Save<
typeof ActionName.SAVE_FILING_STATUS_INFO,
FilingStatus
>
type SaveContactInfo = Save<typeof ActionName.SAVE_CONTACT_INFO, ContactInfo>
type SaveStateResidencyInfo = Save<
typeof ActionName.SAVE_STATE_RESIDENCY,
StateResidency
>
type AddDependent = Save<typeof ActionName.ADD_DEPENDENT, Dependent>
type EditDependent = Save<typeof ActionName.EDIT_DEPENDENT, EditDependentAction>
type RemoveDependent = Save<typeof ActionName.REMOVE_DEPENDENT, number>
type AddSpouse = Save<typeof ActionName.ADD_SPOUSE, Spouse>
type RemoveSpouse = Save<typeof ActionName.REMOVE_SPOUSE, Record<string, never>>
type AddW2 = Save<typeof ActionName.ADD_W2, IncomeW2>
type EditW2 = Save<typeof ActionName.EDIT_W2, EditW2Action>
type RemoveW2 = Save<typeof ActionName.REMOVE_W2, number>
type AddEstimatedTaxes = Save<
typeof ActionName.ADD_ESTIMATED_TAX,
EstimatedTaxPayments
>
type EditEstimatedTaxes = Save<
typeof ActionName.EDIT_ESTIMATED_TAX,
EditEstimatedTaxesAction
>
type RemoveEstimatedTaxes = Save<typeof ActionName.REMOVE_ESTIMATED_TAX, number>
type AddHSA = Save<typeof ActionName.ADD_HSA, HealthSavingsAccount>
type EditHSA = Save<typeof ActionName.EDIT_HSA, EditHSAAction>
type RemoveHSA = Save<typeof ActionName.REMOVE_HSA, number>
type Add1099 = Save<typeof ActionName.ADD_1099, Supported1099>
type Edit1099 = Save<typeof ActionName.EDIT_1099, Edit1099Action>
type Remove1099 = Save<typeof ActionName.REMOVE_1099, number>
type AddProperty = Save<typeof ActionName.ADD_PROPERTY, Property>
type EditProperty = Save<typeof ActionName.EDIT_PROPERTY, EditPropertyAction>
type RemoveProperty = Save<typeof ActionName.REMOVE_PROPERTY, number>
type AnswerQuestion = Save<typeof ActionName.ANSWER_QUESTION, Responses>
type Add1098e = Save<typeof ActionName.ADD_1098e, F1098e>
type Edit1098e = Save<typeof ActionName.EDIT_1098e, Edit1098eAction>
type Remove1098e = Save<typeof ActionName.REMOVE_1098e, number>
type SetInfo = Save<typeof ActionName.SET_INFO, Information>
type SetActiveYear = Save<typeof ActionName.SET_ACTIVE_YEAR, TaxYear>
type AddAsset = Save<typeof ActionName.ADD_ASSET, Asset<Date>>
type EditAsset = Save<typeof ActionName.EDIT_ASSET, EditAssetAction>
type RemoveAsset = Save<typeof ActionName.REMOVE_ASSET, number>
export type Actions =
| SaveRefundInfo
| SavePrimaryPersonInfo
| SaveFilingStatusInfo
| SaveContactInfo
| SaveStateResidencyInfo
| AddDependent
| EditDependent
| RemoveDependent
| AddSpouse
| RemoveSpouse
| AddW2
| EditW2
| RemoveW2
| AddEstimatedTaxes
| EditEstimatedTaxes
| RemoveEstimatedTaxes
| Add1099
| Edit1099
| Remove1099
| AddProperty
| EditProperty
| RemoveProperty
| AnswerQuestion
| Add1098e
| Edit1098e
| Remove1098e
| AddHSA
| EditHSA
| RemoveHSA
| SetInfo
| SetActiveYear
| AddAsset
| EditAsset
| RemoveAsset
export type SignalAction = (year: TaxYear) => Actions
export type ActionCreator<A> = (formData: A) => SignalAction
function signalAction<T extends ActionName>(
t: T
): (year: TaxYear) => Save<T, Record<string, never>> {
return (year: TaxYear) => ({
type: t,
year,
formData: {}
})
}
/**
* Create an action constructor given an action name and a validator
* for the action's payload. The validator checks the payload against
* the schema at runtime so we can see errors if data of the wrong types
* about to be inserted into the model
*/
const makeActionCreator =
<A, T extends ActionName>(t: T, validate?: ValidateFunction<A>) =>
(formData: A) =>
(year: TaxYear): Save<T, A> => ({
type: t,
year,
formData:
validate !== undefined
? validators.checkType<A>(formData, validate)
: formData
})
/**
* This variant includes a preprocessor function that can be used to
* apply formatting changes to provided data, for example.
*/
const makePreprocessActionCreator =
<A, T extends ActionName>(
t: T,
validate: ValidateFunction<A> | undefined,
clean: (d: A) => Partial<A>
) =>
(formData: A) =>
(year: TaxYear): Save<T, A> => ({
type: t,
year,
formData:
validate !== undefined
? validators.checkType({ ...formData, ...clean(formData) }, validate)
: { ...formData, ...clean(formData) }
})
export const saveRefundInfo: ActionCreator<Refund> = makeActionCreator(
ActionName.SAVE_REFUND_INFO,
validators.refund
)
const cleanPerson = <P extends Person>(p: P): P => ({
...p,
ssid: p?.ssid.replace(/-/g, '')
})
export const savePrimaryPersonInfo: ActionCreator<PrimaryPerson> =
makePreprocessActionCreator(
ActionName.SAVE_PRIMARY_PERSON_INFO,
validators.primaryPerson,
cleanPerson
)
export const saveStateResidencyInfo: ActionCreator<StateResidency> =
makeActionCreator(ActionName.SAVE_STATE_RESIDENCY, validators.stateResidency)
export const saveFilingStatusInfo: ActionCreator<FilingStatus> =
makeActionCreator(ActionName.SAVE_FILING_STATUS_INFO, validators.filingStatus)
export const saveContactInfo: ActionCreator<ContactInfo> =
makePreprocessActionCreator(
ActionName.SAVE_CONTACT_INFO,
validators.contactInfo,
(t) => ({
...t,
contactPhoneNumber: t.contactPhoneNumber?.replace(/-/g, '')
})
)
export const addDependent: ActionCreator<Dependent> =
makePreprocessActionCreator(
ActionName.ADD_DEPENDENT,
validators.dependent,
(t: Dependent) => cleanPerson(t)
)
export const editDependent: ActionCreator<EditDependentAction> =
makePreprocessActionCreator(
ActionName.EDIT_DEPENDENT,
undefined,
({ index, value }: EditDependentAction) => ({
index,
value: cleanPerson(value)
})
)
export const removeDependent: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_DEPENDENT,
indexValidator
)
export const addSpouse: ActionCreator<Spouse> = makePreprocessActionCreator(
ActionName.ADD_SPOUSE,
validators.spouse,
cleanPerson
)
export const removeSpouse: SignalAction = signalAction(ActionName.REMOVE_SPOUSE)
export const addW2: ActionCreator<IncomeW2> = makeActionCreator(
ActionName.ADD_W2,
validators.incomeW2
)
export const editW2: ActionCreator<EditW2Action> = makeActionCreator(
ActionName.EDIT_W2
)
export const removeW2: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_W2,
indexValidator
)
export const addEstimatedPayment: ActionCreator<EstimatedTaxPayments> =
makeActionCreator(
ActionName.ADD_ESTIMATED_TAX,
validators.estimatedTaxPayments
)
export const editEstimatedPayment: ActionCreator<EditEstimatedTaxesAction> =
makeActionCreator(ActionName.EDIT_ESTIMATED_TAX)
export const removeEstimatedPayment: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_ESTIMATED_TAX,
indexValidator
)
export const addHSA: ActionCreator<HealthSavingsAccount> = makeActionCreator(
ActionName.ADD_HSA,
validators.healthSavingsAccounts
)
export const editHSA: ActionCreator<EditHSAAction> = makeActionCreator(
ActionName.EDIT_HSA,
ajv.getSchema(
'#/definitions/EditHSAAction'
) as ValidateFunction<EditHSAAction>
)
export const removeHSA: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_HSA,
indexValidator
)
export const add1099: ActionCreator<Supported1099> = makeActionCreator(
ActionName.ADD_1099,
validators.supported1099
)
export const edit1099: ActionCreator<Edit1099Action> = makeActionCreator(
ActionName.EDIT_1099
)
export const remove1099: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_1099,
indexValidator
)
export const addProperty: ActionCreator<Property> = makeActionCreator(
ActionName.ADD_PROPERTY,
validators.property
)
export const editProperty: ActionCreator<EditPropertyAction> =
makeActionCreator(ActionName.EDIT_PROPERTY)
export const removeProperty: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_PROPERTY,
indexValidator
)
export const answerQuestion: ActionCreator<Responses> = makeActionCreator(
ActionName.ANSWER_QUESTION,
validators.responses
)
export const add1098e: ActionCreator<F1098e> = makeActionCreator(
ActionName.ADD_1098e,
validators.f1098e
)
export const edit1098e: ActionCreator<Edit1098eAction> = makeActionCreator(
ActionName.EDIT_1098e
)
export const remove1098e: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_1098e,
indexValidator
)
// debugging purposes only, leaving unchecked.
export const setInfo: ActionCreator<Information> = makeActionCreator(
ActionName.SET_INFO,
validators.information
)
export const setActiveYear: ActionCreator<TaxYear> = makeActionCreator(
ActionName.SET_ACTIVE_YEAR,
ajv.getSchema('#/definitions/TaxYear') as ValidateFunction<TaxYear>
)
export const addAsset: ActionCreator<Asset<Date>> = makeActionCreator(
ActionName.ADD_ASSET
)
export const editAsset: ActionCreator<EditAssetAction> = makeActionCreator(
ActionName.EDIT_ASSET
)
export const removeAsset: ActionCreator<number> = makeActionCreator(
ActionName.REMOVE_ASSET,
indexValidator
)