mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-20 03:15:29 -05:00
Update the reply_count when you leave a reply
This commit is contained in:
parent
03ce4470a4
commit
581f4d0cac
2 changed files with 19 additions and 4 deletions
|
@ -70,7 +70,16 @@ module.exports.commentsReducer = (state, action) => {
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
replies: Object.assign({}, state.replies, {
|
replies: Object.assign({}, state.replies, {
|
||||||
// Replies to comments go at the end of the thread
|
// Replies to comments go at the end of the thread
|
||||||
[action.topLevelCommentId]: state.replies[action.topLevelCommentId].concat(action.comment)
|
[action.topLevelCommentId]: (
|
||||||
|
state.replies[action.topLevelCommentId] || []).concat(action.comment)
|
||||||
|
}),
|
||||||
|
comments: state.comments.map(comment => {
|
||||||
|
if (comment.id === action.topLevelCommentId) {
|
||||||
|
return Object.assign({}, comment, {
|
||||||
|
reply_count: comment.reply_count + 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return comment;
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,9 +43,9 @@ tap.test('setComments', t => {
|
||||||
|
|
||||||
const commentState = {
|
const commentState = {
|
||||||
comments: [
|
comments: [
|
||||||
{id: 'id1', visibility: 'visible'},
|
{id: 'id1', visibility: 'visible', reply_count: 2},
|
||||||
{id: 'id2', visibility: 'visible'},
|
{id: 'id2', visibility: 'visible', reply_count: 0},
|
||||||
{id: 'id3', visibility: 'visible'}
|
{id: 'id3', visibility: 'visible', reply_count: 0}
|
||||||
],
|
],
|
||||||
replies: {
|
replies: {
|
||||||
id1: [
|
id1: [
|
||||||
|
@ -108,6 +108,12 @@ tap.test('addNewComment, reply comment', t => {
|
||||||
state = reducer(commentState, Comments.addNewComment({id: 'new comment'}, 'id1'));
|
state = reducer(commentState, Comments.addNewComment({id: 'new comment'}, 'id1'));
|
||||||
// Adds replies to the end of the replies list
|
// Adds replies to the end of the replies list
|
||||||
t.equal(state.replies.id1[2].id, 'new comment');
|
t.equal(state.replies.id1[2].id, 'new comment');
|
||||||
|
t.equal(state.comments[0].reply_count, 3);
|
||||||
|
|
||||||
|
// Also check a top-level comment that doesn't have replies yet
|
||||||
|
state = reducer(commentState, Comments.addNewComment({id: 'new comment2'}, 'id2'));
|
||||||
|
t.equal(state.replies.id2[0].id, 'new comment2');
|
||||||
|
t.equal(state.comments[1].reply_count, 1);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue