-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathMoneyRequestMerchantPage.js
140 lines (123 loc) · 4.98 KB
/
MoneyRequestMerchantPage.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
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import FormProvider from '@components/Form/FormProvider';
import InputWrapperWithRef from '@components/Form/InputWrapper';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/MoneyRequestMerchantForm';
import {iouDefaultProps, iouPropTypes} from './propTypes';
const propTypes = {
/** Onyx Props */
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: iouPropTypes,
/** Route from navigation */
route: PropTypes.shape({
/** Params from the route */
params: PropTypes.shape({
/** The type of IOU report, i.e. bill, request, send */
iouType: PropTypes.string,
/** The report ID of the IOU */
reportID: PropTypes.string,
/** Which field we are editing */
field: PropTypes.string,
/** reportID for the "transaction thread" */
threadReportID: PropTypes.string,
}),
}).isRequired,
};
const defaultProps = {
iou: iouDefaultProps,
};
function MoneyRequestMerchantPage({iou, route}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const iouType = lodashGet(route, 'params.iouType', '');
const reportID = lodashGet(route, 'params.reportID', '');
const isEmptyMerchant = iou.merchant === '' || iou.merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
useEffect(() => {
const moneyRequestId = `${iouType}${reportID}`;
const shouldReset = iou.id !== moneyRequestId;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
if (_.isEmpty(iou.participants) || (iou.amount === 0 && !iou.receiptPath) || shouldReset) {
Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID), true);
}
}, [iou.id, iou.participants, iou.amount, iou.receiptPath, iouType, reportID]);
function navigateBack() {
Navigation.goBack(ROUTES.MONEY_REQUEST_CONFIRMATION.getRoute(iouType, reportID));
}
const validate = useCallback((value) => {
const errors = {};
if (_.isEmpty(value.moneyRequestMerchant)) {
errors.moneyRequestMerchant = 'common.error.fieldRequired';
}
return errors;
}, []);
/**
* Sets the money request comment by saving it to Onyx.
*
* @param {Object} value
* @param {String} value.moneyRequestMerchant
*/
function updateMerchant(value) {
IOU.setMoneyRequestMerchant(value.moneyRequestMerchant);
navigateBack();
}
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={MoneyRequestMerchantPage.displayName}
>
<HeaderWithBackButton
title={translate('common.merchant')}
onBackButtonPress={() => navigateBack()}
/>
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.MONEY_REQUEST_MERCHANT_FORM}
onSubmit={(value) => updateMerchant(value)}
validate={validate}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<InputWrapperWithRef
InputComponent={TextInput}
inputID={INPUT_IDS.MONEY_REQUEST_MERCHANT}
name={INPUT_IDS.MONEY_REQUEST_MERCHANT}
defaultValue={isEmptyMerchant ? '' : iou.merchant}
maxLength={CONST.MERCHANT_NAME_MAX_LENGTH}
label={translate('common.merchant')}
accessibilityLabel={translate('common.merchant')}
role={CONST.ROLE.PRESENTATION}
ref={inputCallbackRef}
/>
</View>
</FormProvider>
</ScreenWrapper>
);
}
MoneyRequestMerchantPage.propTypes = propTypes;
MoneyRequestMerchantPage.defaultProps = defaultProps;
MoneyRequestMerchantPage.displayName = 'MoneyRequestMerchantPage';
export default withOnyx({
iou: {
key: ONYXKEYS.IOU,
},
})(MoneyRequestMerchantPage);