-
Notifications
You must be signed in to change notification settings - Fork 3k
/
MoneyRequestMerchantPage.js
133 lines (116 loc) · 4.51 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
import React, {useCallback, useEffect, useRef} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import TextInput from '../../components/TextInput';
import ScreenWrapper from '../../components/ScreenWrapper';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import Form from '../../components/Form';
import ONYXKEYS from '../../ONYXKEYS';
import styles from '../../styles/styles';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as IOU from '../../libs/actions/IOU';
import CONST from '../../CONST';
import useLocalize from '../../hooks/useLocalize';
import {iouPropTypes, iouDefaultProps} 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 {translate} = useLocalize();
const inputRef = useRef(null);
const iouType = lodashGet(route, 'params.iouType', '');
const reportID = lodashGet(route, 'params.reportID', '');
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.getMoneyRequestRoute(iouType, reportID), true);
}
}, [iou.id, iou.participants, iou.amount, iou.receiptPath, iouType, reportID]);
function navigateBack() {
Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(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
onEntryTransitionEnd={() => inputRef.current && inputRef.current.focus()}
>
<HeaderWithBackButton
title={translate('common.merchant')}
onBackButtonPress={() => navigateBack()}
/>
<Form
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}>
<TextInput
inputID="moneyRequestMerchant"
name="moneyRequestMerchant"
defaultValue={iou.merchant}
maxLength={CONST.MERCHANT_NAME_MAX_LENGTH}
label={translate('common.merchant')}
accessibilityLabel={translate('common.merchant')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
ref={(el) => (inputRef.current = el)}
/>
</View>
</Form>
</ScreenWrapper>
);
}
MoneyRequestMerchantPage.propTypes = propTypes;
MoneyRequestMerchantPage.defaultProps = defaultProps;
export default withOnyx({
iou: {
key: ONYXKEYS.IOU,
},
})(MoneyRequestMerchantPage);