Migrate actions,reducers and action types to TS #15
This commit is contained in:
13
app/reducers/posts/IPostAction.ts
Normal file
13
app/reducers/posts/IPostAction.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { PostActionType } from "constants/postActionType";
|
||||
|
||||
/**
|
||||
* Post action interface
|
||||
*
|
||||
* @export
|
||||
* @interface IPostAction
|
||||
*/
|
||||
export interface IPostAction {
|
||||
payload: any,
|
||||
type: PostActionType
|
||||
|
||||
}
|
||||
25
app/reducers/posts/PostState.ts
Normal file
25
app/reducers/posts/PostState.ts
Normal 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;
|
||||
}
|
||||
3
app/reducers/posts/index.ts
Normal file
3
app/reducers/posts/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { postReducer } from "./postReducer";
|
||||
|
||||
export {postReducer};
|
||||
99
app/reducers/posts/postReducer.ts
Normal file
99
app/reducers/posts/postReducer.ts
Normal 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;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user