Files
astuto/app/javascript/reducers/replyFormReducer.ts

88 lines
1.6 KiB
TypeScript
Raw Normal View History

import {
2019-09-26 16:03:41 +02:00
CommentRequestSuccessAction,
COMMENT_REQUEST_SUCCESS,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/requestComment';
2019-09-18 13:40:00 +02:00
import {
2019-09-26 16:03:41 +02:00
HandleCommentRepliesType,
TOGGLE_COMMENT_REPLY,
SET_COMMENT_REPLY_BODY,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/handleCommentReplies';
2019-09-18 13:40:00 +02:00
import {
2019-09-26 16:03:41 +02:00
CommentSubmitActionTypes,
2019-09-18 13:40:00 +02:00
COMMENT_SUBMIT_START,
COMMENT_SUBMIT_SUCCESS,
COMMENT_SUBMIT_FAILURE,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/submitComment';
2019-09-18 13:40:00 +02:00
export interface ReplyFormState {
commentId: number;
isOpen: boolean;
body: string;
2019-09-18 13:40:00 +02:00
isSubmitting: boolean;
error: string;
}
const initialState: ReplyFormState = {
commentId: undefined,
isOpen: false,
body: '',
2019-09-18 13:40:00 +02:00
isSubmitting: false,
error: '',
}
const replyFormReducer = (
state = initialState,
2019-09-26 16:03:41 +02:00
action:
CommentRequestSuccessAction |
HandleCommentRepliesType |
CommentSubmitActionTypes,
) => {
switch (action.type) {
case COMMENT_REQUEST_SUCCESS:
return {
...initialState,
commentId: action.comment.id,
};
case TOGGLE_COMMENT_REPLY:
return {
...state,
isOpen: !state.isOpen,
};
case SET_COMMENT_REPLY_BODY:
return {
...state,
body: action.body,
};
2019-09-18 13:40:00 +02:00
case COMMENT_SUBMIT_START:
return {
...state,
isSubmitting: true,
};
case COMMENT_SUBMIT_SUCCESS:
return {
...state,
isOpen: false,
body: '',
isSubmitting: false,
error: '',
};
case COMMENT_SUBMIT_FAILURE:
return {
...state,
error: action.error,
isSubmitting: false,
2019-09-18 13:40:00 +02:00
};
default:
return state;
}
}
export default replyFormReducer;