Migrate actions,reducers and action types to TS #15
This commit is contained in:
27
app/reducers/comments/CommentState.ts
Normal file
27
app/reducers/comments/CommentState.ts
Normal 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;
|
||||
}
|
||||
14
app/reducers/comments/ICommentAction.ts
Normal file
14
app/reducers/comments/ICommentAction.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
import {CommentActionType} from 'constants/commentActionType'
|
||||
|
||||
/**
|
||||
* Comment action interface
|
||||
*
|
||||
* @export
|
||||
* @interface ICommentAction
|
||||
*/
|
||||
export interface ICommentAction {
|
||||
payload: any;
|
||||
type: CommentActionType;
|
||||
|
||||
}
|
||||
123
app/reducers/comments/commentReducer.ts
Normal file
123
app/reducers/comments/commentReducer.ts
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
3
app/reducers/comments/index.ts
Normal file
3
app/reducers/comments/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { commentReducer } from "./commentReducer";
|
||||
|
||||
export {commentReducer};
|
||||
Reference in New Issue
Block a user