Skip to content

Commit

Permalink
normalize comments state
Browse files Browse the repository at this point in the history
  • Loading branch information
thi4go committed May 7, 2021
1 parent 30772b8 commit fa05e15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
35 changes: 20 additions & 15 deletions src/reducers/models/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import unionBy from "lodash/unionBy";
import compose from "lodash/fp/compose";
import set from "lodash/fp/set";
import update from "lodash/fp/update";
import { shortRecordToken } from "src/helpers";
import {
PROPOSAL_STATUS_PUBLIC,
PROPOSAL_STATUS_UNREVIEWED
Expand All @@ -23,7 +24,7 @@ const comments = (state = DEFAULT_STATE, action) =>
{
[act.RECEIVE_RECORD_COMMENTS]: () => {
const { token, comments, accesstime } = action.payload;
const fullToken = comments[0]?.token || token;
const shortToken = shortRecordToken(token);
// Filter duplicated comments by signature. The latest copy found
// will be kept.
const filteredComments = uniqWith(
Expand All @@ -32,37 +33,38 @@ const comments = (state = DEFAULT_STATE, action) =>
arrVal.signature && arrVal.signaure !== othVal.signaure
);
return compose(
set(["comments", "byToken", fullToken], filteredComments),
set(["comments", "accessTimeByToken", fullToken], accesstime)
set(["comments", "byToken", shortToken], filteredComments),
set(["comments", "accessTimeByToken", shortToken], accesstime)
)(state);
},
[act.RECEIVE_NEW_COMMENT]: () => {
const comment = action.payload;
return update(
["comments", "byToken", comment.token],
["comments", "byToken", shortRecordToken(comment.token)],
(comments = []) => [...comments, comment]
)(state);
},
[act.RECEIVE_LIKED_COMMENTS]: () => {
const { token, commentslikes } = action.payload;
return set(
["commentsLikes", "byToken", token],
["commentsLikes", "byToken", shortRecordToken(token)],
commentslikes
)(state);
},
[act.RECEIVE_SYNC_LIKE_COMMENT]: () => {
const { token, vote, commentid } = action.payload;
const commentsLikes = state.commentsLikes.byToken[token];
const shortToken = shortRecordToken(token);
const commentsLikes = state.commentsLikes.byToken[shortToken];
const backupForCommentLikes = cloneDeep(commentsLikes);
const comments = state.comments.byToken[token];
const comments = state.comments.byToken[shortToken];
const isTargetCommentLike = (commentLike) =>
commentLike.commentid === commentid &&
commentLike.token === token;
commentLike.token === shortToken;
const oldCommentVote =
commentsLikes && commentsLikes.find(isTargetCommentLike);
const oldVote = oldCommentVote ? oldCommentVote.vote : 0;
const newCommentLike = {
token,
shortToken,
commentid,
vote: vote === oldVote ? 0 : vote
};
Expand Down Expand Up @@ -100,9 +102,9 @@ const comments = (state = DEFAULT_STATE, action) =>

return compose(
set(["commentsLikes", "backup"], backupForCommentLikes),
set(["commentsLikes", "byToken", token], newCommentsLikes),
set(["commentsLikes", "byToken", shortToken], newCommentsLikes),
set(["comments", "backup"], comments),
update(["comments", "byToken", token], (value) =>
update(["comments", "byToken", shortToken], (value) =>
value.map(updateCommentResultAndTotalVotes)
)
)(state);
Expand All @@ -111,11 +113,12 @@ const comments = (state = DEFAULT_STATE, action) =>
const { backup: commentsBackup } = state.comments;
const { backup: commentsLikesBackup } = state.commentsLikes;
const { token } = action.payload;
const shortToken = shortRecordToken(token);
return compose(
set(["commentsLikes", "backup"], null),
set(["commentsLikes", "byToken", token], commentsLikesBackup),
set(["commentsLikes", "byToken", shortToken], commentsLikesBackup),
set(["comments", "backup"], null),
set(["comments", "byToken", token], commentsBackup)
set(["comments", "byToken", shortToken], commentsBackup)
)(state);
},
[act.RECEIVE_LIKE_COMMENT]: () => {
Expand All @@ -135,7 +138,7 @@ const comments = (state = DEFAULT_STATE, action) =>
};
};
return compose(
update(["comments", "byToken", token], (comments) =>
update(["comments", "byToken", shortRecordToken(token)], (comments) =>
comments.map(censorTargetComment)
)
)(state);
Expand All @@ -146,7 +149,9 @@ const comments = (state = DEFAULT_STATE, action) =>
proposal.status === PROPOSAL_STATUS_PUBLIC &&
oldStatus === PROPOSAL_STATUS_UNREVIEWED
) {
delete state.comments.byToken[proposal.censorshiprecord.token];
delete state.comments.byToken[
shortRecordToken(proposal.censorshiprecord.token)
];
}
return state;
},
Expand Down
5 changes: 3 additions & 2 deletions src/selectors/models/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ export const commentsLikesByToken = get([
]);

const getCommentsByToken = (token) => (commentsByToken) => {
const comment = commentsByToken[token];
const shortToken = token && shortRecordToken(token);
const comment = commentsByToken[shortToken];
if (comment) return comment;
const commentsTokens = Object.keys(commentsByToken);
// check if the provided token is prefix of original token
const matchedTokenByPrefix = commentsTokens.find(
(key) => shortRecordToken(key) === token
(key) => key === shortToken
);
return commentsByToken[matchedTokenByPrefix];
};
Expand Down

0 comments on commit fa05e15

Please sign in to comment.