1.0.6 • Published 3 years ago

redux-load-middleware v1.0.6

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

Redux Load Middleware

Error and loading handler for React Apps

See example project here

Redux Load Middleware enables simple, yet robust handling of async action creators in Redux.

I wrote redux-load-middleware to make basic stuff like error and loading handling seamless. So you could focus on stuff that matters.

Why not redux-saga?

It possible and even encouraged to use redux-load-middleware in conjunction with redux-saga. Think of redux-load-middleware as it's just util that handles loading and errors for you, for many other things you can use redux-saga. It is quite verbose to write async calls with saga you have to write try-catch all the time. And error and loading handling often mix with business logic as a result it becomes harder to read sagas.

Saga can be used for other things like calling other actions after updating the state. Which is the same as what you did before, just excluding error/loading handling.

Why not redux-promise-middleware?

redux-load-middleware works in the same way as redux-promise-middleware. With redux-promise-middleware you have to handle loading and errors manually. The redux-load-middleware has it is own errorsReducer and loadingsReducer to which you just connect with selectors.

Installation

  • Connect loadMiddleware to the store
import { createLoadMiddleware } from 'redux-load-middleware'

const loadMiddleware = createLoadMiddleware()
const store = createStore(reducers, compose(applyMiddleware(loadMiddleware)))
  • Add loadingsReducer and errorsReducer to your reducers
// store.ts
import { combineReducers } from 'redux'
import { loadingsReducer, errorsReducer } from 'redux-load-middleware'

const reducer = combineReducers({
  loadings: loadingsReducer,
  errors: errorsReducer,
  //...your other reducers
})

export default reducer

Getting Started

Adding loading handler

Create action with load property that accepts Promise. Then come up with a name for loading. It is convenient to use the same name as action name.

// actions.ts
import { LoadAction } from 'redux-load-middleware'
type LoginUserAction = LoadAction<c.LOGIN_USER>;

export const loginUser = (): LoginUserAction => ({
  type: 'LOGIN_USER',
  load: api.user.login(), // returns Promise<User>
  loading: 'loginUser', // this name will be used to reterive loading status from loadingsReducer
});

When you dispatch load user action LOGIN_USER_PENDING will be dispatched and in loadings state will set loginUser to true

{
  loadings: {
    loginUser: true 
  }
}

With your selector just connect to loadings state. No need to handle loading in your reducer.

import { createLoadingSelector } from 'redux-load-middleware'
  
export const selectUserLoading = createLoadingSelector('loginUser')

After Promise resolves LOGIN_USER_SUCCESS will be dispatched with payload: User. So you can just listen for LOGIN_USER_SUCCESS, ether from your reducer or sagas.

import { PayloadAction } from 'redux-load-middleware'
import { User } from 'api'

const LoginUserSuccessAction = PayloadAction<'LOGIN_USER_SUCCESS', User>;

const authReducer = (state, action: LoginUserSuccessAction)=>{
    switch (action){
      case 'LOGIN_USER_SUCCESS':
        return {
          user: action.payload
        }
    default:
       return state
    }
}

Action LOGIN_USER_SUCCESS will also remove loginUser from loadings.

{
  loadings: { }
}

Adding error handling

Add errors property action Then come up with a name for loading. It is convenient to use the same name as action name.

// actions.ts
import { LoadAction } from 'redux-load-middleware'
import { loginUserErrors } from './errors'
export type LoginUserAction = LoadAction<c.LOGIN_USER>;

export const loginUser = (): LoginUserAction => ({
  type: 'LOGIN_USER',
  load: api.user.login(),
  loading: 'loginUser',
  errors: loginUserErrors
});

Create errors object and come up with a name for error that your UI will handle for example alertError.

// errors.ts
import { AxiosError } from 'api'

export const loginUserErrors = {
  alertError: (): string | undefined => {
    // your error handling goes here
    return 'Something happened. Try again later'
  },
  // any type of error can me accepted
  formError: (error: AxiosError): string | undefined => { 
     // your data from api response for example 'This email already taken!'
    const message = error?.response?.message
    // you can return any data type string used here as an example
    if(message) return message
  } 
}

When you dispatch loginUser action and Promise gets rejected LOGIN_USER_ERROR will be dispatched and in errors state will set alertError to

{
  loadings: {}
  errors: {  
    alertError: 'Your device is offline!'
    // OR if form error has occured
    formError: 'This email already taken!'
  }
}

You can retrieve error message from state using createErrorSelector helper using alertError key.

import { createErrorSelector } from 'redux-load-middleware';
import { createSelector, Selector } from 'reselect';
import { State } from 'src/store/reducer';


export const selectAlertErrorMessage = createErrorSelector('alertError')

export const selectFormErrorMessage: Selector<State, string | null> = createSelector(
  createErrorSelector('formError'),
  (message) => message ? message : null,
);

In case LOGIN_USER_SUCCESS errors will be cleared.

{
  loadings: {}
  errors: {}
  auth: { 
    user: { 
      id: 1
      name: 'John Doe'
    }
 }
}

Setting loading manually

In order to dispatch error with your custom data

import { setLoading } from 'react-load-middleware'

dispatch(setLoading('manualLoading'))

This will dispatch SET_LOADING action and update store

{
  loadings: {  
    manualLoading:  true
  }
}

To remove loading from loadings state

import { removeLoading } from 'react-load-middleware'

dispatch(removeLoading('manualLoading'))

Will dispatch REMOVE_LOADING action and update state:

{
  loadings: {}
}

Setting errors manually

In order to dispatch error with your custom data

import { setError } from 'react-load-middleware'

dispatch(setError('manualError', { any: 'data' }))

This will dispatch SET_ERROR action and update store

{
  errors: {  
    manualError:  { any: 'data' }
  }
}

To remove error from errors state

import { removeError } from 'react-load-middleware'

dispatch(removeError('manualError'))

Will dispatch REMOVE_ERROR action and update store

{
  errors: {}
}

MIT License

Copyright (c) 2020 Pavel Kostyuk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago