Home Reference Source

application/redux/reducers/content-view-reducers.js

import _ from 'lodash';
import {
    CONTENT_VIEW_UPDATE,
    CONTENT_VIEW_RESET,
    CONTENT_VIEW_NOTIFY_NEW,
    CONTENT_VIEW_CLEAR_NEW
} from './../actions/content-view-actions';

const baseState = {
    filtersOn: false,
    hasNew: false,
    page: 1,
    type: null,
    keywords: null,
    workflow: null,
    status: null,
    sorter: 'modified_at',
    flip: false,
    grid: false,
    mediatype: null
};

export function contentView(state = null, action) {
    if (state === null) {
        state = baseState;
    }

    switch (action.type) {
        case CONTENT_VIEW_UPDATE:
            let newState = Object.assign({}, state, action.payload);
            let testState = Object.assign({}, state, action.payload, {
                filtersOn: false,
                hasNew: false,
                page: 1,
                type: null,
                grid: false
            });

            if (!_.isEqual(testState, baseState)) {
                newState.filtersOn = true;
            } else {
                newState.filtersOn = false;
            }
            return newState;
        case CONTENT_VIEW_RESET:
            if (state.type) {
                return Object.assign({}, baseState, {type: state.type});
            }

            return Object.assign({}, baseState);
        case CONTENT_VIEW_NOTIFY_NEW:
            return Object.assign({}, state, {hasNew: true});
        case CONTENT_VIEW_CLEAR_NEW:
            return Object.assign({}, state, {hasNew: false});
    }

    return state;
};