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

implement offline behaviour for updateGroupChatMemberRole #40700

Merged
merged 10 commits into from
May 6, 2024
35 changes: 31 additions & 4 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2696,17 +2696,44 @@ function updateGroupChatMemberRoles(reportID: string, accountIDList: number[], r
participants[accountID] = {role};
});

const optimisticParticipants: Participants = {};
const successParticipants: Participants = {};

Object.keys(participants).forEach((accountID) => {
const participantID = Number(accountID);
optimisticParticipants[participantID] = {
...participants[participantID],
pendingFields: {
role: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
};
nexarvo marked this conversation as resolved.
Show resolved Hide resolved

successParticipants[participantID] = {
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
Loading