-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathNewDistanceRequestPage.js
85 lines (76 loc) · 2.9 KB
/
NewDistanceRequestPage.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
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect} from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import DistanceRequest from '@components/DistanceRequest';
import Navigation from '@libs/Navigation/Navigation';
import reportPropTypes from '@pages/reportPropTypes';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {iouPropTypes} from './propTypes';
const propTypes = {
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: iouPropTypes,
/** The report on which the request is initiated on */
report: reportPropTypes,
/** Passed from the navigator */
route: PropTypes.shape({
/** Parameters the route gets */
params: PropTypes.shape({
/** Type of IOU */
iouType: PropTypes.oneOf(_.values(CONST.IOU.TYPE)),
/** Id of the report on which the distance request is being created */
reportID: PropTypes.string,
}),
}),
};
const defaultProps = {
iou: {},
report: {},
route: {
params: {
iouType: '',
reportID: '',
},
},
};
// This component is responsible for getting the transactionID from the IOU key, or creating the transaction if it doesn't exist yet, and then passing the transactionID.
// You can't use Onyx props in the withOnyx mapping, so we need to set up and access the transactionID here, and then pass it down so that DistanceRequest can subscribe to the transaction.
function NewDistanceRequestPage({iou, report, route}) {
const iouType = lodashGet(route, 'params.iouType', 'request');
const isEditingNewRequest = Navigation.getActiveRoute().includes('address');
useEffect(() => {
if (iou.transactionID) {
return;
}
IOU.setUpDistanceTransaction();
}, [iou.transactionID]);
const onSubmit = useCallback(() => {
if (isEditingNewRequest) {
Navigation.goBack(ROUTES.MONEY_REQUEST_CONFIRMATION.getRoute(iouType, report.reportID));
return;
}
IOU.navigateToNextPage(iou, iouType, report);
}, [iou, iouType, isEditingNewRequest, report]);
return (
<DistanceRequest
report={report}
route={route}
isEditingNewRequest={isEditingNewRequest}
transactionID={iou.transactionID}
onSubmit={onSubmit}
/>
);
}
NewDistanceRequestPage.displayName = 'NewDistanceRequestPage';
NewDistanceRequestPage.propTypes = propTypes;
NewDistanceRequestPage.defaultProps = defaultProps;
export default withOnyx({
iou: {key: ONYXKEYS.IOU},
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID')}`,
},
})(NewDistanceRequestPage);