Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,8 @@
role: ValueOf<typeof CONST.POLICY.ROLE>;
};

const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!key) {
return;
}
if (val === null || val === undefined) {
delete allPolicies[key];
return;
}

allPolicies[key] = val;
},
});

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 46 in src/libs/actions/Policy/Member.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function

Check warning on line 46 in src/libs/actions/Policy/Member.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => (allReportActions = actions),
Expand All @@ -84,17 +68,6 @@
);
}

/**
* Returns the policy of the report
* @deprecated Get the data straight from Onyx - This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
*/
function getPolicy(policyID: string | undefined): OnyxEntry<Policy> {
if (!allPolicies || !policyID) {
return undefined;
}
return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
}

/**
* Build optimistic data for adding members to the announcement/admins room
*/
Expand Down Expand Up @@ -780,14 +753,12 @@
API.write(WRITE_COMMANDS.UPDATE_WORKSPACE_MEMBERS_ROLE, params, {optimisticData, successData, failureData});
}

function requestWorkspaceOwnerChange(policyID: string | undefined, currentUserAccountID: number, currentUserAccountLogin: string) {
if (!policyID) {
function requestWorkspaceOwnerChange(policy: OnyxEntry<Policy>, currentUserAccountID: number, currentUserAccountLogin: string) {
if (!policy?.id) {
return;
}

// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);
const policyID = policy.id;
const ownershipChecks = {...policyOwnershipChecks?.[policyID]};

const changeOwnerErrors = Object.keys(policy?.errorFields?.changeOwner ?? {});
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspaceOverviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa

const startChangeOwnershipFlow = useCallback(() => {
clearWorkspaceOwnerChangeFlow(policyID);
requestWorkspaceOwnerChange(policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
requestWorkspaceOwnerChange(policy, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
Navigation.navigate(
ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(
policyID,
Expand All @@ -355,7 +355,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
Navigation.getActiveRoute(),
),
);
}, [currentUserPersonalDetails.accountID, currentUserPersonalDetails.login, policyID]);
}, [currentUserPersonalDetails.accountID, currentUserPersonalDetails.login, policyID, policy]);

const handleLeave = useCallback(() => {
const isReimburser = policy?.achAccount?.reimburser === session?.email;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspacesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function WorkspacesListPage() {
}

clearWorkspaceOwnerChangeFlow(policyID);
requestWorkspaceOwnerChange(policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
requestWorkspaceOwnerChange(policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`], currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
Navigation.navigate(
ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(
policyID,
Expand All @@ -342,7 +342,7 @@ function WorkspacesListPage() {
),
);
},
[currentUserPersonalDetails.accountID, currentUserPersonalDetails.login],
[currentUserPersonalDetails.accountID, currentUserPersonalDetails.login, policies],
);

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/members/WorkspaceOwnerChangeCheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function WorkspaceOwnerChangeCheck({policy, accountID, error}: WorkspaceOwnerCha
return;
}

requestWorkspaceOwnerChange(policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
}, [accountID, error, policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login]);
requestWorkspaceOwnerChange(policy, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
}, [policy, accountID, error, policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function WorkspaceOwnerChangeWrapperPage({route, policy}: WorkspaceOwnerChangeWr
if (policy?.isChangeOwnerFailed || policy?.isChangeOwnerSuccessful) {
return;
}
requestWorkspaceOwnerChange(policyID, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
requestWorkspaceOwnerChange(policy, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login ?? '');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [policyID]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-5 (docs)

The ESLint rule react-hooks/exhaustive-deps is disabled here, but the comment doesn't explain why it's necessary to bypass the rule.

Additionally, after this refactor, the policy object is now used in the effect body (line 45 in the diff), but it's not included in the dependency array. This could cause stale closures where the effect doesn't re-run when the policy changes.

Suggested fix:

Either add policy to the dependency array, or if you intentionally want this effect to only run once on mount, document why in the disable comment:

// eslint-disable-next-line react-hooks/exhaustive-deps -- This effect should only run once on mount when policyID changes
}, [policyID]);

Or update the dependency array to include all dependencies:

}, [policyID, policy, currentUserPersonalDetails.accountID, currentUserPersonalDetails.login]);

Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.


Expand Down
2 changes: 1 addition & 1 deletion tests/actions/PolicyMemberTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('actions/PolicyMember', () => {

mockFetch?.pause?.();
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
Member.requestWorkspaceOwnerChange(fakePolicy.id, fakeAccountID, fakeEmail);
Member.requestWorkspaceOwnerChange(fakePolicy, fakeAccountID, fakeEmail);
await waitForBatchedUpdates();
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
Expand Down
Loading