mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
121 lines
2.9 KiB
TypeScript
121 lines
2.9 KiB
TypeScript
import {
|
|
CommentsRequestActionTypes,
|
|
COMMENTS_REQUEST_START,
|
|
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 {
|
|
CommentSubmitActionTypes,
|
|
COMMENT_SUBMIT_START,
|
|
COMMENT_SUBMIT_SUCCESS,
|
|
COMMENT_SUBMIT_FAILURE,
|
|
} from '../actions/submitComment';
|
|
|
|
import {
|
|
ToggleIsUpdateSuccessAction,
|
|
TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
|
|
} from '../actions/updateComment';
|
|
|
|
import commentReducer from './commentReducer';
|
|
import replyFormsReducer from './replyFormsReducer';
|
|
|
|
import { ReplyFormState } from './replyFormReducer';
|
|
|
|
import IComment from '../interfaces/IComment';
|
|
import ICommentJSON from '../interfaces/json/IComment';
|
|
|
|
export interface CommentsState {
|
|
items: Array<IComment>;
|
|
replyForms: Array<ReplyFormState>;
|
|
areLoading: boolean;
|
|
error: string;
|
|
}
|
|
|
|
const initialState: CommentsState = {
|
|
items: [],
|
|
replyForms: [],
|
|
areLoading: false,
|
|
error: '',
|
|
};
|
|
|
|
const commentsReducer = (
|
|
state = initialState,
|
|
action:
|
|
CommentsRequestActionTypes |
|
|
HandleCommentRepliesType |
|
|
CommentSubmitActionTypes |
|
|
ToggleIsUpdateSuccessAction
|
|
): CommentsState => {
|
|
switch (action.type) {
|
|
case COMMENTS_REQUEST_START:
|
|
return {
|
|
...state,
|
|
areLoading: true,
|
|
};
|
|
|
|
case COMMENTS_REQUEST_SUCCESS:
|
|
return {
|
|
...state,
|
|
items: action.comments.map(
|
|
(comment: ICommentJSON) => commentReducer(undefined, commentRequestSuccess(comment))
|
|
),
|
|
replyForms: replyFormsReducer(state.replyForms, action),
|
|
areLoading: false,
|
|
error: '',
|
|
};
|
|
|
|
case COMMENTS_REQUEST_FAILURE:
|
|
return {
|
|
...state,
|
|
areLoading: false,
|
|
error: action.error,
|
|
};
|
|
|
|
case TOGGLE_COMMENT_REPLY:
|
|
case SET_COMMENT_REPLY_BODY:
|
|
return {
|
|
...state,
|
|
replyForms: replyFormsReducer(state.replyForms, action),
|
|
};
|
|
|
|
case COMMENT_SUBMIT_START:
|
|
case COMMENT_SUBMIT_FAILURE:
|
|
return {
|
|
...state,
|
|
replyForms: replyFormsReducer(state.replyForms, action),
|
|
};
|
|
|
|
case COMMENT_SUBMIT_SUCCESS:
|
|
return {
|
|
...state,
|
|
items: [commentReducer(undefined, commentRequestSuccess(action.comment)), ...state.items],
|
|
replyForms: replyFormsReducer(state.replyForms, action),
|
|
};
|
|
|
|
case TOGGLE_COMMENT_IS_UPDATE_SUCCESS:
|
|
return {
|
|
...state,
|
|
items:
|
|
state.items.map(comment => {
|
|
if (comment.id === action.commentId) {
|
|
comment.isPostUpdate = !comment.isPostUpdate;
|
|
return comment;
|
|
} else return comment;
|
|
})
|
|
}
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
export default commentsReducer; |