-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathMoneyRequestSelectorPage.js
163 lines (148 loc) · 7.12 KB
/
MoneyRequestSelectorPage.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
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import DragAndDropProvider from '@components/DragAndDrop/Provider';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import TabSelector from '@components/TabSelector/TabSelector';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import compose from '@libs/compose';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as IOUUtils from '@libs/IOUUtils';
import Navigation from '@libs/Navigation/Navigation';
import OnyxTabNavigator, {TopTab} from '@libs/Navigation/OnyxTabNavigator';
import * as ReportUtils from '@libs/ReportUtils';
import withReportOrNotFound from '@pages/home/report/withReportOrNotFound';
import reportPropTypes from '@pages/reportPropTypes';
import useThemeStyles from '@styles/useThemeStyles';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import NewDistanceRequestPage from './NewDistanceRequestPage';
import ReceiptSelector from './ReceiptSelector';
import NewRequestAmountPage from './steps/NewRequestAmountPage';
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,
/** Report on which the money request is being created */
report: reportPropTypes,
/** Which tab has been selected */
selectedTab: PropTypes.string,
};
const defaultProps = {
selectedTab: CONST.TAB.SCAN,
report: {},
};
function MoneyRequestSelectorPage(props) {
const styles = useThemeStyles();
const [isDraggingOver, setIsDraggingOver] = useState(false);
const iouType = lodashGet(props.route, 'params.iouType', '');
const reportID = lodashGet(props.route, 'params.reportID', '');
const {translate} = useLocalize();
const title = {
[CONST.IOU.TYPE.REQUEST]: translate('iou.requestMoney'),
[CONST.IOU.TYPE.SEND]: translate('iou.sendMoney'),
[CONST.IOU.TYPE.SPLIT]: translate('iou.splitBill'),
};
const isFromGlobalCreate = !reportID;
const isExpenseChat = ReportUtils.isPolicyExpenseChat(props.report);
const isExpenseReport = ReportUtils.isExpenseReport(props.report);
const shouldDisplayDistanceRequest = isExpenseChat || isExpenseReport || isFromGlobalCreate;
const resetMoneyRequestInfo = () => {
const moneyRequestID = `${iouType}${reportID}`;
IOU.resetMoneyRequestInfo(moneyRequestID);
};
// Allow the user to create the request if we are creating the request in global menu or the report can create the request
const isAllowedToCreateRequest = _.isEmpty(props.report.reportID) || ReportUtils.canCreateRequest(props.report, iouType);
const prevSelectedTab = usePrevious(props.selectedTab);
useEffect(() => {
if (prevSelectedTab === props.selectedTab) {
return;
}
resetMoneyRequestInfo();
// resetMoneyRequestInfo function is not added as dependencies since they don't change between renders
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.selectedTab, prevSelectedTab]);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableKeyboardAvoidingView={false}
shouldEnableMinHeight={DeviceCapabilities.canUseTouchScreen()}
headerGapStyles={isDraggingOver ? [styles.receiptDropHeaderGap] : []}
testID={MoneyRequestSelectorPage.displayName}
>
{({safeAreaPaddingBottomStyle}) => (
<FullPageNotFoundView shouldShow={!IOUUtils.isValidMoneyRequestType(iouType) || !isAllowedToCreateRequest}>
<DragAndDropProvider
isDisabled={props.selectedTab !== CONST.TAB.SCAN}
setIsDraggingOver={setIsDraggingOver}
>
<View style={[styles.flex1, safeAreaPaddingBottomStyle]}>
<HeaderWithBackButton
title={title[iouType]}
onBackButtonPress={Navigation.dismissModal}
/>
{iouType === CONST.IOU.TYPE.REQUEST || iouType === CONST.IOU.TYPE.SPLIT ? (
<OnyxTabNavigator
id={CONST.TAB.RECEIPT_TAB_ID}
selectedTab={props.selectedTab}
tabBar={({state, navigation, position}) => (
<TabSelector
state={state}
navigation={navigation}
position={position}
/>
)}
>
<TopTab.Screen
name={CONST.TAB.MANUAL}
component={NewRequestAmountPage}
initialParams={{reportID, iouType}}
/>
<TopTab.Screen
name={CONST.TAB.SCAN}
component={ReceiptSelector}
initialParams={{reportID, iouType, pageIndex: 1}}
/>
{shouldDisplayDistanceRequest && (
<TopTab.Screen
name={CONST.TAB.DISTANCE}
component={NewDistanceRequestPage}
initialParams={{reportID, iouType}}
/>
)}
</OnyxTabNavigator>
) : (
<NewRequestAmountPage route={props.route} />
)}
</View>
</DragAndDropProvider>
</FullPageNotFoundView>
)}
</ScreenWrapper>
);
}
MoneyRequestSelectorPage.propTypes = propTypes;
MoneyRequestSelectorPage.defaultProps = defaultProps;
MoneyRequestSelectorPage.displayName = 'MoneyRequestSelectorPage';
export default compose(
withReportOrNotFound(false),
withOnyx({
selectedTab: {
key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`,
},
}),
)(MoneyRequestSelectorPage);