forked from faceyspacey/redux-first-router-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routesMap.js
123 lines (110 loc) · 2.65 KB
/
routesMap.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
import { redirect, NOT_FOUND } from 'redux-first-router'
import { fetchData, isAllowed, isServer } from './utils'
export default {
HOME: '/',
LIST: {
path: '/list/:category',
thunk: async (dispatch, getState) => {
const {
jwToken,
location: { payload: { category } },
videosByCategory: { categories }
} = getState()
if (categories[category]) return
const videos = await fetchData(`/api/videos/${category}`, jwToken)
if (videos.length === 0) {
return dispatch({ type: NOT_FOUND })
}
dispatch({ type: 'VIDEOS_FETCHED', payload: { videos, category } })
}
},
VIDEO: {
path: '/video/:slug',
thunk: async (dispatch, getState) => {
// TASK FOR YOU. YES, YOU!
//
// visit a VIDEO page in the app, then refresh the page, then make
// this work when visited directly by copying the LIST route above and
// using fetchData(`/api/video/${slug}`) and by dispatching
// the the corresponding action type which I'll leave up to you to find
// in ../reducers/index.js :)
}
},
PLAY: {
path: '/video/:slug/play',
thunk: (dispatch, getState) => {
if (typeof window === 'undefined') {
const { slug } = getState().location.payload
const action = redirect({ type: 'VIDEO', payload: { slug } })
dispatch(action)
}
}
},
LOGIN: '/login',
ADMIN: {
path: '/admin', // TRY: visit this path or dispatch ADMIN
role: 'admin' // + change jwToken to 'real' in server/index.js
}
}
export const options = {
onBeforeChange: (dispatch, getState, action) => {
const allowed = isAllowed(action.type, getState())
if (!allowed) {
const action = redirect({ type: 'LOGIN' })
dispatch(action)
}
},
onAfterChange: (dispatch, getState) => {
const { type } = getState().location
if (type === 'LOGIN' && !isServer) {
setTimeout(() => {
alert(
"NICE, You're adventurous! Try changing the jwToken cookie from 'fake' to 'real' in server/index.js to access the Admin Panel. Then 'onBeforeChange' will let you in."
)
}, 1500)
}
}
}
// DON'T GO DOWN THERE!
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// |
// ▼
// ANSWER: videoRoute.thunk.body:
/* HURRAY! You found the answers on the back of the cereal box!
const { jwToken, location: { payload: { slug } } } = getState()
const video = await fetchData(`/api/video/${slug}`, jwToken)
if (!video) {
return dispatch({ type: NOT_FOUND })
}
dispatch({ type: 'VIDEO_FOUND', payload: video })
*/