Migrate actions,reducers and action types to TS #15

This commit is contained in:
Qolzam
2017-10-10 08:01:25 +07:00
parent 3b3899e7af
commit f9d213f741
113 changed files with 2692 additions and 1275 deletions

View File

@@ -0,0 +1,27 @@
import { Comment } from "domain/comments";
/**
* Comment state
*
* @export
* @class CommentState
*/
export class CommentState {
/**
* The list of comments on the posts
*
* @type {({[postId: string]: {[commentId: string]: Comment}} | null)}
* @memberof CommentState
*/
postComments: {[postId: string]: {[commentId: string]: Comment}} = {};
/**
* If the comments are loaded {true} or not {false}
*
* @type {Boolean}
* @memberof CommentState
*/
loaded: Boolean = false;
}

View File

@@ -0,0 +1,14 @@
import {CommentActionType} from 'constants/commentActionType'
/**
* Comment action interface
*
* @export
* @interface ICommentAction
*/
export interface ICommentAction {
payload: any;
type: CommentActionType;
}

View File

@@ -0,0 +1,123 @@
// - Import react components
import moment from 'moment'
import _ from 'lodash'
// - Import domain
import { User } from "domain/users";
import { Comment } from "domain/comments";
// - Import action types
import {CommentActionType} from 'constants/commentActionType'
import { CommentState } from "./CommentState";
import { ICommentAction } from "./ICommentAction";
/**
* Comment reducer
* @param state
* @param action
*/
export var commentReducer = (state: CommentState = new CommentState(), action: ICommentAction) => {
var { payload } = action
switch (action.type) {
/* _____________ CRUD _____________ */
case CommentActionType.ADD_COMMENT:
return {
...state,
postComments: {
...state.postComments,
[payload.postId]: {
...state.postComments![payload.postId],
[payload.id]: {
...payload.comment,
editorStatus: false
}
}
}
}
case CommentActionType.ADD_COMMENT_LIST:
return {
...state,
postComments: {
...payload
},
loaded:true
}
case CommentActionType.UPDATE_COMMENT:
return {
...state,
postComments: {
...state.postComments,
[payload.postId]: {
...state.postComments![payload.postId],
[payload.id]: {
...state.postComments![payload.postId][payload.id],
text: payload.text,
editorStatus: false
}
}
}
}
case CommentActionType.DELETE_COMMENT:
var parsedComments = {}
if (!state.postComments![payload.postId]) {
return state
}
Object.keys(state.postComments![payload.postId]).map((id) => {
if (id !== payload.id) {
_.merge(parsedComments, { [id]: { ...state.postComments![payload.postId][id] } })
}
})
return {
...state,
postComments: {
...state.postComments,
[payload.postId]: {
...parsedComments
}
}
}
case CommentActionType.CLOSE_COMMENT_EDITOR:
return {
...state,
postComments: {
...state.postComments,
[payload.postId]: {
...state.postComments![payload.postId],
[payload.id]: {
...state.postComments![payload.postId][payload.id],
editorStatus: false
}
}
}
}
case CommentActionType.OPEN_COMMENT_EDITOR:
return {
...state,
postComments: {
...state.postComments,
[payload.postId]: {
...state.postComments![payload.postId],
[payload.id]: {
...state.postComments![payload.postId][payload.id],
editorStatus: true
}
}
}
}
case CommentActionType.CLEAR_ALL_DATA_COMMENT:
return new CommentState();
default:
return state;
}
}

View File

@@ -0,0 +1,3 @@
import { commentReducer } from "./commentReducer";
export {commentReducer};