-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MoneyRequestParticipantsPage.js
137 lines (120 loc) · 5.17 KB
/
MoneyRequestParticipantsPage.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
import React, {useEffect, useRef} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import CONST from '../../../../CONST';
import ONYXKEYS from '../../../../ONYXKEYS';
import ROUTES from '../../../../ROUTES';
import MoneyRequestParticipantsSplitSelector from './MoneyRequestParticipantsSplitSelector';
import MoneyRequestParticipantsSelector from './MoneyRequestParticipantsSelector';
import styles from '../../../../styles/styles';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import Navigation from '../../../../libs/Navigation/Navigation';
import compose from '../../../../libs/compose';
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities';
import HeaderWithBackButton from '../../../../components/HeaderWithBackButton';
import * as IOU from '../../../../libs/actions/IOU';
import participantPropTypes from '../../../../components/participantPropTypes';
const propTypes = {
/** React Navigation route */
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,
}),
}).isRequired,
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: PropTypes.shape({
/** ID (iouType + reportID) of the request */
id: PropTypes.string,
/** Amount of the request */
amount: PropTypes.number,
/** List of the participants */
participants: PropTypes.arrayOf(participantPropTypes),
}),
...withLocalizePropTypes,
};
const defaultProps = {
iou: {
id: '',
amount: 0,
participants: [],
},
};
function MoneyRequestParticipantsPage(props) {
const prevMoneyRequestId = useRef(props.iou.id);
const iouType = useRef(lodashGet(props.route, 'params.iouType', ''));
const reportID = useRef(lodashGet(props.route, 'params.reportID', ''));
const navigateToNextStep = () => {
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current));
};
const navigateBack = (forceFallback = false) => {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), forceFallback);
};
useEffect(() => {
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (prevMoneyRequestId.current !== props.iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing
if (props.iou.id) {
navigateBack(true);
}
return;
}
// Reset the money request Onyx if the ID in Onyx does not match the ID from params
const moneyRequestId = `${iouType.current}${reportID.current}`;
const shouldReset = props.iou.id !== moneyRequestId;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
if ((props.iou.amount === 0 && !props.iou.receiptPath) || shouldReset) {
navigateBack(true);
}
return () => {
prevMoneyRequestId.current = props.iou.id;
};
}, [props.iou.amount, props.iou.id, props.iou.receiptPath]);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight={DeviceCapabilities.canUseTouchScreen()}
>
{({safeAreaPaddingBottomStyle}) => (
<View style={styles.flex1}>
<HeaderWithBackButton
title={props.translate('iou.cash')}
onBackButtonPress={navigateBack}
/>
{iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.SPLIT ? (
<MoneyRequestParticipantsSplitSelector
onStepComplete={navigateToNextStep}
participants={props.iou.participants}
onAddParticipants={IOU.setMoneyRequestParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
) : (
<MoneyRequestParticipantsSelector
onStepComplete={navigateToNextStep}
onAddParticipants={IOU.setMoneyRequestParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
iouType={iouType.current}
/>
)}
</View>
)}
</ScreenWrapper>
);
}
MoneyRequestParticipantsPage.displayName = 'IOUParticipantsPage';
MoneyRequestParticipantsPage.propTypes = propTypes;
MoneyRequestParticipantsPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
iou: {key: ONYXKEYS.IOU},
}),
)(MoneyRequestParticipantsPage);