-
Notifications
You must be signed in to change notification settings - Fork 3k
/
IOURequestStepParticipants.tsx
174 lines (156 loc) · 8.22 KB
/
IOURequestStepParticipants.tsx
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
164
165
166
167
168
169
170
171
172
173
174
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import FormHelpMessage from '@components/FormHelpMessage';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import * as IOUUtils from '@libs/IOUUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import MoneyRequestParticipantsSelector from '@pages/iou/request/MoneyRequestParticipantsSelector';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Participant} from '@src/types/onyx/IOU';
import StepScreenWrapper from './StepScreenWrapper';
import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
type IOURequestStepParticipantsOnyxProps = {
/** Whether the confirmation step should be skipped */
skipConfirmation: OnyxEntry<boolean>;
};
type IOURequestStepParticipantsProps = IOURequestStepParticipantsOnyxProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_PARTICIPANTS> &
WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_PARTICIPANTS>;
function IOURequestStepParticipants({
route: {
params: {iouType, reportID, transactionID, action},
},
transaction,
skipConfirmation,
}: IOURequestStepParticipantsProps) {
const participants = transaction?.participants;
const {translate} = useLocalize();
const styles = useThemeStyles();
const selectedReportID = useRef<string>(reportID);
const numberOfParticipants = useRef(participants?.length ?? 0);
const iouRequestType = TransactionUtils.getRequestType(transaction);
const isSplitRequest = iouType === CONST.IOU.TYPE.SPLIT;
const headerTitle = useMemo(() => {
if (action === CONST.IOU.ACTION.CATEGORIZE) {
return translate('iou.categorize');
}
if (action === CONST.IOU.ACTION.SUBMIT) {
return translate('iou.submitExpense');
}
if (action === CONST.IOU.ACTION.SHARE) {
return translate('iou.share');
}
if (isSplitRequest) {
return translate('iou.splitExpense');
}
if (iouType === CONST.IOU.TYPE.PAY) {
return translate('iou.paySomeone', {});
}
if (iouType === CONST.IOU.TYPE.INVOICE) {
return translate('workspace.invoices.sendInvoice');
}
return translate('iou.submitExpense');
}, [iouType, translate, isSplitRequest, action]);
const receiptFilename = transaction?.filename;
const receiptPath = transaction?.receipt?.source;
const receiptType = transaction?.receipt?.type;
// When the component mounts, if there is a receipt, see if the image can be read from the disk. If not, redirect the user to the starting step of the flow.
// This is because until the expense is saved, the receipt file is only stored in the browsers memory as a blob:// and if the browser is refreshed, then
// the image ceases to exist. The best way for the user to recover from this is to start over from the start of the expense process.
// skip this in case user is moving the transaction as the receipt path will be valid in that case
useEffect(() => {
if (IOUUtils.isMovingTransactionFromTrackExpense(action)) {
return;
}
IOU.navigateToStartStepIfScanFileCannotBeRead(receiptFilename ?? '', receiptPath ?? '', () => {}, iouRequestType, iouType, transactionID, reportID, receiptType ?? '');
}, [receiptType, receiptPath, receiptFilename, iouRequestType, iouType, transactionID, reportID, action]);
const addParticipant = useCallback(
(val: Participant[]) => {
IOU.setMoneyRequestParticipants(transactionID, val);
const rateID = DistanceRequestUtils.getCustomUnitRateID(val[0]?.reportID ?? '');
IOU.setCustomUnitRateID(transactionID, rateID);
numberOfParticipants.current = val.length;
// When multiple participants are selected, the reportID is generated at the end of the confirmation step.
// So we are resetting selectedReportID ref to the reportID coming from params.
if (val.length !== 1) {
selectedReportID.current = reportID;
return;
}
// When a participant is selected, the reportID needs to be saved because that's the reportID that will be used in the confirmation step.
selectedReportID.current = val[0]?.reportID ?? reportID;
},
[reportID, transactionID],
);
const goToNextStep = useCallback(() => {
const isCategorizing = action === CONST.IOU.ACTION.CATEGORIZE;
const isShareAction = action === CONST.IOU.ACTION.SHARE;
const isPolicyExpenseChat = participants?.some((participant) => participant.isPolicyExpenseChat);
if (iouType === CONST.IOU.TYPE.SPLIT && !isPolicyExpenseChat && transaction?.amount && transaction?.currency) {
const participantAccountIDs = participants?.map((participant) => participant.accountID) as number[];
IOU.setSplitShares(transaction, transaction.amount, transaction.currency, participantAccountIDs);
}
IOU.setMoneyRequestTag(transactionID, '');
IOU.setMoneyRequestCategory(transactionID, '');
if ((isCategorizing || isShareAction) && numberOfParticipants.current === 0) {
ReportUtils.createDraftWorkspaceAndNavigateToConfirmationScreen(transactionID, action);
return;
}
const iouConfirmationPageRoute = ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(action, iouType, transactionID, selectedReportID.current || reportID);
if (isCategorizing) {
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CATEGORY.getRoute(action, iouType, transactionID, selectedReportID.current || reportID, iouConfirmationPageRoute));
} else {
Navigation.navigate(iouConfirmationPageRoute);
}
}, [iouType, transactionID, transaction, reportID, action, participants]);
const navigateBack = useCallback(() => {
IOUUtils.navigateToStartMoneyRequestStep(iouRequestType, iouType, transactionID, reportID, action);
}, [iouRequestType, iouType, transactionID, reportID, action]);
return (
<StepScreenWrapper
headerTitle={headerTitle}
onBackButtonPress={navigateBack}
shouldShowWrapper
testID={IOURequestStepParticipants.displayName}
includeSafeAreaPaddingBottom={false}
>
{skipConfirmation && (
<FormHelpMessage
style={[styles.ph4, styles.mb4]}
isError={false}
shouldShowRedDotIndicator={false}
message={translate('quickAction.noLongerHaveReportAccess')}
/>
)}
<MoneyRequestParticipantsSelector
participants={isSplitRequest ? participants : []}
onParticipantsAdded={addParticipant}
onFinish={goToNextStep}
iouType={iouType}
iouRequestType={iouRequestType}
action={action}
/>
</StepScreenWrapper>
);
}
IOURequestStepParticipants.displayName = 'IOURequestStepParticipants';
const IOURequestStepParticipantsWithOnyx = withOnyx<IOURequestStepParticipantsProps, IOURequestStepParticipantsOnyxProps>({
skipConfirmation: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? 0;
return `${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`;
},
},
})(IOURequestStepParticipants);
export default withWritableReportOrNotFound(withFullTransactionOrNotFound(IOURequestStepParticipantsWithOnyx));