Home Reference Source

application/redux/reducers/global-history-reducers.js

import Immutable from 'immutable';
import {
    GLOBAL_HISTORY_PUSH,
    GLOBAL_HISTORY_POP
} from './../actions/global-history-actions';

export function globalHistory(state = null, action) {
    if (state === null) {
        state = {
            items: Immutable.fromJS([]).toList(),
            selected: null
        };
    }

    switch (action.type) {
        case GLOBAL_HISTORY_POP:
            if (!action.payload) {
                return Object.assign({}, state, {
                    items: state.items.pop()
                });
            }

            return Object.assign({}, state, {
                items: state.items.filter((item) => item.get('uuid') != action.payload)
            });

        case GLOBAL_HISTORY_PUSH:
            // remove it from the stack wherever it may be and push it back onto the top
            return Object.assign({}, state, {
                items: state.items
                    .filter((item) => item.get('uuid') != action.payload.uuid)
                    .unshift(Immutable.fromJS(action.payload))
            });
    }

    return state;
};