-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathMoneyRequestDescriptionPage.js
131 lines (118 loc) · 4.69 KB
/
MoneyRequestDescriptionPage.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
import React, {Component} 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 withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
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 compose from '../../libs/compose';
import * as IOU from '../../libs/actions/IOU';
import optionPropTypes from '../../components/optionPropTypes';
const propTypes = {
...withLocalizePropTypes,
/** Onyx Props */
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: PropTypes.shape({
id: PropTypes.string,
amount: PropTypes.number,
comment: PropTypes.string,
participants: PropTypes.arrayOf(optionPropTypes),
}),
};
const defaultProps = {
iou: {
id: '',
amount: 0,
comment: '',
participants: [],
},
};
class MoneyRequestDescriptionPage extends Component {
constructor(props) {
super(props);
this.updateComment = this.updateComment.bind(this);
this.navigateBack = this.navigateBack.bind(this);
this.iouType = lodashGet(props.route, 'params.iouType', '');
this.reportID = lodashGet(props.route, 'params.reportID', '');
}
componentDidMount() {
const moneyRequestId = `${this.iouType}${this.reportID}`;
const shouldReset = this.props.iou.id !== moneyRequestId;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
if (_.isEmpty(this.props.iou.participants) || this.props.iou.amount === 0 || shouldReset) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(this.iouType, this.reportID), true);
}
}
// eslint-disable-next-line rulesdir/prefer-early-return
componentDidUpdate(prevProps) {
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (_.isEmpty(this.props.iou.participants) || this.props.iou.amount === 0 || prevProps.iou.id !== this.props.iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing.
if (this.props.iou.id) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(this.iouType, this.reportID), true);
}
}
}
navigateBack() {
Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(this.iouType, this.reportID));
}
/**
* Sets the money request comment by saving it to Onyx.
*
* @param {Object} value
* @param {String} value.moneyRequestComment
*/
updateComment(value) {
IOU.setMoneyRequestDescription(value.moneyRequestComment);
this.navigateBack();
}
render() {
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
onEntryTransitionEnd={() => this.descriptionInputRef && this.descriptionInputRef.focus()}
>
<HeaderWithBackButton
title={this.props.translate('common.description')}
onBackButtonPress={this.navigateBack}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM}
onSubmit={this.updateComment}
submitButtonText={this.props.translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<TextInput
inputID="moneyRequestComment"
name="moneyRequestComment"
defaultValue={this.props.iou.comment}
label={this.props.translate('moneyRequestConfirmationList.whatsItFor')}
ref={(el) => (this.descriptionInputRef = el)}
/>
</View>
</Form>
</ScreenWrapper>
);
}
}
MoneyRequestDescriptionPage.propTypes = propTypes;
MoneyRequestDescriptionPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
iou: {key: ONYXKEYS.IOU},
}),
)(MoneyRequestDescriptionPage);