All files / src/redux reducers.js

55.56% Statements 10/18
10% Branches 1/10
22.22% Functions 2/9
52.94% Lines 9/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                    1x   1x                                   1x       1x           1x       1x 1x     1x       4x                      
/* * *  *  * *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * */
/* Copyright (c) 2017 Mobify Research & Development Inc. All rights reserved. */
/* * *  *  * *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * */
 
import Immutable from 'immutable'
import {handleActions} from 'redux-actions'
import {
    receiveCartData,
    receiveCartPaymentMethods,
    receiveCartShippingMethods,
    receiveCategoryData,
    receiveCustomerData,
    receiveProductData,
    receiveProductSearchData,
    receiveStoreData
} from './actions'
import {skipListsMerger} from './utils'
 
const initialState = Immutable.Map()
 
const cartReducer = handleActions({
    [receiveCartData]: (state, {payload}) => {
        const replaceState = !state || !payload || state.get('id') !== payload.id
 
        return replaceState ?
            Immutable.fromJS(payload) :
            state.mergeWith(skipListsMerger, payload)
    },
    [receiveCartPaymentMethods]: (state, {payload}) => (state.mergeDeep({
        paymentMethods: payload
    })),
    [receiveCartShippingMethods]: (state, {payload}) => (state.mergeDeep({
        shippingMethods: payload
    }))
}, null)
 
// TODO: This reducer isn't smart enough to reduce child categories into the root
// 'categories' redux branch, we should make it do that.
const categoryReducer = handleActions({
    [receiveCategoryData]: (state, {payload}) => (state.mergeDeep(payload))
}, initialState)
 
const customerReducer = handleActions({
    [receiveCustomerData]: (state, {payload}) => {
        return payload ? (state || initialState).mergeDeep(payload) : null
    }
}, null)
 
const productReducer = handleActions({
    [receiveProductData]: (state, {payload}) => (state.mergeDeep(payload))
}, initialState)
 
const productSearchReducer = handleActions({
    [receiveProductSearchData]: (state, {payload}) => (state.mergeDeep(payload))
}, initialState)
 
const storeReducer = handleActions({
    [receiveStoreData]: (state, {payload}) => (state.mergeDeep(payload))
}, initialState)
 
const simpleReducer = (state = null) => state
 
export default {
    cart: cartReducer,
    categories: categoryReducer,
    customer: customerReducer,
    orders: simpleReducer,
    products: productReducer,
    productSearches: productSearchReducer,
    stores: storeReducer,
}