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,13 @@
import { PostActionType } from "constants/postActionType";
/**
* Post action interface
*
* @export
* @interface IPostAction
*/
export interface IPostAction {
payload: any,
type: PostActionType
}

View File

@@ -0,0 +1,25 @@
/**
* Post state
*
* @export
* @class PostState
*/
export class PostState {
/**
* The list of user posts
*
* @type {*}
* @memberof PostState
*/
userPosts: any = {};
/**
* If user posts are loaded {true} or not {false}
*
* @type {Boolean}
* @memberof PostState
*/
loaded: Boolean = false;
}

View File

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

View File

@@ -0,0 +1,99 @@
// - Import react components
import moment from 'moment'
import _ from 'lodash'
import {Reducer, Action} from "redux";
// - Import action types
import { PostActionType } from "constants/postActionType";
import { PostState } from "./PostState";
import { IPostAction } from "./IPostAction";
/**
* Post reducer
* @param {object} state
* @param {object} action
*/
export var postReducer = (state : PostState = new PostState(), action : IPostAction) => {
const { payload } = action;
switch (action.type) {
case PostActionType.CLEAR_ALL_DATA_POST:
return new PostState()
case PostActionType.ADD_IMAGE_POST:
return {
...state,
userPosts: {
...state.userPosts,
[payload.uid]: {
...state.userPosts[payload.uid],
[payload.post.id]: { ...payload.post }
}
}
}
case PostActionType.ADD_POST:
return {
...state,
userPosts: {
...state.userPosts,
[payload.uid]: {
...state.userPosts[payload.uid],
[payload.post.id]: { ...payload.post }
}
}
}
case PostActionType.UPDATE_POST:
return {
...state,
userPosts: {
...state.userPosts,
[payload.uid]: {
...state.userPosts[payload.uid],
[payload.post.id]: {
...state.userPosts[payload.uid][payload.post.id],
...payload.post
}
}
}
}
case PostActionType.DELETE_POST:
let filteredPosts = {}
Object.keys(state.userPosts[payload.uid]).map((key) => {
if (key !== payload.id) {
return _.merge(filteredPosts, { [key]: { ...state.userPosts[payload.uid][key] } })
}
})
return {
...state,
userPosts: {
...state.userPosts,
[payload.uid]: {
...filteredPosts
}
}
}
case PostActionType.ADD_LIST_POST:
return {
...state,
userPosts: {
...state.userPosts,
[payload.uid]: {
...state.userPosts[payload.uid],
...payload.posts
}
},
loaded:true
}
default:
return state;
}
}