[Enhancement] Apply immutable js. (#49)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Post } from 'src/core/domain/posts'
|
||||
import { Map, fromJS, List } from 'immutable'
|
||||
|
||||
/**
|
||||
* Post state
|
||||
@@ -14,7 +15,7 @@ export class PostState {
|
||||
* @type {*}
|
||||
* @memberof PostState
|
||||
*/
|
||||
userPosts: any = {}
|
||||
userPosts = Map({})
|
||||
|
||||
/**
|
||||
* If user posts are loaded {true} or not {false}
|
||||
@@ -27,12 +28,12 @@ export class PostState {
|
||||
/**
|
||||
* Stream data storage
|
||||
*/
|
||||
stream?: {hasMoreData: boolean, lastPageRequest: number, lastPostId: string} =
|
||||
{hasMoreData: true, lastPageRequest: -1, lastPostId: ''}
|
||||
stream?: Map<string,any> =
|
||||
Map({hasMoreData: true, lastPageRequest: -1, lastPostId: ''})
|
||||
|
||||
/**
|
||||
* Profile posts data storage
|
||||
*/
|
||||
profile?: {[userId: string]: {hasMoreData: boolean, lastPageRequest: number, lastPostId: string}} =
|
||||
{}
|
||||
profile?: Map<string, any> =
|
||||
Map({})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import moment from 'moment/moment'
|
||||
import _ from 'lodash'
|
||||
import { Reducer, Action } from 'redux'
|
||||
import { Map } from 'immutable'
|
||||
|
||||
// - Import action types
|
||||
import { PostActionType } from 'constants/postActionType'
|
||||
@@ -11,167 +12,94 @@ import { IPostAction } from './IPostAction'
|
||||
import { Post } from 'src/core/domain/posts/post'
|
||||
import CommonAPI from 'src/api/CommonAPI'
|
||||
|
||||
const updatePost = (state: any, payload: any) => {
|
||||
const post: Map<string, any> = payload.post
|
||||
const updatePostOwnerId = post.get('ownerUserId')
|
||||
const updatePostId = post.get('id')
|
||||
return state
|
||||
.setIn(['userPosts', updatePostOwnerId, updatePostId], Map(post))
|
||||
}
|
||||
|
||||
const updatePostComments = (state: any, payload: any) => {
|
||||
const post: Map<string, any> = payload.post
|
||||
const updatePostOwnerId = post.get('ownerUserId')
|
||||
const updatePostId = post.get('id')
|
||||
return state
|
||||
.setIn(['userPosts', updatePostOwnerId, updatePostId, 'comments'], post.get('comments'))
|
||||
}
|
||||
|
||||
const updatePostVotes = (state: any, payload: any) => {
|
||||
const post: Map<string, any> = payload.post
|
||||
const updatePostOwnerId = post.get('ownerUserId')
|
||||
const updatePostId = post.get('id')
|
||||
return state
|
||||
.setIn(['userPosts', updatePostOwnerId, updatePostId, 'votes'], post.get('votes'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Post reducer
|
||||
* @param {object} state
|
||||
* @param {object} action
|
||||
*/
|
||||
export let postReducer = (state: PostState = new PostState(), action: IPostAction) => {
|
||||
export let postReducer = (state = Map(new PostState()), action: IPostAction) => {
|
||||
const { payload } = action
|
||||
switch (action.type) {
|
||||
case PostActionType.CLEAR_ALL_DATA_POST:
|
||||
return new PostState()
|
||||
return Map(new PostState())
|
||||
|
||||
case PostActionType.ADD_IMAGE_POST:
|
||||
return {
|
||||
...state,
|
||||
userPosts: {
|
||||
...state.userPosts,
|
||||
[payload.uid]: {
|
||||
...state.userPosts[payload.uid],
|
||||
[payload.post.id]: { ...payload.post }
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['userPosts', payload.uid, payload.post.id], Map(payload.post))
|
||||
|
||||
case PostActionType.ADD_POST:
|
||||
return {
|
||||
...state,
|
||||
userPosts: {
|
||||
...state.userPosts,
|
||||
[payload.uid]: {
|
||||
...state.userPosts[payload.uid],
|
||||
[payload.post.id]: { ...payload.post }
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['userPosts', payload.uid, payload.post.id], Map(payload.post))
|
||||
|
||||
case PostActionType.UPDATE_POST:
|
||||
const post: Post = payload.post
|
||||
return {
|
||||
...state,
|
||||
userPosts: {
|
||||
...state.userPosts,
|
||||
[post.ownerUserId!]: {
|
||||
...state.userPosts[post.ownerUserId!],
|
||||
[payload.post.id]: {
|
||||
...payload.post,
|
||||
comments: post.comments,
|
||||
votes: post.votes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case PostActionType.UPDATE_POST: return updatePost(state, payload)
|
||||
case PostActionType.UPDATE_POST_COMMENTS: return updatePostComments(state, payload)
|
||||
case PostActionType.UPDATE_POST_VOTES: return updatePostVotes(state, payload)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
.deleteIn(['userPosts', payload.uid, payload.id])
|
||||
|
||||
case PostActionType.ADD_LIST_POST:
|
||||
const newUserPosts = payload.userPosts as { [userId: string]: { [postId: string]: Post } }
|
||||
const mergedObject = _.merge(state.userPosts, newUserPosts)
|
||||
return {
|
||||
...state,
|
||||
userPosts: {
|
||||
...mergedObject
|
||||
},
|
||||
loaded: true
|
||||
return state
|
||||
.mergeDeepIn(['userPosts'], payload.userPosts)
|
||||
.set('loaded', true)
|
||||
|
||||
}
|
||||
case PostActionType.HAS_MORE_DATA_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
hasMoreData: true
|
||||
}
|
||||
return state
|
||||
.setIn(['stream', 'hasMoreData'], true)
|
||||
|
||||
}
|
||||
case PostActionType.NOT_MORE_DATA_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
hasMoreData: false
|
||||
}
|
||||
|
||||
}
|
||||
return state
|
||||
.setIn(['stream', 'hasMoreData'], false)
|
||||
|
||||
case PostActionType.REQUEST_PAGE_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
lastPageRequest: payload.page
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['stream', 'lastPageRequest'], payload.page)
|
||||
|
||||
case PostActionType.LAST_POST_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
lastPostId: payload.lastPostId
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['stream', 'lastPostId'], payload.lastPostId)
|
||||
|
||||
case PostActionType.HAS_MORE_DATA_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
hasMoreData: true
|
||||
}
|
||||
return state
|
||||
.setIn(['profile', 'hasMoreData'], true)
|
||||
|
||||
}
|
||||
case PostActionType.NOT_MORE_DATA_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
[payload.userId]: {
|
||||
...state.profile![payload.userId],
|
||||
hasMoreData: false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return state
|
||||
.setIn(['profile', payload.userId, 'hasMoreData'], false)
|
||||
|
||||
case PostActionType.REQUEST_PAGE_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
[payload.userId]: {
|
||||
...state.profile![payload.userId],
|
||||
lastPageRequest: payload.page
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['profile', payload.userId, 'lastPageRequest'], payload.page)
|
||||
|
||||
case PostActionType.LAST_POST_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
[payload.userId]: {
|
||||
...state.profile![payload.userId],
|
||||
lastPostId: payload.lastPostId
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
.setIn(['profile', payload.userId, 'lastPostId'], payload.lastPostId)
|
||||
|
||||
default:
|
||||
return state
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const getPost = (state: any, userId: string, postId: string) => {
|
||||
return (state.post.userPosts && state.post.userPosts[userId] && state.post.userPosts[userId][postId])
|
||||
? state.post.userPosts[userId][postId]
|
||||
: null
|
||||
import {Map} from 'immutable'
|
||||
|
||||
const getPost = (state: Map<string, any>, userId: string, postId: string) => {
|
||||
return state.getIn(['post', 'userPosts', userId, postId])
|
||||
}
|
||||
|
||||
export const postSelector = {
|
||||
|
||||
Reference in New Issue
Block a user