Skip to content

Commit

Permalink
Merge pull request #40700 from nexarvo/fix/40465
Browse files Browse the repository at this point in the history
implement offline behaviour for `updateGroupChatMemberRole`
  • Loading branch information
marcaaron authored May 6, 2024
2 parents 8b26576 + 927e162 commit 2c3f939
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
32 changes: 26 additions & 6 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2739,24 +2739,44 @@ function inviteToRoom(reportID: string, inviteeEmailsToAccountIDs: InvitedEmails
}

function updateGroupChatMemberRoles(reportID: string, accountIDList: number[], role: ValueOf<typeof CONST.REPORT.ROLE>) {
const participants: Participants = {};
const memberRoles: Record<number, string> = {};
const optimisticParticipants: Participants = {};
const successParticipants: Participants = {};

accountIDList.forEach((accountID) => {
memberRoles[accountID] = role;
participants[accountID] = {role};
optimisticParticipants[accountID] = {
role,
pendingFields: {
role: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
};
successParticipants[accountID] = {
pendingFields: {
role: null,
},
pendingAction: null,
};
});

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
participants,
},
value: {participants: optimisticParticipants},
},
];

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {participants: successParticipants},
},
];
const parameters: UpdateGroupChatMemberRolesParams = {reportID, memberRoles: JSON.stringify(memberRoles)};
API.write(WRITE_COMMANDS.UPDATE_GROUP_CHAT_MEMBER_ROLES, parameters, {optimisticData});
API.write(WRITE_COMMANDS.UPDATE_GROUP_CHAT_MEMBER_ROLES, parameters, {optimisticData, successData});
}

/** Invites people to a group chat */
Expand Down
17 changes: 10 additions & 7 deletions src/pages/ReportParticipantDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 Text from '@components/Text';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
Expand Down Expand Up @@ -119,13 +120,15 @@ function ReportParticipantDetails({personalDetails, report, route}: ReportPartic
</View>
<View style={styles.w100}>
{isCurrentUserAdmin && (
<MenuItemWithTopDescription
disabled={isSelectedMemberCurrentUser}
title={member?.role === CONST.REPORT.ROLE.ADMIN ? translate('common.admin') : translate('common.member')}
description={translate('common.role')}
shouldShowRightIcon
onPress={openRoleSelectionModal}
/>
<OfflineWithFeedback pendingAction={member?.pendingFields?.role ?? null}>
<MenuItemWithTopDescription
disabled={isSelectedMemberCurrentUser}
title={member?.role === CONST.REPORT.ROLE.ADMIN ? translate('common.admin') : translate('common.member')}
description={translate('common.role')}
shouldShowRightIcon
onPress={openRoleSelectionModal}
/>
</OfflineWithFeedback>
)}
<MenuItem
title={translate('common.profile')}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/ReportParticipantsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function ReportParticipantsPage({report, personalDetails, session}: ReportPartic
roleBadge = <Badge text={translate('common.admin')} />;
}

const pendingAction = pendingChatMember?.pendingAction ?? report.participants?.[accountID]?.pendingAction;

result.push({
keyForList: `${accountID}`,
accountID,
Expand All @@ -96,7 +98,7 @@ function ReportParticipantsPage({report, personalDetails, session}: ReportPartic
text: formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details)),
alternateText: formatPhoneNumber(details?.login ?? ''),
rightElement: roleBadge,
pendingAction: pendingChatMember?.pendingAction,
pendingAction,
icons: [
{
source: UserUtils.getAvatar(details?.avatar, accountID),
Expand Down
4 changes: 2 additions & 2 deletions src/types/onyx/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type PendingChatMember = {
pendingAction: OnyxCommon.PendingAction;
};

type Participant = {
type Participant = OnyxCommon.OnyxValueWithOfflineFeedback<{
hidden?: boolean;
role?: 'admin' | 'member';
};
}>;

type InvoiceReceiver =
| {
Expand Down

0 comments on commit 2c3f939

Please sign in to comment.