Last active
August 10, 2017 08:13
-
-
Save faceyspacey/05a6c2d8b173d624f20759e9d59156ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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