Created
July 23, 2017 22:38
-
-
Save faceyspacey/a1906757b979c09860bc1c67bda0fa34 to your computer and use it in GitHub Desktop.
isLoadingReducer.js
This file contains hidden or 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
| 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