Reducers properly handle create, delete, and list operations for comment replies
This commit is contained in:
parent
40cea617ee
commit
624ef0c9b7
2 changed files with 64 additions and 12 deletions
src/redux
|
@ -4,7 +4,13 @@ import Lbry from 'lbry';
|
||||||
import { selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
|
import { selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
|
||||||
import { doToast } from 'redux/actions/notifications';
|
import { doToast } from 'redux/actions/notifications';
|
||||||
|
|
||||||
export function doCommentList(uri: string, page: number = 1, pageSize: number = 99999) {
|
// if parentId is provided, `uri` is ignored
|
||||||
|
export function doCommentList(
|
||||||
|
uri: string,
|
||||||
|
page: number = 1,
|
||||||
|
pageSize: number = 99999, // while the desktop app doesn't paginate
|
||||||
|
parentId?: string
|
||||||
|
) {
|
||||||
return (dispatch: Dispatch, getState: GetState) => {
|
return (dispatch: Dispatch, getState: GetState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
const claim = selectClaimsByUri(state)[uri];
|
const claim = selectClaimsByUri(state)[uri];
|
||||||
|
@ -17,6 +23,7 @@ export function doCommentList(uri: string, page: number = 1, pageSize: number =
|
||||||
claim_id: claimId,
|
claim_id: claimId,
|
||||||
page,
|
page,
|
||||||
page_size: pageSize,
|
page_size: pageSize,
|
||||||
|
parentId: parentId,
|
||||||
})
|
})
|
||||||
.then((result: CommentListResponse) => {
|
.then((result: CommentListResponse) => {
|
||||||
const { items: comments } = result;
|
const { items: comments } = result;
|
||||||
|
@ -26,6 +33,7 @@ export function doCommentList(uri: string, page: number = 1, pageSize: number =
|
||||||
comments,
|
comments,
|
||||||
claimId: claimId,
|
claimId: claimId,
|
||||||
uri: uri,
|
uri: uri,
|
||||||
|
parentId: parentId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -66,6 +74,7 @@ export function doCommentCreate(
|
||||||
data: {
|
data: {
|
||||||
comment: result,
|
comment: result,
|
||||||
claimId: claim_id,
|
claimId: claim_id,
|
||||||
|
parentId: parent_id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,6 +7,7 @@ const defaultState: CommentsState = {
|
||||||
byId: {}, // ClaimID -> list of comments
|
byId: {}, // ClaimID -> list of comments
|
||||||
commentsByUri: {}, // URI -> claimId
|
commentsByUri: {}, // URI -> claimId
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
repliesByCommentId: {}, // commentId -> list of commentIds
|
||||||
myComments: undefined,
|
myComments: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,9 +24,10 @@ export const commentReducer = handleActions(
|
||||||
}),
|
}),
|
||||||
|
|
||||||
[ACTIONS.COMMENT_CREATE_COMPLETED]: (state: CommentsState, action: any): CommentsState => {
|
[ACTIONS.COMMENT_CREATE_COMPLETED]: (state: CommentsState, action: any): CommentsState => {
|
||||||
const { comment, claimId }: { comment: Comment, claimId: string } = action.data;
|
const { comment, claimId, parentId } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
const comments = byId[claimId];
|
const comments = byId[claimId];
|
||||||
const newCommentIds = comments.slice();
|
const newCommentIds = comments.slice();
|
||||||
|
|
||||||
|
@ -36,6 +38,13 @@ export const commentReducer = handleActions(
|
||||||
newCommentIds.unshift(comment.comment_id);
|
newCommentIds.unshift(comment.comment_id);
|
||||||
byId[claimId] = newCommentIds;
|
byId[claimId] = newCommentIds;
|
||||||
|
|
||||||
|
if (parentId) {
|
||||||
|
const newReplies = repliesByCommentId[parentId] || [];
|
||||||
|
// unlike regular comments, newest replies should be at the bottom of list
|
||||||
|
newReplies.push(comment.comment_id);
|
||||||
|
repliesByCommentId[parentId] = newReplies;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
commentById,
|
commentById,
|
||||||
|
@ -47,32 +56,50 @@ export const commentReducer = handleActions(
|
||||||
[ACTIONS.COMMENT_LIST_STARTED]: state => ({ ...state, isLoading: true }),
|
[ACTIONS.COMMENT_LIST_STARTED]: state => ({ ...state, isLoading: true }),
|
||||||
|
|
||||||
[ACTIONS.COMMENT_LIST_COMPLETED]: (state: CommentsState, action: any) => {
|
[ACTIONS.COMMENT_LIST_COMPLETED]: (state: CommentsState, action: any) => {
|
||||||
const { comments, claimId, uri } = action.data;
|
const { comments, claimId, uri, parentId } = action.data;
|
||||||
|
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
const commentsByUri = Object.assign({}, state.commentsByUri);
|
const commentsByUri = Object.assign({}, state.commentsByUri);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
if (comments) {
|
if (comments) {
|
||||||
// we use an Array to preserve order of listing
|
// we use an Array to preserve order of listing
|
||||||
// in reality this doesn't matter and we can just
|
// in reality this doesn't matter and we can just
|
||||||
// sort comments by their timestamp
|
// sort comments by their timestamp
|
||||||
const commentIds = Array(comments.length);
|
const commentIds = Array(comments.length);
|
||||||
|
const replyThreads = {};
|
||||||
|
|
||||||
// map the comment_ids to the new comments
|
// map the comment_ids to the new comments
|
||||||
for (let i = 0; i < comments.length; i++) {
|
for (let i = 0; i < comments.length; i++) {
|
||||||
commentIds[i] = comments[i].comment_id;
|
const comment = comments[i];
|
||||||
commentById[commentIds[i]] = comments[i];
|
commentIds[i] = comment.comment_id;
|
||||||
|
commentById[commentIds[i]] = comment;
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
if (!(comment.parent_id in replyThreads)) {
|
||||||
|
replyThreads[comment.parent_id] = [];
|
||||||
|
}
|
||||||
|
replyThreads[comment.parent_id].push(comment.comment_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byId[claimId] = commentIds;
|
Object.entries(replyThreads).forEach((parent_id, replyIds) => {
|
||||||
|
repliesByCommentId[parent_id] = replyIds;
|
||||||
|
});
|
||||||
|
|
||||||
commentsByUri[uri] = claimId;
|
commentsByUri[uri] = claimId;
|
||||||
|
// don't override the entire list with the replies to one comment
|
||||||
|
if (parentId == null) {
|
||||||
|
byId[claimId] = commentIds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
byId,
|
byId,
|
||||||
commentById,
|
commentById,
|
||||||
commentsByUri,
|
commentsByUri,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
@ -89,21 +116,37 @@ export const commentReducer = handleActions(
|
||||||
const { comment_id } = action.data;
|
const { comment_id } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
// to remove the comment and its references
|
const comment: Comment = commentById[comment_id];
|
||||||
const claimId = commentById[comment_id].claim_id;
|
|
||||||
for (let i = 0; i < byId[claimId].length; i++) {
|
// keep record of comment's existence if it has replies
|
||||||
if (byId[claimId][i] === comment_id) {
|
if (!(comment.comment_id in repliesByCommentId)) {
|
||||||
byId[claimId].splice(i, 1);
|
const claimId = commentById[comment_id].claim_id;
|
||||||
break;
|
for (let i = 0; i < byId[claimId].length; i++) {
|
||||||
|
if (byId[claimId][i] === comment_id) {
|
||||||
|
byId[claimId].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
for (let i = 0; i < repliesByCommentId[comment.parent_id]; i++) {
|
||||||
|
if (repliesByCommentId[comment.parent_id][i] === comment.comment_id) {
|
||||||
|
repliesByCommentId[comment.parent_id].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete commentById[comment_id];
|
delete commentById[comment_id];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
commentById,
|
commentById,
|
||||||
byId,
|
byId,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue