Updates comment-related redux code to support sdk version 0.53.0 #259
1 changed files with 24 additions and 6 deletions
|
@ -3,8 +3,9 @@ import * as ACTIONS from 'constants/action_types';
|
||||||
import { handleActions } from 'util/redux-utils';
|
import { handleActions } from 'util/redux-utils';
|
||||||
|
|
||||||
const defaultState: CommentsState = {
|
const defaultState: CommentsState = {
|
||||||
byId: {},
|
commentById: {}, // commentId -> Comment
|
||||||
commentsByUri: {},
|
byId: {}, // ClaimID -> list of comments
|
||||||
|
commentsByUri: {}, // URI -> claimId
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
myComments: undefined,
|
myComments: undefined,
|
||||||
};
|
};
|
||||||
|
@ -23,16 +24,23 @@ 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 }: any = action.data;
|
const { comment, claimId }: any = action.data;
|
||||||
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
const comments = byId[claimId];
|
const comments = byId[claimId];
|
||||||
const newComments = comments.slice();
|
const newCommentIds = comments.slice();
|
||||||
|
|
||||||
newComments.unshift(comment);
|
// add the comment by its ID
|
||||||
byId[claimId] = newComments;
|
commentById[comment.comment_id] = comment;
|
||||||
|
|
||||||
|
// push the comment_id to the top of ID list
|
||||||
|
newCommentIds.unshift(comment.comment_id);
|
||||||
|
byId[claimId] = newCommentIds;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
commentById,
|
||||||
byId,
|
byId,
|
||||||
|
isLoading: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -40,16 +48,26 @@ export const commentReducer = handleActions(
|
||||||
|
|
||||||
[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 } = action.data;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
if (comments) {
|
if (comments) {
|
||||||
byId[claimId] = comments;
|
const commentIds = Array(comments.length);
|
||||||
|
// map the comment_ids to the new comments
|
||||||
|
for (let i = 0; i < comments.length; i++) {
|
||||||
|
commentIds[i] = comments[i].comment_id;
|
||||||
|
commentById[commentIds[i]] = comments[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
byId[claimId] = commentIds;
|
||||||
commentsByUri[uri] = claimId;
|
commentsByUri[uri] = claimId;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
byId,
|
byId,
|
||||||
|
commentById,
|
||||||
commentsByUri,
|
commentsByUri,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue