-
Notifications
You must be signed in to change notification settings - Fork 3k
/
WorkspaceMemberDetailsPage.tsx
307 lines (284 loc) · 16.2 KB
/
WorkspaceMemberDetailsPage.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import ExpensifyCardImage from '@assets/images/expensify-card.svg';
import Avatar from '@components/Avatar';
import Button from '@components/Button';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePrevious from '@hooks/usePrevious';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
import variables from '@styles/variables';
import * as Card from '@userActions/Card';
import * as Member from '@userActions/Policy/Member';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PersonalDetails, PersonalDetailsList} from '@src/types/onyx';
import type {ListItemType} from './WorkspaceMemberDetailsRoleSelectionModal';
import WorkspaceMemberDetailsRoleSelectionModal from './WorkspaceMemberDetailsRoleSelectionModal';
type WorkspacePolicyOnyxProps = {
/** Personal details of all users */
personalDetails: OnyxEntry<PersonalDetailsList>;
};
type WorkspaceMemberDetailsPageProps = Omit<WithPolicyAndFullscreenLoadingProps, 'route'> &
WorkspacePolicyOnyxProps &
StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.MEMBER_DETAILS>;
function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceMemberDetailsPageProps) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();
const {translate} = useLocalize();
const StyleUtils = useStyleUtils();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${policy?.workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`);
const [isRemoveMemberConfirmModalVisible, setIsRemoveMemberConfirmModalVisible] = useState(false);
const [isRoleSelectionModalVisible, setIsRoleSelectionModalVisible] = useState(false);
const accountID = Number(route.params.accountID);
const policyID = route.params.policyID;
const memberLogin = personalDetails?.[accountID]?.login ?? '';
const member = policy?.employeeList?.[memberLogin];
const prevMember = usePrevious(member);
const details = personalDetails?.[accountID] ?? ({} as PersonalDetails);
const fallbackIcon = details.fallbackIcon ?? '';
const displayName = details.displayName ?? '';
const isSelectedMemberOwner = policy?.owner === details.login;
const isSelectedMemberCurrentUser = accountID === currentUserPersonalDetails?.accountID;
const isCurrentUserAdmin = policy?.employeeList?.[personalDetails?.[currentUserPersonalDetails?.accountID]?.login ?? '']?.role === CONST.POLICY.ROLE.ADMIN;
const isCurrentUserOwner = policy?.owner === currentUserPersonalDetails?.login;
const ownerDetails = personalDetails?.[policy?.ownerAccountID ?? -1] ?? ({} as PersonalDetails);
const policyOwnerDisplayName = ownerDetails.displayName ?? policy?.owner ?? '';
const memberCards = useMemo(() => {
if (!cardsList) {
return [];
}
return Object.values(cardsList).filter((card) => card.accountID === accountID);
}, [cardsList, accountID]);
const confirmModalPrompt = useMemo(() => {
const isApprover = Member.isApprover(policy, accountID);
if (!isApprover) {
return translate('workspace.people.removeMemberPrompt', {memberName: displayName});
}
return translate('workspace.people.removeMembersWarningPrompt', {
memberName: displayName,
ownerName: policyOwnerDisplayName,
});
}, [accountID, policy, displayName, policyOwnerDisplayName, translate]);
const roleItems: ListItemType[] = useMemo(
() => [
{
value: CONST.POLICY.ROLE.ADMIN,
text: translate('common.admin'),
isSelected: member?.role === CONST.POLICY.ROLE.ADMIN,
keyForList: CONST.POLICY.ROLE.ADMIN,
},
{
value: CONST.POLICY.ROLE.USER,
text: translate('common.member'),
isSelected: member?.role !== CONST.POLICY.ROLE.ADMIN,
keyForList: CONST.POLICY.ROLE.USER,
},
],
[member?.role, translate],
);
useEffect(() => {
if (!prevMember || prevMember?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || member?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return;
}
Navigation.goBack();
}, [member, prevMember]);
const askForConfirmationToRemove = () => {
setIsRemoveMemberConfirmModalVisible(true);
};
const removeUser = useCallback(() => {
Member.removeMembers([accountID], policyID);
setIsRemoveMemberConfirmModalVisible(false);
}, [accountID, policyID]);
const navigateToProfile = useCallback(() => {
Navigation.navigate(ROUTES.PROFILE.getRoute(accountID, Navigation.getActiveRoute()));
}, [accountID]);
const navigateToDetails = useCallback(
(cardID: string) => {
Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID, Navigation.getActiveRoute()));
},
[policyID],
);
const navigateToIssueNewCard = useCallback(() => {
const activeRoute = Navigation.getActiveRoute();
Card.setIssueNewCardStepAndData({
step: CONST.EXPENSIFY_CARD.STEP.CARD_TYPE,
data: {
assigneeEmail: memberLogin,
},
isEditing: false,
});
Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD_ISSUE_NEW.getRoute(policyID, activeRoute));
}, [memberLogin, policyID]);
const openRoleSelectionModal = useCallback(() => {
setIsRoleSelectionModalVisible(true);
}, []);
const changeRole = useCallback(
({value}: ListItemType) => {
setIsRoleSelectionModalVisible(false);
Member.updateWorkspaceMembersRole(policyID, [accountID], value);
},
[accountID, policyID],
);
const startChangeOwnershipFlow = useCallback(() => {
Member.clearWorkspaceOwnerChangeFlow(policyID);
Member.requestWorkspaceOwnerChange(policyID);
Navigation.navigate(ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(policyID, accountID, 'amountOwed' as ValueOf<typeof CONST.POLICY.OWNERSHIP_ERRORS>));
}, [accountID, policyID]);
// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage =
!member || (member.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && prevMember?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
if (shouldShowNotFoundPage) {
return <NotFoundPage />;
}
return (
<AccessOrNotFoundWrapper
policyID={policyID}
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
>
<ScreenWrapper testID={WorkspaceMemberDetailsPage.displayName}>
{({safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithBackButton
title={displayName}
subtitle={policy?.name}
/>
<ScrollView contentContainerStyle={safeAreaPaddingBottomStyle}>
<View style={[styles.containerWithSpaceBetween, styles.pointerEventsBoxNone, styles.justifyContentStart]}>
<View style={[styles.avatarSectionWrapper, styles.pb0]}>
<OfflineWithFeedback pendingAction={details.pendingFields?.avatar}>
<Avatar
containerStyles={[styles.avatarXLarge, styles.mv5, styles.noOutline]}
imageStyles={[styles.avatarXLarge]}
source={details.avatar}
avatarID={accountID}
type={CONST.ICON_TYPE_AVATAR}
size={CONST.AVATAR_SIZE.XLARGE}
fallbackIcon={fallbackIcon}
/>
</OfflineWithFeedback>
{!!(details.displayName ?? '') && (
<Text
style={[styles.textHeadline, styles.pre, styles.mb6, styles.w100, styles.textAlignCenter]}
numberOfLines={1}
>
{displayName}
</Text>
)}
{isSelectedMemberOwner && isCurrentUserAdmin && !isCurrentUserOwner ? (
<Button
text={translate('workspace.people.transferOwner')}
onPress={startChangeOwnershipFlow}
medium
isDisabled={isOffline}
icon={Expensicons.Transfer}
iconStyles={StyleUtils.getTransformScaleStyle(0.8)}
style={styles.mv5}
/>
) : (
<Button
text={translate('workspace.people.removeWorkspaceMemberButtonTitle')}
onPress={askForConfirmationToRemove}
medium
isDisabled={isSelectedMemberOwner || isSelectedMemberCurrentUser}
icon={Expensicons.RemoveMembers}
iconStyles={StyleUtils.getTransformScaleStyle(0.8)}
style={styles.mv5}
/>
)}
<ConfirmModal
danger
title={translate('workspace.people.removeMemberTitle')}
isVisible={isRemoveMemberConfirmModalVisible}
onConfirm={removeUser}
onCancel={() => setIsRemoveMemberConfirmModalVisible(false)}
prompt={confirmModalPrompt}
confirmText={translate('common.remove')}
cancelText={translate('common.cancel')}
/>
</View>
<View style={styles.w100}>
<MenuItemWithTopDescription
disabled={isSelectedMemberOwner || isSelectedMemberCurrentUser}
title={member?.role === CONST.POLICY.ROLE.ADMIN ? translate('common.admin') : translate('common.member')}
description={translate('common.role')}
shouldShowRightIcon
onPress={openRoleSelectionModal}
/>
<MenuItem
style={styles.mb5}
title={translate('common.profile')}
icon={Expensicons.Info}
onPress={navigateToProfile}
shouldShowRightIcon
/>
<WorkspaceMemberDetailsRoleSelectionModal
isVisible={isRoleSelectionModalVisible}
items={roleItems}
onRoleChange={changeRole}
onClose={() => setIsRoleSelectionModalVisible(false)}
/>
{policy?.areExpensifyCardsEnabled && (
<>
<View style={[styles.ph5, styles.pv3]}>
<Text style={StyleUtils.combineStyles([styles.sidebarLinkText, styles.optionAlternateText, styles.textLabelSupporting])}>
{translate('walletPage.assignedCards')}
</Text>
</View>
{memberCards.map((card) => (
<MenuItem
title={card.nameValuePairs?.cardTitle}
badgeText={CurrencyUtils.convertAmountToDisplayString(card.nameValuePairs?.unapprovedExpenseLimit)}
icon={ExpensifyCardImage}
displayInDefaultIconColor
iconStyles={styles.cardIcon}
contentFit="contain"
iconWidth={variables.cardIconWidth}
iconHeight={variables.cardIconHeight}
onPress={() => navigateToDetails(card.cardID.toString())}
shouldShowRightIcon
/>
))}
<MenuItem
title={translate('workspace.expensifyCard.newCard')}
icon={Expensicons.Plus}
onPress={navigateToIssueNewCard}
/>
</>
)}
</View>
</View>
</ScrollView>
</>
)}
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
WorkspaceMemberDetailsPage.displayName = 'WorkspaceMemberDetailsPage';
export default withPolicyAndFullscreenLoading(WorkspaceMemberDetailsPage);