Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Last active June 5, 2018 11:01
Show Gist options
  • Save kettanaito/2dd13fca87968273b54613fd4b2281d7 to your computer and use it in GitHub Desktop.
Save kettanaito/2dd13fca87968273b54613fd4b2281d7 to your computer and use it in GitHub Desktop.
Redux - Immutability
import { Record, List } from 'immutable'
const CartInitialState = new Record({
items: List()
})
const initialState = CartInitialState({
items: List()
})
export default initialState
import initialState from './initial-state'
export default function cart(state = initialState, action) {
switch (action.type) {
case 'ADD_ITEM': {
const { item } = action
const nextItems = state.update('items', items => items.concat(item))
return state.set('items', nextItems)
}
case 'REMOVE_ITEM': {
const { itemId } = action
const nextItems = state.get('items').filterNot(item => item.id === itemId)
return state.set('items', nextItems)
}
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment