You must study this commit for the sake of Mark-san
This commit is contained in:
parent
49e4015c17
commit
dc33eed88b
2 changed files with 17 additions and 3 deletions
|
@ -9,7 +9,10 @@ class PostList extends React.Component {
|
||||||
this.props.fetchPosts();
|
this.props.fetchPosts();
|
||||||
}
|
}
|
||||||
renderList() {
|
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 (
|
return (
|
||||||
<div className="item" key={post.id}>
|
<div className="item" key={post.id}>
|
||||||
<div className="content">
|
<div className="content">
|
||||||
|
|
|
@ -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) {
|
switch (action.type) {
|
||||||
case 'FETCH_POSTS':
|
case 'FETCH_POSTS':
|
||||||
return action.payload;
|
const mergedEntities = normalizeEntities(state.entities, action.payload)
|
||||||
|
return {...state, ...{entities: mergedEntities}}
|
||||||
case 'FETCH_POST':
|
case 'FETCH_POST':
|
||||||
return action.payload;
|
return action.payload;
|
||||||
default:
|
default:
|
||||||
|
|
Reference in a new issue