Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unable to select members after adding them to the workspace #36713

Merged
merged 10 commits into from
Mar 4, 2024
Merged
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"react-native-linear-gradient": "^2.8.1",
"react-native-localize": "^2.2.6",
"react-native-modal": "^13.0.0",
"react-native-onyx": "2.0.7",
"react-native-onyx": "2.0.10",
"react-native-pager-view": "6.2.2",
"react-native-pdf": "6.7.3",
"react-native-performance": "^5.1.0",
Expand Down
14 changes: 0 additions & 14 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4523,19 +4523,6 @@ function getReportOfflinePendingActionAndErrors(report: OnyxEntry<Report>): Repo
return {reportPendingAction, reportErrors};
}

function getPolicyExpenseChatReportIDByOwner(policyOwner: string): string | null {
const policyWithOwner = Object.values(allPolicies ?? {}).find((policy) => policy?.owner === policyOwner);
if (!policyWithOwner) {
return null;
}

const expenseChat = Object.values(allReports ?? {}).find((report) => isPolicyExpenseChat(report) && report?.policyID === policyWithOwner.id);
if (!expenseChat) {
return null;
}
return expenseChat.reportID;
}

/**
* Check if the report can create the request with type is iouType
*/
Expand Down Expand Up @@ -5219,7 +5206,6 @@ export {
isDM,
isSelfDM,
getPolicy,
getPolicyExpenseChatReportIDByOwner,
getWorkspaceChats,
shouldDisableRename,
hasSingleParticipant,
Expand Down
23 changes: 21 additions & 2 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import playSoundExcludingMobile from '@libs/Sound/playSoundExcludingMobile';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BlockedFromConcierge, FrequentlyUsedEmoji} from '@src/types/onyx';
import type {BlockedFromConcierge, FrequentlyUsedEmoji, Policy} from '@src/types/onyx';
import type Login from '@src/types/onyx/Login';
import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer';
import type OnyxPersonalDetails from '@src/types/onyx/PersonalDetails';
Expand Down Expand Up @@ -775,7 +775,7 @@ function generateStatementPDF(period: string) {
/**
* Sets a contact method / secondary login as the user's "Default" contact method.
*/
function setContactMethodAsDefault(newDefaultContactMethod: string) {
function setContactMethodAsDefault(newDefaultContactMethod: string, policies: OnyxCollection<Pick<Policy, 'id' | 'ownerAccountID' | 'owner'>>) {
const oldDefaultContactMethod = currentEmail;
const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -868,6 +868,25 @@ function setContactMethodAsDefault(newDefaultContactMethod: string) {
},
];

Object.values(policies ?? {}).forEach((policy) => {
if (policy?.ownerAccountID !== currentUserAccountID) {
return;
}
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
value: {
owner: newDefaultContactMethod,
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, this PR didn't address the need to optimistically update the policy employeeList by removing the previous contact method and adding the new one. More details can be found here: #44705 (comment)

},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
value: {
owner: policy.owner,
},
});
});
const parameters: SetContactMethodAsDefaultParams = {
partnerUserID: newDefaultContactMethod,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {LoginList, SecurityGroup, Session as TSession} from '@src/types/onyx';
import type {LoginList, Policy, SecurityGroup, Session as TSession} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import ValidateCodeForm from './ValidateCodeForm';
import type {ValidateCodeFormHandle} from './ValidateCodeForm/BaseValidateCodeForm';
Expand All @@ -48,11 +48,14 @@ type ContactMethodDetailsPageOnyxProps = {

/** Indicated whether the report data is loading */
isLoadingReportData: OnyxEntry<boolean>;

/** The list of this user's policies */
policies: OnyxCollection<Pick<Policy, 'id' | 'ownerAccountID' | 'owner'>>;
};

type ContactMethodDetailsPageProps = ContactMethodDetailsPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.CONTACT_METHOD_DETAILS>;

function ContactMethodDetailsPage({loginList, session, myDomainSecurityGroups, securityGroups, isLoadingReportData = true, route}: ContactMethodDetailsPageProps) {
function ContactMethodDetailsPage({loginList, session, myDomainSecurityGroups, securityGroups, isLoadingReportData = true, route, policies}: ContactMethodDetailsPageProps) {
const {formatPhoneNumber, translate} = useLocalize();
const theme = useTheme();
const themeStyles = useThemeStyles();
Expand Down Expand Up @@ -88,8 +91,8 @@ function ContactMethodDetailsPage({loginList, session, myDomainSecurityGroups, s
* Attempt to set this contact method as user's "Default contact method"
*/
const setAsDefault = useCallback(() => {
User.setContactMethodAsDefault(contactMethod);
}, [contactMethod]);
User.setContactMethodAsDefault(contactMethod, policies);
}, [contactMethod, policies]);

/**
* Checks if the user is allowed to change their default contact method. This should only be allowed if:
Expand Down Expand Up @@ -302,4 +305,12 @@ export default withOnyx<ContactMethodDetailsPageProps, ContactMethodDetailsPageO
isLoadingReportData: {
key: `${ONYXKEYS.IS_LOADING_REPORT_DATA}`,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: (data) => ({
id: data?.id ?? '',
ownerAccountID: data?.ownerAccountID,
owner: data?.owner ?? '',
}),
},
})(ContactMethodDetailsPage);
5 changes: 2 additions & 3 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ function WorkspaceMembersPage({policyMembers, personalDetails, route, policy, se
*/
const validateSelection = useCallback(() => {
const newErrors: Errors = {};
const ownerAccountID = PersonalDetailsUtils.getAccountIDsByLogins(policy?.owner ? [policy.owner] : [])[0];
selectedEmployees.forEach((member) => {
if (member !== ownerAccountID && member !== session?.accountID) {
if (member !== policy?.ownerAccountID && member !== session?.accountID) {
return;
}
newErrors[member] = translate('workspace.people.error.cannotRemove');
Expand Down Expand Up @@ -326,7 +325,7 @@ function WorkspaceMembersPage({policyMembers, personalDetails, route, policy, se
isSelected,
isDisabled:
accountID === session?.accountID ||
details.login === policy?.owner ||
accountID === policy?.ownerAccountID ||
policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE ||
!isEmptyObject(policyMember.errors),
text: formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details)),
Expand Down
Loading