lbry-redux/src/redux/reducers/comments.js

157 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-06-11 21:14:37 -04:00
// @flow
import * as ACTIONS from 'constants/action_types';
2019-06-26 19:13:29 -04:00
import { handleActions } from 'util/redux-utils';
2019-06-11 21:14:37 -04:00
const defaultState: CommentsState = {
commentById: {}, // commentId -> Comment
byId: {}, // ClaimID -> list of comments
commentsByUri: {}, // URI -> claimId
2019-06-11 21:14:37 -04:00
isLoading: false,
myComments: undefined,
2019-06-11 21:14:37 -04:00
};
2019-06-26 19:13:29 -04:00
export const commentReducer = handleActions(
{
[ACTIONS.COMMENT_CREATE_STARTED]: (state: CommentsState, action: any): CommentsState => ({
...state,
isLoading: true,
}),
[ACTIONS.COMMENT_CREATE_FAILED]: (state: CommentsState, action: any) => ({
...state,
isLoading: false,
}),
[ACTIONS.COMMENT_CREATE_COMPLETED]: (state: CommentsState, action: any): CommentsState => {
2020-01-13 16:52:23 -05:00
const { comment, claimId }: { comment: Comment, claimId: string } = action.data;
const commentById = Object.assign({}, state.commentById);
2019-06-26 19:13:29 -04:00
const byId = Object.assign({}, state.byId);
const comments = byId[claimId];
const newCommentIds = comments.slice();
2019-06-26 19:13:29 -04:00
// add the comment by its ID
commentById[comment.comment_id] = comment;
// push the comment_id to the top of ID list
newCommentIds.unshift(comment.comment_id);
byId[claimId] = newCommentIds;
2019-06-26 19:13:29 -04:00
return {
...state,
commentById,
2019-06-26 19:13:29 -04:00
byId,
isLoading: false,
2019-06-26 19:13:29 -04:00
};
},
[ACTIONS.COMMENT_LIST_STARTED]: state => ({ ...state, isLoading: true }),
[ACTIONS.COMMENT_LIST_COMPLETED]: (state: CommentsState, action: any) => {
const { comments, claimId, uri } = action.data;
const commentById = Object.assign({}, state.commentById);
2019-06-26 19:13:29 -04:00
const byId = Object.assign({}, state.byId);
const commentsByUri = Object.assign({}, state.commentsByUri);
if (comments) {
2020-01-13 16:52:23 -05:00
// we use an Array to preserve order of listing
// in reality this doesn't matter and we can just
// sort comments by their timestamp
const commentIds = Array(comments.length);
2020-01-13 16:52:23 -05:00
// 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;
2019-06-26 19:13:29 -04:00
commentsByUri[uri] = claimId;
}
return {
...state,
byId,
commentById,
2019-06-26 19:13:29 -04:00
commentsByUri,
isLoading: false,
};
},
[ACTIONS.COMMENT_LIST_FAILED]: (state: CommentsState, action: any) => ({
...state,
isLoading: false,
}),
[ACTIONS.COMMENT_ABANDON_STARTED]: (state: CommentsState, action: any) => ({
...state,
isLoading: true,
}),
2020-01-13 16:52:23 -05:00
[ACTIONS.COMMENT_ABANDON_COMPLETED]: (state: CommentsState, action: any) => {
const { comment_id } = action.data;
2020-01-13 16:52:23 -05:00
const commentById = Object.assign({}, state.commentById);
const byId = Object.assign({}, state.byId);
// to remove the comment and its references
const claimId = commentById[comment_id].claim_id;
for (let i = 0; i < byId[claimId].length; i++) {
if (byId[claimId][i] === comment_id) {
byId[claimId].splice(i, 1);
break;
}
2020-01-13 16:52:23 -05:00
}
delete commentById[comment_id];
2020-01-13 16:52:23 -05:00
return {
...state,
commentById,
byId,
isLoading: false,
};
},
// do nothing
[ACTIONS.COMMENT_ABANDON_FAILED]: (state: CommentsState, action: any) => ({
...state,
isLoading: false,
}),
2020-01-13 16:52:23 -05:00
// do nothing
[ACTIONS.COMMENT_UPDATE_STARTED]: (state: CommentsState, action: any) => ({
...state,
isLoading: true,
}),
2020-01-13 16:52:23 -05:00
// replace existing comment with comment returned here under its comment_id
[ACTIONS.COMMENT_UPDATE_COMPLETED]: (state: CommentsState, action: any) => {
const { comment } = action.data;
const commentById = Object.assign({}, state.commentById);
if (comment) {
commentById[comment.comment_id] = comment;
}
return {
...state,
commentById,
isLoading: false,
};
},
// nothing can be done here
[ACTIONS.COMMENT_UPDATE_FAILED]: (state: CommentsState, action: any) => ({
...state,
isLoading: false,
}),
2020-01-13 16:52:23 -05:00
// nothing can really be done here
[ACTIONS.COMMENT_HIDE_STARTED]: (state: CommentsState, action: any) => ({
...state,
isLoading: true,
}),
[ACTIONS.COMMENT_HIDE_COMPLETED]: (state: CommentsState, action: any) => ({
2020-01-13 16:52:23 -05:00
...state, // todo: add HiddenComments state & create selectors
isLoading: false,
}),
2020-01-13 16:52:23 -05:00
// nothing can be done here
[ACTIONS.COMMENT_HIDE_FAILED]: (state: CommentsState, action: any) => ({
...state,
isLoading: false,
}),
2019-06-26 19:13:29 -04:00
},
defaultState
);