-
Notifications
You must be signed in to change notification settings - Fork 3k
/
IOURequestStepMerchant.js
185 lines (167 loc) · 7.18 KB
/
IOURequestStepMerchant.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
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
import lodashGet from 'lodash/get';
import lodashIsEmpty from 'lodash/isEmpty';
import PropTypes from 'prop-types';
import React, {useCallback} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import categoryPropTypes from '@components/categoryPropTypes';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import tagPropTypes from '@components/tagPropTypes';
import TextInput from '@components/TextInput';
import transactionPropTypes from '@components/transactionPropTypes';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import {policyPropTypes} from '@pages/workspace/withPolicy';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import INPUT_IDS from '@src/types/form/MoneyRequestMerchantForm';
import IOURequestStepRoutePropTypes from './IOURequestStepRoutePropTypes';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
const propTypes = {
/** Navigation route context info provided by react navigation */
route: IOURequestStepRoutePropTypes.isRequired,
/** Onyx Props */
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
transaction: transactionPropTypes,
/** The draft transaction that holds data to be persisted on the current transaction */
splitDraftTransaction: transactionPropTypes,
/** The policy of the report */
policy: policyPropTypes.policy,
/** Collection of categories attached to a policy */
policyCategories: PropTypes.objectOf(categoryPropTypes),
/** Collection of tags attached to a policy */
policyTags: tagPropTypes,
};
const defaultProps = {
transaction: {},
splitDraftTransaction: {},
policy: null,
policyTags: null,
policyCategories: null,
};
function IOURequestStepMerchant({
route: {
params: {transactionID, reportID, backTo, action, iouType},
},
transaction,
splitDraftTransaction,
policy,
policyTags,
policyCategories,
}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const isEditing = action === CONST.IOU.ACTION.EDIT;
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
const isEditingSplitBill = iouType === CONST.IOU.TYPE.SPLIT && isEditing;
const {merchant} = ReportUtils.getTransactionDetails(isEditingSplitBill && !lodashIsEmpty(splitDraftTransaction) ? splitDraftTransaction : transaction);
const isEmptyMerchant = merchant === '' || merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
const isMerchantRequired = _.some(transaction.participants, (participant) => Boolean(participant.isPolicyExpenseChat));
const navigateBack = () => {
Navigation.goBack(backTo);
};
/**
* @param {Object} value
* @param {String} value.moneyRequestMerchant
*/
const validate = useCallback(
(value) => {
const errors = {};
if (isMerchantRequired && _.isEmpty(value.moneyRequestMerchant)) {
errors.moneyRequestMerchant = 'common.error.fieldRequired';
}
return errors;
},
[isMerchantRequired],
);
/**
* @param {Object} value
* @param {String} value.moneyRequestMerchant
*/
const updateMerchant = (value) => {
const newMerchant = value.moneyRequestMerchant.trim();
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
if (isEditingSplitBill) {
IOU.setDraftSplitTransaction(transactionID, {merchant: newMerchant});
navigateBack();
return;
}
// In case the merchant hasn't been changed, do not make the API request.
// In case the merchant has been set to empty string while current merchant is partial, do nothing too.
if (newMerchant === merchant || (newMerchant === '' && merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT)) {
navigateBack();
return;
}
IOU.setMoneyRequestMerchant(transactionID, newMerchant, !isEditing);
if (isEditing) {
// When creating new money requests newMerchant can be blank so we fall back on PARTIAL_TRANSACTION_MERCHANT
IOU.updateMoneyRequestMerchant(transactionID, reportID, newMerchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, policy, policyTags, policyCategories);
}
navigateBack();
};
return (
<StepScreenWrapper
headerTitle={translate('common.merchant')}
onBackButtonPress={navigateBack}
shouldShowWrapper
testID={IOURequestStepMerchant.displayName}
>
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.MONEY_REQUEST_MERCHANT_FORM}
onSubmit={updateMerchant}
validate={validate}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.MONEY_REQUEST_MERCHANT}
name={INPUT_IDS.MONEY_REQUEST_MERCHANT}
defaultValue={isEmptyMerchant ? '' : merchant}
maxLength={CONST.MERCHANT_NAME_MAX_LENGTH}
label={translate('common.merchant')}
accessibilityLabel={translate('common.merchant')}
role={CONST.ROLE.PRESENTATION}
ref={inputCallbackRef}
/>
</View>
</FormProvider>
</StepScreenWrapper>
);
}
IOURequestStepMerchant.propTypes = propTypes;
IOURequestStepMerchant.defaultProps = defaultProps;
IOURequestStepMerchant.displayName = 'IOURequestStepMerchant';
export default compose(
withWritableReportOrNotFound,
withFullTransactionOrNotFound,
withOnyx({
splitDraftTransaction: {
key: ({route}) => {
const transactionID = lodashGet(route, 'params.transactionID', 0);
return `${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`;
},
},
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
policyCategories: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report ? report.policyID : '0'}`,
},
policyTags: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`,
},
}),
)(IOURequestStepMerchant);