1
0
Fork 0

You must study this commit for the sake of Mark-san

This commit is contained in:
Amarpreet Minhas 2019-07-26 17:07:17 -04:00
parent 49e4015c17
commit dc33eed88b
2 changed files with 17 additions and 3 deletions

View file

@ -9,7 +9,10 @@ class PostList extends React.Component {
this.props.fetchPosts();
}
renderList() {
return this.props.posts.map(post => {
const {posts} = this.props
const postKeys = Object.keys(posts.entities)
return postKeys.map(id => {
const post = posts.entities[id]
return (
<div className="item" key={post.id}>
<div className="content">

View file

@ -1,7 +1,18 @@
export default (state = [], action) => {
const initialState = {
entities: {},
};
const normalizeEntities = (entities, payloadData) => {
const entitiesMap = {}
payloadData.forEach(post => entitiesMap[post.id] = post)
return {...entities, ...entitiesMap}
}
export default (state = initialState, action) => {
switch (action.type) {
case 'FETCH_POSTS':
return action.payload;
const mergedEntities = normalizeEntities(state.entities, action.payload)
return {...state, ...{entities: mergedEntities}}
case 'FETCH_POST':
return action.payload;
default: