Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Created May 22, 2018 07:46
Show Gist options
  • Save kettanaito/59e42de9a51395a8a53b062269ee9e37 to your computer and use it in GitHub Desktop.
Save kettanaito/59e42de9a51395a8a53b062269ee9e37 to your computer and use it in GitHub Desktop.
Redux - Selector
import { Record } from 'immutable'
const CartInitialState = new Record({
items: [],
})
const initialState = new CartInitialState()
export default initialState
import initialState from './initial-state'
import { ADD_ITEM, REMOVE_ITEM } from './action-types'
export default function cart(state = initialState, action) {
switch (action.type) {
case ADD_ITEM: {
const { item } = action
const nextItems = state.update('items', items => items.push(item))
return state.set('items', nextItems)
}
case REMOVE_ITEM: {
const { itemId } = action
const nextItems = state.get('items').filter(item => item.get('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