Add comment replies toggle and body to state

This commit is contained in:
riggraz
2019-09-17 19:09:38 +02:00
parent 0c0c6d4e30
commit ecfdc54100
8 changed files with 191 additions and 4 deletions

View File

@@ -4,20 +4,31 @@ import {
COMMENTS_REQUEST_SUCCESS,
COMMENTS_REQUEST_FAILURE,
} from '../actions/requestComments';
import { commentRequestSuccess } from '../actions/requestComment';
import {
HandleCommentRepliesType,
TOGGLE_COMMENT_REPLY,
SET_COMMENT_REPLY_BODY,
} from '../actions/handleCommentReplies';
import commentReducer from './commentReducer';
import commentRepliesReducer from './commentRepliesReducer';
import IComment from '../interfaces/IComment';
import { CommentRepliesState } from './commentRepliesReducer';
export interface CommentsState {
items: Array<IComment>;
replies: Array<CommentRepliesState>;
areLoading: boolean;
error: string;
}
const initialState: CommentsState = {
items: [],
replies: [],
areLoading: false,
error: '',
};
@@ -39,6 +50,9 @@ const commentsReducer = (
items: action.comments.map(
comment => commentReducer(undefined, commentRequestSuccess(comment))
),
replies: action.comments.map(
comment => commentRepliesReducer(undefined, commentRequestSuccess(comment))
),
areLoading: false,
error: '',
};
@@ -50,6 +64,20 @@ const commentsReducer = (
error: action.error,
};
case TOGGLE_COMMENT_REPLY:
case SET_COMMENT_REPLY_BODY:
return {
...state,
replies: state.replies.map(
reply => (
reply.commentId === action.commentId ?
commentRepliesReducer(reply, action)
:
reply
)
),
};
default:
return state;
}