-
Notifications
You must be signed in to change notification settings - Fork 3
/
reducers.js
189 lines (173 loc) · 4.45 KB
/
reducers.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { combineReducers } from 'redux'
import { MOVE_LEFT, MOVE_TO, SET_X, SET_VIEWPORT_WIDTH } from './runtime/actions'
import { NAVIGATE, TOGGLE_EXPAND, UPDATE_SETTINGS } from './actions'
import getViewportWidth from './runtime/get-viewport-width'
function apps(state = { byName: {}, items: [] }, action) {
if (action.type === NAVIGATE &&
action.sequence.type === 'next' &&
action.payload.apps.items.length) {
return {
byName: {
...state.byName,
...action.payload.apps.byName
},
items: [
...state.items,
...action.payload.apps.items
]
}
} else {
return state
}
}
function panels(state = { byId: {}, items: [] }, action) {
if (action.type === NAVIGATE &&
action.sequence.type === 'next' &&
action.payload.panels.items.length) {
return {
byId: {
...state.byId,
...action.payload.panels.byId
},
items: [
...state.items,
...action.payload.panels.items
]
}
} else if (action.type === UPDATE_SETTINGS) {
return {
...state,
byId: action.payload.nextPanelsById
}
} else {
return state
}
}
function router(state = { isLoading: true, routes: { byContext: {}, items: [] } }, action) {
switch (action.type) {
case NAVIGATE:
if (action.sequence.type === 'start') {
return {
...state,
isLoading: true,
uri: action.meta.uri
}
} else if (action.sequence.type === 'next') {
return {
...state,
...action.payload.router,
isLoading: false
}
}
break
case SET_VIEWPORT_WIDTH:
case TOGGLE_EXPAND:
return {
...state,
routes: {
items: state.routes.items,
byContext: action.payload.routesByContext
}
}
break
case UPDATE_SETTINGS:
if (action.payload.nextPosition) {
return {
...state,
routes: {
items: state.routes.items,
byContext: action.payload.nextPosition.routesByContext
}
}
} else {
return state
}
break
default: return state
}
}
export const MOBILE_THRESHOLD = 720
// TODO REVERT!
const preferredSnapPoint = window.panels && typeof window.panels.preferredSnapPoint === 'number' ? window.panels.preferredSnapPoint : 90
const viewportWidth = getViewportWidth()
const shouldGoMobile = viewportWidth < MOBILE_THRESHOLD
const snapPoint = shouldGoMobile ? 0 : preferredSnapPoint
const RUNTIME_DEFAULT_STATE = {
maxFullPanelWidth: viewportWidth - snapPoint,
preferredSnapPoint,
snappedAt: 0,
snapPoint,
shouldGoMobile,
x: 0,
viewportWidth,
width: viewportWidth,
widths: []
}
function runtime(state = RUNTIME_DEFAULT_STATE, action) {
switch (action.type) {
// TODO normalise
case MOVE_LEFT:
case MOVE_TO:
case SET_X:
return {
...state,
snappedAt: action.payload.snappedAt,
x: action.payload.x
}
break
case NAVIGATE:
if (action.sequence.type === 'next') {
return {
...state,
maxFullPanelWidth: action.payload.runtime.maxFullPanelWidth,
regions: action.payload.runtime.regions,
snappedAt: action.payload.runtime.snappedAt,
width: action.payload.runtime.width,
widths: action.payload.runtime.widths,
x: action.payload.runtime.x
}
} else {
return state
}
break
case SET_VIEWPORT_WIDTH:
return {
...state,
maxFullPanelWidth: action.payload.maxFullPanelWidth,
shouldGoMobile: action.payload.shouldGoMobile,
snapPoint: action.payload.snapPoint,
viewportWidth: action.payload.viewportWidth,
width: action.payload.width,
widths: action.payload.widths,
x: action.payload.x
}
break
case TOGGLE_EXPAND:
return {
...state,
width: action.payload.width,
widths: action.payload.widths,
x: action.payload.x
}
break
case UPDATE_SETTINGS:
if (action.payload.nextPosition) {
return {
...state,
width: action.payload.nextPosition.width,
widths: action.payload.nextPosition.widths,
x: action.payload.nextPosition.x
}
} else {
return state
}
break
default: return state
}
}
export default combineReducers({
apps,
panels,
router,
runtime
})