Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Created July 23, 2017 22:38
Show Gist options
  • Select an option

  • Save faceyspacey/a1906757b979c09860bc1c67bda0fa34 to your computer and use it in GitHub Desktop.

Select an option

Save faceyspacey/a1906757b979c09860bc1c67bda0fa34 to your computer and use it in GitHub Desktop.
isLoadingReducer.js
const memory = {}
const isLoading = (state = false, action = {}, mem = memory) => {
switch (action.type) {
case 'LIST':
return !mem[action.payload.category]
case 'VIDEO':
return !mem[action.payload.slug]
case 'VIDEOS_FETCHED':
return !(mem[action.payload.category] = true)
case 'VIDEO_FOUND':
return !(mem[action.payload.slug] = true)
default:
return state
}
}
// the thunk for /video/:slug has to change to this:
VIDEO: {
path: '/video/:slug',
thunk: async (dispatch, getState) => {
const { location: { payload: { slug } } } = getState()
const video = await fetchData(`/api/video/${slug}`)
if (!video) {
return dispatch({ type: NOT_FOUND })
}
dispatch({ type: 'VIDEO_FOUND', payload: { slug, video }) // put the slug in the payload now
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment