1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { createReducer } from 'redux-su';
import { AsyncStorage } from 'react-native';
import { persistStore, persistReducer } from 'redux-persist';
import { Portal, Link, Field } from '../Api/types';
import { Region } from '../Api/interfaces';
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] }),
'portalsGC': (store: any, action: { portals: { [tile: string]: string[] } }) => {
let portalsCache = store.portalsCache
let portals = store.portals
Object.keys(action.portals).forEach(tileId => {
const oldPortals = portalsCache[tileId]
if (oldPortals != undefined) {
oldPortals.forEach((guid: string) => {
delete portals[guid]
})
}
portalsCache[tileId] = action.portals[tileId]
})
return ({ ...store, portals: { ...portals }, portalsCache })
},
'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: {}, portalsCache: {} }),
'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]
}),
'setRegion': (store: any, action: { region: Region }) => ({
...store, region: action.region
})
}, { filterLevel: [0, 8], region: null })
}
const rootReducer = combineReducers(reducers)
const persistConfig = {
key: 'root',
storage: AsyncStorage,
}
const persistedReducer = persistReducer(persistConfig, rootReducer)
export default () => {
let store = createStore(persistedReducer, applyMiddleware(thunk))
let persistor = persistStore(store)
return { store, persistor }
}
|