Download react-redux-realworld-example-app/src/middleware.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-redux-realworld-example-app/src/middleware.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-redux-realworld-example-app/src/middleware.js
-
curl -L -o middleware.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-redux-realworld-example-app/src/middleware.js
1.77 kB
| import agent from './agent'; | |
| import { | |
| ASYNC_START, | |
| ASYNC_END, | |
| LOGIN, | |
| LOGOUT, | |
| REGISTER | |
| } from './constants/actionTypes'; | |
| const promiseMiddleware = store => next => action => { | |
| if (isPromise(action.payload)) { | |
| store.dispatch({ type: ASYNC_START, subtype: action.type }); | |
| const currentView = store.getState().viewChangeCounter; | |
| const skipTracking = action.skipTracking; | |
| action.payload.then( | |
| res => { | |
| const currentState = store.getState() | |
| if (!skipTracking && currentState.viewChangeCounter !== currentView) { | |
| return | |
| } | |
| console.log('RESULT', res); | |
| action.payload = res; | |
| store.dispatch({ type: ASYNC_END, promise: action.payload }); | |
| store.dispatch(action); | |
| }, | |
| error => { | |
| const currentState = store.getState() | |
| if (!skipTracking && currentState.viewChangeCounter !== currentView) { | |
| return | |
| } | |
| console.log('ERROR', error); | |
| action.error = true; | |
| action.payload = error.response.body; | |
| if (!action.skipTracking) { | |
| store.dispatch({ type: ASYNC_END, promise: action.payload }); | |
| } | |
| store.dispatch(action); | |
| } | |
| ); | |
| return; | |
| } | |
| next(action); | |
| }; | |
| const localStorageMiddleware = store => next => action => { | |
| if (action.type === REGISTER || action.type === LOGIN) { | |
| if (!action.error) { | |
| window.localStorage.setItem('jwt', action.payload.user.token); | |
| agent.setToken(action.payload.user.token); | |
| } | |
| } else if (action.type === LOGOUT) { | |
| window.localStorage.setItem('jwt', ''); | |
| agent.setToken(null); | |
| } | |
| next(action); | |
| }; | |
| function isPromise(v) { | |
| return v && typeof v.then === 'function'; | |
| } | |
| export { promiseMiddleware, localStorageMiddleware } | |