summaryrefslogblamecommitdiff
path: root/src/Store/store.ts
blob: 581f14b4961d9e2d511ec46fd8e86c9aefe1cfaf (plain) (tree)
1
2
3
4
5
6





                                                                      






                                                                                                                                                           
                                                                                               






                                                                                     



                                                                                                                                    
                 
 






                                                                                   

                                                                 




































                                                                                               















                                                    
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { createReducer } from 'redux-su';
import { composeWithDevTools } from 'redux-devtools-extension';
import { Portal, Link, Field } from '../Api/types';

const reducers = {
    'auth': createReducer({
        'authSet': (store: any, action: { v: string, csrf: string, user: any }) => ({ v: action.v, csrf: action.csrf, user: action.user, loading: false }),
        'authLoad': (store: any, action: { loading: boolean }) => ({ ...store, loading: action.loading })
    }, { v: "", csrf: "", user: null, loading: false }),
    'entities': createReducer({
        'portalSet': (store: any, action: { guid: string, portal: Portal }) =>
            ({ ...store, portals: { ...store.portals, [action.guid]: { ...action.portal } } }),
        'linkSet': (store: any, action: { guid: string, link: Link }) =>
            ({ ...store, links: { ...store.links, [action.guid]: action.link } }),
        'fieldSet': (store: any, action: { guid: string, field: Field }) =>
            ({ ...store, fields: { ...store.fields, [action.guid]: action.field } }),
        'portalsSet': (store: any, action: { portals: Portal }) => {
            const portals = store.portals
            Object.keys(action.portals).forEach(guid => {
                if (portals[guid]) {
                    portals[guid] = { ...portals[guid], fraction: action.portals[guid].fraction, level: action.portals[guid].level }
                } else {
                    portals[guid] = action.portals[guid]
                }

            })
            return { ...store, portals }
        },
        'linksSet': (store: any, action: { links: { [guid: string]: Link } }) =>
            ({ ...store, links: { ...store.links, ...action.links } }),
        'fieldsSet': (store: any, action: { fields: { [guid: string]: Field } }) =>
            ({ ...store, fields: { ...store.fields, ...action.fields } }),
        'setLoad': (store: any, action: { queue: string[][] }) =>
            ({ ...store, loadQueue: [...action.queue] }),
        'linksGC': (store: any, action: { links: { [tile: string]: string[] } }) => {
            let linksCache = store.linksCache
            let links = store.links
            Object.keys(action.links).forEach(tileId => {
                const oldLinks = linksCache[tileId]
                if (oldLinks != undefined) {
                    oldLinks.forEach((guid: string) => {
                        delete links[guid]
                    })
                }
                linksCache[tileId] = action.links[tileId]
            })
            return ({ ...store, links: { ...links }, linksCache })
        },
        'fieldsGC': (store: any, action: { fields: { [tile: string]: string[] } }) => {
            let fieldsCache = store.fieldsCache
            let fields = store.fields
            Object.keys(action.fields).forEach(tileId => {
                const oldFields = fieldsCache[tileId]
                if (oldFields != undefined) {
                    oldFields.forEach((guid: string) => {
                        delete fields[guid]
                    })
                }
                fieldsCache[tileId] = action.fields[tileId]
            })
            return ({ ...store, fields: { ...fields }, fieldsCache })
        },
    }, { portals: {}, fields: {}, links: {}, loadQueue: [], linksCache: {}, fieldsCache: {} }),
    'settings': createReducer({
        'setLevelFrom': (store: any, action: { level: number }) => ({
            ...store, filterLevel: [action.level, store.filterLevel[1]]
        }),
        'setLevelTo': (store: any, action: { level: number }) => ({
            ...store, filterLevel: [store.filterLevel[0], action.level]
        })
    }, { filterLevel: [0, 8] })
}

function extend(obj: any, src: any) {
    for (var key in src) {
        if (src.hasOwnProperty(key) && !!src[key]) {
            obj[key] = src[key];
        }
    }
    return obj;
}

const store = createStore(
    combineReducers(reducers),
    composeWithDevTools(applyMiddleware(thunk))
)
export default store