[Refactoring][Breaking Changes]
- Move `reducers` and `actions` folders to `store` folder. - Move pages container components to `containers` folder.
This commit is contained in:
13
src/store/reducers/posts/IPostAction.ts
Normal file
13
src/store/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
|
||||
|
||||
}
|
||||
38
src/store/reducers/posts/PostState.ts
Normal file
38
src/store/reducers/posts/PostState.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Post } from 'src/core/domain/posts'
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Stream data storage
|
||||
*/
|
||||
stream?: {hasMoreData: boolean, lastPageRequest: number, lastPostId: string} =
|
||||
{hasMoreData: true, lastPageRequest: -1, lastPostId: ''}
|
||||
|
||||
/**
|
||||
* Profile posts data storage
|
||||
*/
|
||||
profile?: {[userId: string]: {hasMoreData: boolean, lastPageRequest: number, lastPostId: string}} =
|
||||
{}
|
||||
}
|
||||
7
src/store/reducers/posts/index.ts
Normal file
7
src/store/reducers/posts/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { postReducer } from './postReducer'
|
||||
import { postSelector } from './postSelector'
|
||||
|
||||
export {
|
||||
postReducer,
|
||||
postSelector
|
||||
}
|
||||
179
src/store/reducers/posts/postReducer.ts
Normal file
179
src/store/reducers/posts/postReducer.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
// - Import react components
|
||||
import moment from 'moment/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'
|
||||
import { Post } from 'src/core/domain/posts/post'
|
||||
import CommonAPI from 'src/api/CommonAPI'
|
||||
|
||||
/**
|
||||
* Post reducer
|
||||
* @param {object} state
|
||||
* @param {object} action
|
||||
*/
|
||||
export let 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:
|
||||
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.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:
|
||||
const newUserPosts = payload.userPosts as { [userId: string]: { [postId: string]: Post } }
|
||||
const mergedObject = _.merge(state.userPosts, newUserPosts)
|
||||
return {
|
||||
...state,
|
||||
userPosts: {
|
||||
...mergedObject
|
||||
},
|
||||
loaded: true
|
||||
|
||||
}
|
||||
case PostActionType.HAS_MORE_DATA_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
hasMoreData: true
|
||||
}
|
||||
|
||||
}
|
||||
case PostActionType.NOT_MORE_DATA_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
hasMoreData: false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case PostActionType.REQUEST_PAGE_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
lastPageRequest: payload.page
|
||||
}
|
||||
}
|
||||
|
||||
case PostActionType.LAST_POST_STREAM:
|
||||
return {
|
||||
...state,
|
||||
stream: {
|
||||
...state.stream,
|
||||
lastPostId: payload.lastPostId
|
||||
}
|
||||
}
|
||||
case PostActionType.HAS_MORE_DATA_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
hasMoreData: true
|
||||
}
|
||||
|
||||
}
|
||||
case PostActionType.NOT_MORE_DATA_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
[payload.userId]: {
|
||||
...state.profile![payload.userId],
|
||||
hasMoreData: false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case PostActionType.REQUEST_PAGE_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
profile: {
|
||||
...state.profile,
|
||||
[payload.userId]: {
|
||||
...state.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
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return state
|
||||
|
||||
}
|
||||
}
|
||||
9
src/store/reducers/posts/postSelector.ts
Normal file
9
src/store/reducers/posts/postSelector.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
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
|
||||
}
|
||||
|
||||
export const postSelector = {
|
||||
getPost
|
||||
}
|
||||
Reference in New Issue
Block a user