From 63cdb89144f27929928f819b4ffae936ff79ac15 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 4 Apr 2017 00:21:15 -0400 Subject: PLT-6147 Fixed reactions not rendering properly while loading (#5958) --- webapp/stores/reaction_store.jsx | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'webapp/stores') diff --git a/webapp/stores/reaction_store.jsx b/webapp/stores/reaction_store.jsx index 166569e3d..51ad5140e 100644 --- a/webapp/stores/reaction_store.jsx +++ b/webapp/stores/reaction_store.jsx @@ -37,36 +37,36 @@ class ReactionStore extends EventEmitter { } addReaction(postId, reaction) { - const reactions = []; - - for (const existing of this.getReactions(postId)) { - // make sure not to add duplicates - if (existing.user_id !== reaction.user_id || existing.post_id !== reaction.post_id || - existing.emoji_name !== reaction.emoji_name) { - reactions.push(existing); - } - } + let reactions = this.getReactions(postId) || []; + + // Make sure not to add duplicates + const existingIndex = reactions.findIndex((existing) => { + return existing.user_id === reaction.user_id && existing.post_id === reaction.post_id && existing.emoji_name === reaction.emoji_name; + }); - reactions.push(reaction); + if (existingIndex === -1) { + reactions = [...reactions, reaction]; + } this.setReactions(postId, reactions); } removeReaction(postId, reaction) { - const reactions = []; + let reactions = this.getReactions(postId) || []; + + const existingIndex = reactions.findIndex((existing) => { + return existing.user_id === reaction.user_id && existing.post_id === reaction.post_id && existing.emoji_name === reaction.emoji_name; + }); - for (const existing of this.getReactions(postId)) { - if (existing.user_id !== reaction.user_id || existing.post_id !== reaction.post_id || - existing.emoji_name !== reaction.emoji_name) { - reactions.push(existing); - } + if (existingIndex !== -1) { + reactions = reactions.slice(0, existingIndex).concat(reactions.slice(existingIndex + 1)); } this.setReactions(postId, reactions); } getReactions(postId) { - return this.reactions.get(postId) || []; + return this.reactions.get(postId); } handleEventPayload(payload) { @@ -89,4 +89,4 @@ class ReactionStore extends EventEmitter { } } -export default new ReactionStore(); \ No newline at end of file +export default new ReactionStore(); -- cgit v1.2.3-1-g7c22