Skip to content

Commit

Permalink
Merge pull request #46692 from bernhardoj/fix/46344-only-allow-owner-…
Browse files Browse the repository at this point in the history
…to-delete-ws
  • Loading branch information
blimpich authored Aug 2, 2024
2 parents 63e5fe1 + def7a98 commit 7e3e032
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
});
});

const policy = getPolicy(policyID);
// Restore the old report stateNum and statusNum
const failureData: OnyxUpdate[] = [
{
Expand All @@ -334,6 +335,13 @@ function deleteWorkspace(policyID: string, policyName: string) {
errors: reimbursementAccount?.errors ?? null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
avatarURL: policy?.avatarURL,
},
},
];

reportsToArchive.forEach((report) => {
Expand Down
23 changes: 14 additions & 9 deletions src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {useCallback, useState} from 'react';
import type {ImageStyle, StyleProp} from 'react-native';
import {Image, StyleSheet, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import Avatar from '@components/Avatar';
import AvatarWithImagePicker from '@components/AvatarWithImagePicker';
import Button from '@components/Button';
Expand Down Expand Up @@ -55,6 +55,8 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();
const {canUseSpotnanaTravel} = usePermissions();

const [currentUserAccountID = -1] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID});

// When we create a new workspace, the policy prop will be empty on the first render. Therefore, we have to use policyDraft until policy has been set in Onyx.
const policy = policyDraft?.id ? policyDraft : policyProp;
const outputCurrency = policy?.outputCurrency ?? '';
Expand Down Expand Up @@ -84,6 +86,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
}),
);
const readOnly = !PolicyUtils.isPolicyAdmin(policy);
const isOwner = PolicyUtils.isPolicyOwner(policy, currentUserAccountID);
const imageStyle: StyleProp<ImageStyle> = shouldUseNarrowLayout ? [styles.mhv12, styles.mhn5, styles.mbn5] : [styles.mhv8, styles.mhn8, styles.mbn5];
const shouldShowAddress = !readOnly || formattedAddress;

Expand Down Expand Up @@ -270,14 +273,16 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
medium
icon={Expensicons.QrCode}
/>
<Button
accessibilityLabel={translate('common.delete')}
text={translate('common.delete')}
style={[styles.ml2]}
onPress={() => setIsDeleteModalOpen(true)}
medium
icon={Expensicons.Trashcan}
/>
{isOwner && (
<Button
accessibilityLabel={translate('common.delete')}
text={translate('common.delete')}
style={[styles.ml2]}
onPress={() => setIsDeleteModalOpen(true)}
medium
icon={Expensicons.Trashcan}
/>
)}
</View>
)}
</Section>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspacesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function WorkspacesListPage({policies, reimbursementAccount, reports, session}:
// In such cases, let us use the available chat report ids from the policy.
const threeDotsMenuItems: PopoverMenuItem[] = [];

if (isAdmin) {
if (isOwner) {
threeDotsMenuItems.push({
icon: Expensicons.Trashcan,
text: translate('workspace.common.delete'),
Expand Down

0 comments on commit 7e3e032

Please sign in to comment.