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

Refactor Report_TogglePinned in App #9201

Merged
merged 8 commits into from
Jun 9, 2022
Merged
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
1 change: 0 additions & 1 deletion src/libs/Pusher/EventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default {
REPORT_COMMENT_CHUNK: 'chunked-reportComment',
REPORT_COMMENT_EDIT_CHUNK: 'chunked-reportCommentEdit',
REPORT_COMMENT_EDIT: 'reportCommentEdit',
REPORT_TOGGLE_PINNED: 'reportTogglePinned',
PREFERRED_LOCALE: 'preferredLocale',
EXPENSIFY_CARD_UPDATE: 'expensifyCardUpdate',
SCREEN_SHARE_REQUEST: 'screenshareRequest',
Expand Down
31 changes: 13 additions & 18 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Visibility from '../Visibility';
import ROUTES from '../../ROUTES';
import Timing from './Timing';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import CONFIG from '../../CONFIG';
import CONST from '../../CONST';
import Log from '../Log';
Expand Down Expand Up @@ -703,16 +704,6 @@ function updateReportWithNewAction(
});
}

/**
* Updates a report in Onyx with a new pinned state.
*
* @param {Number} reportID
* @param {Boolean} isPinned
*/
function updateReportPinnedState(reportID, isPinned) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {isPinned});
}

/**
* Get the private pusher channel name for a Report.
*
Expand Down Expand Up @@ -764,11 +755,6 @@ function subscribeToUserEvents() {
pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message),
true,
);

// Live-update a report's pinned state when a 'report toggle pinned' event is received.
PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_TOGGLE_PINNED,
currentUserAccountID,
pushJSON => updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned));
}

/**
Expand Down Expand Up @@ -1270,11 +1256,20 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false
*/
function togglePinnedState(report) {
const pinnedValue = !report.isPinned;
updateReportPinnedState(report.reportID, pinnedValue);
DeprecatedAPI.Report_TogglePinned({

// Optimistically pin/unpin the report before we send out the command
const optimisticData = [
{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
value: {isPinned: pinnedValue},
},
];

API.write('TogglePinnedChat', {
reportID: report.reportID,
pinnedValue,
});
}, {optimisticData});
}

/**
Expand Down
26 changes: 2 additions & 24 deletions tests/actions/ReportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('actions/Report', () => {
});
});

it('should update pins in Onyx when togglePinned event is handled via Pusher', () => {
it('should update pins in Onyx when togglePinned is called', () => {
const TEST_USER_ACCOUNT_ID = 1;
const TEST_USER_LOGIN = 'test@test.com';
const REPORT_ID = 1;
Expand All @@ -143,34 +143,12 @@ describe('actions/Report', () => {

// Set up Onyx with some test user data
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
.then(() => {
Report.subscribeToUserEvents();
return waitForPromisesToResolve();
})
.then(() => {
Report.togglePinnedState(REPORT);
return waitForPromisesToResolve();
})
.then(() => {
// Before pusher event gets sent back, test that Onyx immediately updated the report pin state.
expect(reportIsPinned).toEqual(true);
})
.then(() => {
// We subscribed to the Pusher channel above and now we need to simulate a reportTogglePinned
// Pusher event so we can verify that pinning was handled correctly and merged into the report.
const channel = Pusher.getChannel(`private-encrypted-user-accountID-1${CONFIG.PUSHER.SUFFIX}`);
channel.emit(Pusher.TYPE.REPORT_TOGGLE_PINNED, {
reportID: REPORT_ID,
isPinned: true,
});

// Once an event is emitted to the Pusher channel we should see the report pin get updated
// by the Pusher callback and added to the storage so we must wait for promises to resolve again and
// then verify the data is in Onyx.
return waitForPromisesToResolve();
})
.then(() => {
// Make sure the pin state gets from the Pusher callback into Onyx.
// Test that Onyx immediately updated the report pin state.
expect(reportIsPinned).toEqual(true);
});
});
Expand Down