-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReimbursementAccountUtils.js
61 lines (55 loc) · 1.5 KB
/
ReimbursementAccountUtils.js
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
import lodashGet from 'lodash/get';
import lodashUnset from 'lodash/unset';
import lodashCloneDeep from 'lodash/cloneDeep';
import * as BankAccounts from './actions/BankAccounts';
/**
* Get the default state for input fields in the VBA flow
*
* @param {Object} props
* @param {String} fieldName
* @param {*} defaultValue
*
* @returns {*}
*/
function getDefaultStateForField(props, fieldName, defaultValue = '') {
return lodashGet(props, ['reimbursementAccountDraft', fieldName])
|| lodashGet(props, ['achData', fieldName], defaultValue);
}
/**
* @param {Object} props
* @returns {Object}
*/
function getErrors(props) {
return lodashGet(props, ['reimbursementAccount', 'errors'], {});
}
/**
* @param {Object} props
* @param {String} path
*/
function clearError(props, path) {
const errors = getErrors(props);
if (!lodashGet(errors, path, false)) {
// No error found for this path
return;
}
// Clear the existing errors
const newErrors = lodashCloneDeep(errors);
lodashUnset(newErrors, path);
BankAccounts.setBankAccountFormValidationErrors(newErrors);
}
/**
* @param {Object} props
* @param {Object} errorTranslationKeys
* @param {String} inputKey
* @returns {String}
*/
function getErrorText(props, errorTranslationKeys, inputKey) {
const errors = getErrors(props);
return errors[inputKey] ? props.translate(errorTranslationKeys[inputKey]) : '';
}
export {
getDefaultStateForField,
getErrors,
clearError,
getErrorText,
};