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

198 lines
5.0 KiB
TypeScript
Raw Normal View History

import {
PostRequestActionTypes,
POST_REQUEST_START,
POST_REQUEST_SUCCESS,
POST_REQUEST_FAILURE,
2022-05-07 11:27:07 +02:00
} from '../actions/Post/requestPost';
2019-09-21 12:54:57 +02:00
import {
ChangePostBoardSuccessAction,
CHANGE_POST_BOARD_SUCCESS,
2022-05-07 11:27:07 +02:00
} from '../actions/Post/changePostBoard';
2019-09-21 12:54:57 +02:00
import {
ChangePostStatusSuccessAction,
CHANGE_POST_STATUS_SUCCESS,
2022-05-07 11:27:07 +02:00
} from '../actions/Post/changePostStatus';
2019-09-30 16:54:37 +02:00
import {
LikesRequestActionTypes,
LIKES_REQUEST_START,
LIKES_REQUEST_SUCCESS,
LIKES_REQUEST_FAILURE,
2022-05-07 11:27:07 +02:00
} from '../actions/Like/requestLikes';
2019-09-30 16:54:37 +02:00
import {
LikeActionTypes,
LIKE_SUBMIT_SUCCESS,
2022-05-07 11:27:07 +02:00
} from '../actions/Like/submitLike';
2019-09-30 16:54:37 +02:00
import {
CommentsRequestActionTypes,
COMMENTS_REQUEST_START,
COMMENTS_REQUEST_SUCCESS,
COMMENTS_REQUEST_FAILURE,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/requestComments';
import {
HandleCommentRepliesType,
TOGGLE_COMMENT_REPLY,
SET_COMMENT_REPLY_BODY,
TOGGLE_COMMENT_IS_POST_UPDATE_FLAG,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/handleCommentReplies';
2019-09-18 13:40:00 +02:00
import {
CommentSubmitActionTypes,
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
import {
ToggleIsUpdateSuccessAction,
TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
2022-05-07 11:27:07 +02:00
} from '../actions/Comment/updateComment';
import { FollowActionTypes, FOLLOW_SUBMIT_SUCCESS } from '../actions/Follow/submitFollow';
import { FollowRequestActionTypes, FOLLOW_REQUEST_SUCCESS } from '../actions/Follow/requestFollow';
import {
PostStatusChangesRequestActionTypes,
POST_STATUS_CHANGES_REQUEST_START,
POST_STATUS_CHANGES_REQUEST_SUCCESS,
POST_STATUS_CHANGES_REQUEST_FAILURE,
} from '../actions/PostStatusChange/requestPostStatusChanges';
import {
PostStatusChangeSubmitted,
POST_STATUS_CHANGE_SUBMITTED
} from '../actions/PostStatusChange/submittedPostStatusChange';
import postReducer from './postReducer';
2019-09-30 16:54:37 +02:00
import likesReducer from './likesReducer';
import commentsReducer from './commentsReducer';
2019-09-30 16:54:37 +02:00
import { LikesState } from './likesReducer';
import { CommentsState } from './commentsReducer';
import postStatusChangesReducer, { PostStatusChangesState } from './postStatusChangesReducer';
import IPost from '../interfaces/IPost';
interface CurrentPostState {
item: IPost;
isLoading: boolean;
error: string;
2019-09-30 16:54:37 +02:00
likes: LikesState;
followed: boolean;
comments: CommentsState;
postStatusChanges: PostStatusChangesState,
}
const initialState: CurrentPostState = {
2019-09-26 16:03:41 +02:00
item: postReducer(undefined, {} as PostRequestActionTypes),
isLoading: false,
error: '',
2019-09-30 16:54:37 +02:00
likes: likesReducer(undefined, {} as LikesRequestActionTypes),
followed: false,
2019-09-26 16:03:41 +02:00
comments: commentsReducer(undefined, {} as CommentsRequestActionTypes),
postStatusChanges: postStatusChangesReducer(undefined, {} as PostStatusChangesRequestActionTypes),
};
const currentPostReducer = (
state = initialState,
action:
PostRequestActionTypes |
2019-09-21 12:54:57 +02:00
ChangePostBoardSuccessAction |
ChangePostStatusSuccessAction |
2019-09-30 16:54:37 +02:00
LikesRequestActionTypes |
LikeActionTypes |
CommentsRequestActionTypes |
2019-09-18 13:40:00 +02:00
HandleCommentRepliesType |
CommentSubmitActionTypes |
ToggleIsUpdateSuccessAction |
FollowActionTypes |
FollowRequestActionTypes |
PostStatusChangesRequestActionTypes |
PostStatusChangeSubmitted
): CurrentPostState => {
switch (action.type) {
case POST_REQUEST_START:
return {
...state,
isLoading: true,
};
case POST_REQUEST_SUCCESS:
return {
...state,
item: postReducer(undefined, action),
isLoading: false,
error: '',
};
case POST_REQUEST_FAILURE:
return {
...state,
isLoading: false,
error: action.error,
};
2019-09-21 12:54:57 +02:00
case CHANGE_POST_BOARD_SUCCESS:
case CHANGE_POST_STATUS_SUCCESS:
return {
...state,
item: postReducer(state.item, action),
};
2019-09-30 16:54:37 +02:00
case LIKES_REQUEST_START:
case LIKES_REQUEST_SUCCESS:
case LIKES_REQUEST_FAILURE:
case LIKE_SUBMIT_SUCCESS:
return {
...state,
likes: likesReducer(state.likes, action),
};
case COMMENTS_REQUEST_START:
case COMMENTS_REQUEST_SUCCESS:
case COMMENTS_REQUEST_FAILURE:
case TOGGLE_COMMENT_REPLY:
case SET_COMMENT_REPLY_BODY:
2019-09-18 13:40:00 +02:00
case COMMENT_SUBMIT_START:
case COMMENT_SUBMIT_SUCCESS:
case COMMENT_SUBMIT_FAILURE:
case TOGGLE_COMMENT_IS_UPDATE_SUCCESS:
case TOGGLE_COMMENT_IS_POST_UPDATE_FLAG:
return {
...state,
comments: commentsReducer(state.comments, action),
};
case FOLLOW_REQUEST_SUCCESS:
return {
...state,
followed: action.follow.user_id ? true : false,
};
case FOLLOW_SUBMIT_SUCCESS:
return {
...state,
followed: action.isFollow,
};
case POST_STATUS_CHANGES_REQUEST_START:
case POST_STATUS_CHANGES_REQUEST_SUCCESS:
case POST_STATUS_CHANGES_REQUEST_FAILURE:
case POST_STATUS_CHANGE_SUBMITTED:
return {
...state,
postStatusChanges: postStatusChangesReducer(state.postStatusChanges, action),
};
default:
return state;
}
}
export default currentPostReducer;