Skip to content

Commit

Permalink
rename and document functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg committed Feb 19, 2023
1 parent 4c07ba2 commit e064297
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
30 changes: 22 additions & 8 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ function hasAccountIDReacted(accountID, users, skinTone) {
* @param {{ name: string, code: string, types: string[] }} emoji
* @param {number} [skinTone] Optional.
*/
function addReaction(reportID, originalReportAction, emoji, skinTone) {
function addEmojiReaction(reportID, originalReportAction, emoji, skinTone) {
const message = originalReportAction.message[0];
let reactionObject = message.reactions && _.find(message.reactions, reaction => reaction.emoji === emoji.name);
const needToInsertReactionObject = !reactionObject;
Expand Down Expand Up @@ -1255,7 +1255,13 @@ function addReaction(reportID, originalReportAction, emoji, skinTone) {
// API.write('AddReaction', parameters, {optimisticData});
}

function removeReaction(reportID, originalReportAction, emoji) {
/**
* Removes a reaction to the report action.
* @param {String} reportID
* @param {Object} originalReportAction
* @param {{ name: string, code: string, types: string[] }} emoji
*/
function removeEmojiReaction(reportID, originalReportAction, emoji) {
const message = originalReportAction.message[0];
const reactionObject = message.reactions && _.find(message.reactions, reaction => reaction.emoji === emoji.name);
if (!reactionObject) {
Expand Down Expand Up @@ -1298,16 +1304,24 @@ function removeReaction(reportID, originalReportAction, emoji) {
// API.write('RemoveReaction', parameters, {optimisticData});
}

function toggleReaction(reportID, reportAction, emoji, skinTone) {
/**
* Calls either addEmojiReaction or removeEmojiReaction depending on if the current user has reacted to the report action.
* @param {String} reportID
* @param {Object} reportAction
* @param {Object} emoji
* @param {number} skinTone
* @returns {Promise}
*/
function toggleEmojiReaction(reportID, reportAction, emoji, skinTone) {
const message = reportAction.message[0];
const reactionObject = message.reactions && _.find(message.reactions, reaction => reaction.emoji === emoji.name);
if (reactionObject) {
const hasCurrentUserReacted = hasAccountIDReacted(currentUserAccountID, reactionObject.users, skinTone);
if (hasCurrentUserReacted) {
return removeReaction(reportID, reportAction, emoji, skinTone);
return removeEmojiReaction(reportID, reportAction, emoji, skinTone);
}
}
return addReaction(reportID, reportAction, emoji, skinTone);
return addEmojiReaction(reportID, reportAction, emoji, skinTone);
}

export {
Expand Down Expand Up @@ -1341,7 +1355,7 @@ export {
clearIOUError,
subscribeToNewActionEvent,
showReportActionNotification,
addReaction,
removeReaction,
toggleReaction,
addEmojiReaction,
removeEmojiReaction,
toggleEmojiReaction,
};
2 changes: 1 addition & 1 deletion src/pages/home/report/ContextMenu/ContextMenuActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default [

const onEmojiSelected = (emoji) => {
// TODO: we need to add the preferred skin tone here as well somehow
Report.toggleReaction(reportID, reportAction, emoji);
Report.toggleEmojiReaction(reportID, reportAction, emoji);
closeContextMenu();
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ReportActionItem extends Component {

toggleReaction(emoji) {
// TODO: we need to add the skin tone here somehow
Report.toggleReaction(this.props.report.reportID, this.props.action, emoji);
Report.toggleEmojiReaction(this.props.report.reportID, this.props.action, emoji);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/actions/ReportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ describe('actions/Report', () => {
const resultAction = _.first(_.values(reportActions));

// Add a reaction to the comment
Report.addReaction(REPORT_ID, resultAction, EMOJI);
Report.addEmojiReaction(REPORT_ID, resultAction, EMOJI);
return waitForPromisesToResolve();
})
.then(() => {
Expand All @@ -553,7 +553,7 @@ describe('actions/Report', () => {
})]));

// Now we remove the reaction
Report.removeReaction(REPORT_ID, resultAction, EMOJI);
Report.removeEmojiReaction(REPORT_ID, resultAction, EMOJI);
return waitForPromisesToResolve();
})
.then(() => {
Expand All @@ -566,11 +566,11 @@ describe('actions/Report', () => {
const resultAction = _.first(_.values(reportActions));

// Add the reaction to the comment, but two times with different variations
Report.addReaction(REPORT_ID, resultAction, EMOJI);
Report.addEmojiReaction(REPORT_ID, resultAction, EMOJI);
return waitForPromisesToResolve()
.then(() => {
const updatedResultAction = _.first(_.values(reportActions));
Report.addReaction(
Report.addEmojiReaction(
REPORT_ID,
updatedResultAction,
EMOJI,
Expand Down Expand Up @@ -598,7 +598,7 @@ describe('actions/Report', () => {
})]));

// Now we remove the reaction, and expect that both variations are removed
Report.removeReaction(REPORT_ID, updatedResultAction, EMOJI);
Report.removeEmojiReaction(REPORT_ID, updatedResultAction, EMOJI);
return waitForPromisesToResolve();
})
.then(() => {
Expand Down

0 comments on commit e064297

Please sign in to comment.