Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Last active July 14, 2017 14:21
Show Gist options
  • Save notgiorgi/be172157b41824fe8f5dd64aa76e3503 to your computer and use it in GitHub Desktop.
Save notgiorgi/be172157b41824fe8f5dd64aa76e3503 to your computer and use it in GitHub Desktop.
function reducer(state = [], action) {
  switch (action.type) {
    /* ... */
    case 'FETCH_POSTS_SUCCESS':
      return [...state, ...action.payload]
    /* ... */
    default:
      return state 
  }
}
// Somewhere in the component
<PostList>
  {posts.map(post => <Post {...post} />}    
</PostList>
function reducer(state = [], action){
  switch(action.type) {
    case 'INCREMENT_POST_LIKES': {
      const { post_id } = action.payload
      const index = state.findIndex(post => post.id = post_id)
      if (index) {
        const oldPost = state[index]
        const post = {
          ...oldPost,
          likes: oldPost.likes + 1
        }
      }
    }
    default:
      return state
  }
}
function reducer(state = {}, action) {
  switch (action.type) {
    /* ... */
    case 'FETCH_POSTS_SUCCESS': {
      const posts = action.payload
          .reduce(
            (acc, x) => Object.assign(acc, { [x.id]: x }),
            {}
          )

      return {
        ...state,
        ...posts
      }
    }
    /* ... */
    default:
      return state 
  }
}
[
  { id: 3, title: 'foo', likes: 5 },
  { id: 7, title: 'bar', likes: 2 },
  { id: 8, title: 'Foo Bar', likes: 12 }
]
// Becomes
{
  3: { id: 3, title: 'foo', likes: 5 },
  7: { id: 7, title: 'bar', likes: 2 },
  8: { id: 8, title: 'Foo Bar', likes: 12 }
}
function reducer(state = {}, action) {
  switch (action.type) {
    /* ... */
    case 'INCREMENT_POST_LIKES': {
      const { post_id } = action.payload
      const oldPost = state[post_id]
      return {
        ...state,
        [post_id]: {
          ...oldPost,
          likes: oldPost.likes + 1
        }
      }
    }
    case 'UPDATE_POST_TEXT': {
      const { post_id, text } = action.payload
      const oldPost = state[post_id]
      
      return {
        ...state,
        [post_id]: {
          ...oldPost,
          text
        }
      }
    }
    /* ... */
  }
}
function postReducer(state = {}, action) {
  switch(action.type) {
    case 'FETCH_POSTS_SUCCESS':
      return action.payload
    case 'INCREMENT_POST_LIKES':
      return {
        ...state,
        likes: state.likes + 1
      }
     case 'UPDATE_POST_TEXT':
      return {
        ...state,
        text: action.payload.text
      }
  }
}

function postsReducer(state = {}, action) {
  switch (action.type) {
    case 'FETCH_POSTS_SUCCESS':
      const posts = action.payload.posts
        .reduce(
          (acc, x) => ({
            ...acc,
            [x.id]: postReducer(
              undefined,
              { type: 'FETCH_POSTS_SUCCESS', payload: x }
            )
          }),
          {}
        )

       return {
        ...state,
        ...posts,
      }
    case 'DELETE_POST':
      return {
        ...state,
        [action.payload.post_id]: undefined
      }
  }
}
function postsReducer(state = {}, action) {
  switch (action.type) {
    case 'FETCH_POSTS_SUCCESS':
      const createPostReducer = post =>
        postReducer(undefined, { type: 'FETCH_POSTS_SUCCESS', payload: post })
      
      const posts = normalize(
        action.payload.posts
          .map(createPostReducer),
        'id'
      )

       return {
        ...state,
        ...posts,
      }
    case 'DELETE_POST':
      return {
        ...state,
        [action.payload.post_id]: undefined
      }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment