-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathIOURequestStepSendFrom.tsx
97 lines (84 loc) · 4.25 KB
/
IOURequestStepSendFrom.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
import React, {useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import * as Expensicons from '@components/Icon/Expensicons';
import SelectionList from '@components/SelectionList';
import type {ListItem} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import useLocalize from '@hooks/useLocalize';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import {sortWorkspacesBySelected} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
type WorkspaceListItem = ListItem & {
value: string;
};
type IOURequestStepSendFromProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_SEND_FROM> &
WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_SEND_FROM>;
function IOURequestStepSendFrom({route, transaction}: IOURequestStepSendFromProps) {
const {translate} = useLocalize();
const {transactionID, backTo} = route.params;
const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email});
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const selectedWorkspace = useMemo(() => transaction?.participants?.find((participant) => participant.isSender), [transaction]);
const workspaceOptions: WorkspaceListItem[] = useMemo(() => {
const availableWorkspaces = PolicyUtils.getActiveAdminWorkspaces(allPolicies, currentUserLogin).filter((policy) => PolicyUtils.canSendInvoiceFromWorkspace(policy.id));
return availableWorkspaces
.sort((policy1, policy2) => sortWorkspacesBySelected({policyID: policy1.id, name: policy1.name}, {policyID: policy2.id, name: policy2.name}, selectedWorkspace?.policyID))
.map((policy) => ({
text: policy.name,
value: policy.id,
keyForList: policy.id,
icons: [
{
id: policy.id,
source: policy?.avatarURL ? policy.avatarURL : ReportUtils.getDefaultWorkspaceAvatar(policy.name),
fallbackIcon: Expensicons.FallbackWorkspaceAvatar,
name: policy.name,
type: CONST.ICON_TYPE_WORKSPACE,
},
],
isSelected: selectedWorkspace?.policyID === policy.id,
}));
}, [allPolicies, currentUserLogin, selectedWorkspace]);
const navigateBack = () => {
Navigation.goBack(backTo);
};
const selectWorkspace = (item: WorkspaceListItem) => {
const newParticipants = (transaction?.participants ?? []).filter((participant) => participant.accountID);
newParticipants.push({
policyID: item.value,
isSender: true,
selected: false,
});
IOU.setMoneyRequestParticipants(transactionID, newParticipants);
navigateBack();
};
return (
<StepScreenWrapper
headerTitle={translate('workspace.invoices.sendFrom')}
onBackButtonPress={navigateBack}
shouldShowWrapper
testID={IOURequestStepSendFrom.displayName}
includeSafeAreaPaddingBottom
>
<SelectionList
sections={[{data: workspaceOptions, title: translate('common.workspaces')}]}
onSelectRow={selectWorkspace}
shouldSingleExecuteRowSelect
ListItem={UserListItem}
initiallyFocusedOptionKey={selectedWorkspace?.policyID}
/>
</StepScreenWrapper>
);
}
IOURequestStepSendFrom.displayName = 'IOURequestStepSendFrom';
export default withWritableReportOrNotFound(withFullTransactionOrNotFound(IOURequestStepSendFrom));