UNPKG

4.55 kBJavaScriptView Raw
1import { combineReducers } from 'redux'
2import { MOVE_LEFT, MOVE_TO, SET_X, SET_VIEWPORT_WIDTH } from './runtime/actions'
3import { NAVIGATE, TOGGLE_EXPAND, UPDATE_SETTINGS } from './actions'
4import getViewportWidth from './runtime/get-viewport-width'
5
6function apps(state = { byName: {}, items: [] }, action) {
7 if (action.type === NAVIGATE &&
8 action.sequence.type === 'next' &&
9 action.payload.apps.items.length) {
10 return {
11 byName: {
12 ...state.byName,
13 ...action.payload.apps.byName
14 },
15 items: [
16 ...state.items,
17 ...action.payload.apps.items
18 ]
19 }
20 } else {
21 return state
22 }
23}
24
25function panels(state = { byId: {}, items: [] }, action) {
26 if (action.type === NAVIGATE &&
27 action.sequence.type === 'next' &&
28 action.payload.panels.items.length) {
29 return {
30 byId: {
31 ...state.byId,
32 ...action.payload.panels.byId
33 },
34 items: [
35 ...state.items,
36 ...action.payload.panels.items
37 ]
38 }
39 } else if (action.type === UPDATE_SETTINGS) {
40 return {
41 ...state,
42 byId: action.payload.nextPanelsById
43 }
44 } else {
45 return state
46 }
47}
48
49function router(state = { isLoading: true, routes: { byContext: {}, items: [] } }, action) {
50 switch (action.type) {
51 case NAVIGATE:
52 if (action.sequence.type === 'start') {
53 return {
54 ...state,
55 isLoading: true,
56 uri: action.meta.uri
57 }
58 } else if (action.sequence.type === 'next') {
59 return {
60 ...state,
61 ...action.payload.router,
62 isLoading: false
63 }
64 }
65 break
66
67 case SET_VIEWPORT_WIDTH:
68 case TOGGLE_EXPAND:
69 return {
70 ...state,
71 routes: {
72 items: state.routes.items,
73 byContext: action.payload.routesByContext
74 }
75 }
76 break
77
78 case UPDATE_SETTINGS:
79 if (action.payload.nextPosition) {
80 return {
81 ...state,
82 routes: {
83 items: state.routes.items,
84 byContext: action.payload.nextPosition.routesByContext
85 }
86 }
87 } else {
88 return state
89 }
90 break
91
92 default: return state
93 }
94}
95
96export const MOBILE_THRESHOLD = 720
97
98// TODO REVERT!
99const preferredSnapPoint = window.panels && typeof window.panels.preferredSnapPoint === 'number' ? window.panels.preferredSnapPoint : 90
100const viewportWidth = getViewportWidth()
101const shouldGoMobile = viewportWidth < MOBILE_THRESHOLD
102const snapPoint = shouldGoMobile ? 0 : preferredSnapPoint
103
104const RUNTIME_DEFAULT_STATE = {
105 maxFullPanelWidth: viewportWidth - snapPoint,
106 preferredSnapPoint,
107 snappedAt: 0,
108 snapPoint,
109 shouldGoMobile,
110 x: 0,
111 viewportWidth,
112 width: viewportWidth,
113 widths: []
114}
115
116function runtime(state = RUNTIME_DEFAULT_STATE, action) {
117 switch (action.type) {
118 // TODO normalise
119 case MOVE_LEFT:
120 case MOVE_TO:
121 case SET_X:
122 return {
123 ...state,
124 snappedAt: action.payload.snappedAt,
125 x: action.payload.x
126 }
127 break
128
129 case NAVIGATE:
130 if (action.sequence.type === 'next') {
131 return {
132 ...state,
133 maxFullPanelWidth: action.payload.runtime.maxFullPanelWidth,
134 regions: action.payload.runtime.regions,
135 snappedAt: action.payload.runtime.snappedAt,
136 width: action.payload.runtime.width,
137 widths: action.payload.runtime.widths,
138 x: action.payload.runtime.x
139 }
140 } else {
141 return state
142 }
143 break
144
145 case SET_VIEWPORT_WIDTH:
146 return {
147 ...state,
148 maxFullPanelWidth: action.payload.maxFullPanelWidth,
149 shouldGoMobile: action.payload.shouldGoMobile,
150 snapPoint: action.payload.snapPoint,
151 viewportWidth: action.payload.viewportWidth,
152 width: action.payload.width,
153 widths: action.payload.widths,
154 x: action.payload.x
155 }
156 break
157
158 case TOGGLE_EXPAND:
159 return {
160 ...state,
161 width: action.payload.width,
162 widths: action.payload.widths,
163 x: action.payload.x
164 }
165 break
166
167 case UPDATE_SETTINGS:
168 if (action.payload.nextPosition) {
169 return {
170 ...state,
171 width: action.payload.nextPosition.width,
172 widths: action.payload.nextPosition.widths,
173 x: action.payload.nextPosition.x
174 }
175 } else {
176 return state
177 }
178 break
179
180 default: return state
181 }
182}
183
184export default combineReducers({
185 apps,
186 panels,
187 router,
188 runtime
189})