Created
August 31, 2018 18:48
-
-
Save whisher/36e51c24e5dcf4c0e6cceb623f84459d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REDUCER | |
case PostsActionTypes.LoadPostsSuccess: { | |
const posts = action.payload.posts; | |
const entities = posts.reduce( | |
(entities: { [id: string]: Post }, post: Post) => { | |
return { | |
...entities, | |
[post._id]: post, | |
}; | |
}, | |
{ | |
...state.entities, | |
} | |
); | |
return { | |
...state, | |
loading: false, | |
loaded: true, | |
entities, | |
ids: Object.keys(entities) | |
}; | |
SELECTOR | |
export const getPostsEntities = (state: State) => { | |
const data = {}; | |
state.ids.forEach(id => { | |
data[id] = state.entities[id]; | |
}); | |
return data; | |
}; | |
PERCHE AL FILTER | |
case PostsActionTypes.FilterPostsByTitle: { | |
const search = action.payload.search; | |
const entities = { ...state.entities}; | |
const ids = Object.keys(entities).filter(id => { | |
return entities[id].title.indexOf(search) !== -1; | |
}); | |
return { | |
...state, | |
ids, | |
error: false, | |
loading: false | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment