/
app.tsx
158 lines (141 loc) · 3.97 KB
/
app.tsx
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as dotProp from "dot-prop-immutable"
import { IConfig } from "./interfaces"
import { IAnalyticsMeta } from "../../shared/analytics/GoogleAnalytics";
declare var Config: IConfig
// Actions
const LOADING = "ealgis/app/LOADING"
const LOADED = "ealgis/app/LOADED"
const BEGIN_FETCH = "ealgis/app/BEGIN_FETCH"
const FINISH_FETCH = "ealgis/app/FINISH_FETCH"
const SET_LAST_PAGE = "ealgis/app/SET_LAST_PAGE"
const TOGGLE_SIDEBAR = "ealgis/app/TOGGLE_SIDEBAR"
const TOGGLE_MODAL = "ealgis/app/TOGGLE_MODAL"
const TOGGLE_USER_MENU = "ealgis/app/TOGGLE_USER_MENU"
const SET_ACTIVE_COMPONENT = "ealgis/app/SET_ACTIVE_COMPONENT"
export enum eEalUIComponent {
MAP_UI = 1, // Actually, this includes the static pages too.
DATA_BROWSER = 2,
FILTER_EXPRESSION_EDITOR = 3,
VALUE_EXPRESSION_EDITOR = 4,
}
const initialState: IModule = {
private_site: Config["PRIVATE_SITE"],
loading: true,
requestsInProgress: 0,
sidebarOpen: true,
previousPath: "",
modals: new Map(),
userMenuState: false,
activeContentComponent: eEalUIComponent.MAP_UI,
}
// Reducer
export default function reducer(state = initialState, action: IAction) {
let requestsInProgress = dotProp.get(state, "requestsInProgress")
switch (action.type) {
case LOADING:
return dotProp.set(state, "loading", true)
case LOADED:
return dotProp.set(state, "loading", false)
case BEGIN_FETCH:
return dotProp.set(state, "requestsInProgress", ++requestsInProgress)
case FINISH_FETCH:
return dotProp.set(state, "requestsInProgress", --requestsInProgress)
case SET_LAST_PAGE:
return dotProp.set(state, "previousPath", action.previousPath)
case TOGGLE_SIDEBAR:
return dotProp.toggle(state, "sidebarOpen")
case TOGGLE_MODAL:
const modals = dotProp.get(state, "modals")
modals.set(action.modalId, !modals.get(action.modalId))
return dotProp.set(state, "modals", modals)
case TOGGLE_USER_MENU:
return dotProp.set(state, "userMenuState", action.open)
case SET_ACTIVE_COMPONENT:
return dotProp.set(state, "activeContentComponent", action.contentComponent)
default:
return state
}
}
// Action Creators
export function loading(): IAction {
return {
type: LOADING,
}
}
export function loaded(): IAction {
return {
type: LOADED,
}
}
export function beginFetch(): IAction {
return {
type: BEGIN_FETCH,
}
}
export function finishFetch(): IAction {
return {
type: FINISH_FETCH,
}
}
export function setLastPage(previousPath: string): IAction {
return {
type: SET_LAST_PAGE,
previousPath,
}
}
export function toggleSidebarState(): IAction {
return {
type: TOGGLE_SIDEBAR,
meta: {
analytics: {
category: "App",
},
},
}
}
export function toggleModalState(modalId: string): IAction {
return {
type: TOGGLE_MODAL,
modalId,
}
}
export function toggleUserMenu(open: boolean): IAction {
return {
type: TOGGLE_USER_MENU,
open,
meta: {
analytics: {
category: "App",
},
},
}
}
export function setActiveContentComponent(contentComponent: eEalUIComponent) {
return {
type: SET_ACTIVE_COMPONENT,
contentComponent,
}
}
// Models
export interface IModule {
private_site: boolean
loading: boolean
requestsInProgress: number
sidebarOpen: boolean
previousPath: string
modals: Map<string, boolean>
userMenuState: boolean
activeContentComponent: eEalUIComponent
}
export interface IAction {
type: string
previousPath?: string
modalId?: string
open?: boolean
contentComponent?: eEalUIComponent
meta?: {
analytics: IAnalyticsMeta
}
}
// Side effects, only as applicable
// e.g. thunks, epics, et cetera