Skip to content

Instantly share code, notes, and snippets.

@whisher
Created August 31, 2018 18:48
Show Gist options
  • Save whisher/36e51c24e5dcf4c0e6cceb623f84459d to your computer and use it in GitHub Desktop.
Save whisher/36e51c24e5dcf4c0e6cceb623f84459d to your computer and use it in GitHub Desktop.
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