diff options
Diffstat (limited to 'src/Store')
-rw-r--r-- | src/Store/store.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/Store/store.ts b/src/Store/store.ts new file mode 100644 index 0000000..0929882 --- /dev/null +++ b/src/Store/store.ts @@ -0,0 +1,63 @@ +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'; + +type Display = { + portals: string[], + fields: string[], + links: string[] +} + +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 => { + const portal: Portal = portals[guid] + const newPortal: Portal = action.portals[guid] + if (!portal) { + portals[guid] = newPortal + return + } + portals[guid] = extend(portal, newPortal) + }) + 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 } }), + 'entitiesLoad': (store: any, action: { loading: number }) => + ({ ...store, loading: action.loading }), + 'entitiesDisplay': (store: any, action: { display: Display }) => + ({ ...store, display: action.display }), + + }, { portals: {}, fields: {}, links: {}, loading: 0, display: { portals: [], fields: [], links: [] } }) +} + +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
\ No newline at end of file |