import { Map } from 'immutable';
import { FILTER_MARKETS, UPDATE_MARKETS, SERVER_DATA_ACTIVE_SYMBOLS } from '../_constants/ActionTypes';

const initialState = new Map({
    active: [],
    query: '',
    shownMarkets: [],
});

export default (state = initialState, action) => {
    const doFilter = (markets = state.allMarkets, query = state.query) => {
        const queryLc = query.toLowerCase();
        return markets.filter(m =>
            queryLc === '' ||
            m.id.toLowerCase().includes(queryLc) ||
            m.name.toLowerCase().includes(queryLc)
        );
    };

    switch (action.type) {
        case SERVER_DATA_ACTIVE_SYMBOLS: {
            const data = action.serverResponse.data;
            return state.set('active', data);
        }
        case FILTER_MARKETS:
            return {
                ...state,
                shownMarkets: doFilter(state.markets, action.query),
                query: action.query,
            };
        case UPDATE_MARKETS:
            return {
                ...state,
                shownMarkets: doFilter(state.markets, action.query),
                query: action.query,
            };
        default:
            return state;
    }
};

@@ -178,16 +178,30 @@
 ve: %5B%5D,%0A
+    tree: %7B%7D,%0A
     quer
@@ -207,16 +207,16 @@
 ry: '',%0A
-
     show
@@ -234,16 +234,449 @@
 %5D,%0A%7D);%0A%0A
+const generateTree = (symbols) =%3E %7B%0A    const tree = %7B%7D;%0A    symbols.forEach((sym) =%3E %7B%0A        if (!tree%5Bsym.market_display_name%5D) tree%5Bsym.market_display_name%5D = %7B%7D;%0A        if (!tree%5Bsym.market_display_name%5D%5Bsym.submarket_display_name%5D) tree%5Bsym.market_display_name%5D%5Bsym.submarket_display_name%5D = %7B%7D;%0A        tree%5Bsym.market_display_name%5D%5Bsym.submarket_display_name%5D%5Bsym%5D = sym.display_name;%0A    %7D);%0A    return new Map(tree);%0A%7D;%0A%0A
 export d
@@ -1114,20 +1114,29 @@
   const 
-data
+activeSymbols
  = actio
@@ -1186,27 +1186,111 @@
 tate
-.set('active', data
+%0A                .set('active', activeSymbols)%0A                .set('tree', generateTree(activeSymbols)
 );%0A 

Add market tree built from active symbols