Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Last active August 10, 2017 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faceyspacey/05a6c2d8b173d624f20759e9d59156ce to your computer and use it in GitHub Desktop.
Save faceyspacey/05a6c2d8b173d624f20759e9d59156ce to your computer and use it in GitHub Desktop.
import { redirect } from 'redux-first-router'
import { isAllowed } from './utils'
const options = {
onBeforeChange: (dispatch, getState, action) => {
const allowed = isAllowed(action.type, getState())
if (!allowed) {
const action = redirect({ type: 'LOGIN' })
dispatch(action)
}
}
}
const routesMap = {
LIST: {
path: '/list/:category',
thunk: async (dispatch, getState) => {
const{ payload: { category } } = getState().location
const videos = await fetchData(`/api/videos/${category}`)
if (videos.length === 0) {
const action = redirect({ type: 'LIST', payload: { category: 'all' })
dispatch(action)
}
dispatch({ type: 'VIDEOS_FETCHED', payload: { videos, category } })
}
}
}
const routesMap = {
LIST: {
path: '/list/:category',
thunk: async (dispatch, getState) => {
const {
location: { payload: { category } },
listData
} = getState()
if (listData[category]) return
const videos = await fetchData(`/api/videos/${category}`)
if (videos.length === 0) {
const action = redirect({ type: 'LIST', payload: { category: 'all' })
dispatch(action)
}
dispatch({ type: 'VIDEOS_FETCHED', payload: { videos, category } })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment