Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for "Is Loading" State? #107

Open
iMerica opened this issue Aug 28, 2019 · 1 comment
Open

Support for "Is Loading" State? #107

iMerica opened this issue Aug 28, 2019 · 1 comment

Comments

@iMerica
Copy link

iMerica commented Aug 28, 2019

Hello,

My app has over 75 different types of axios actions using this library and I would like to handle the loading state in a generalized way rather than hundreds of lines of case TYPE.FOO_SUCCESS in my reducers.

For context: I have a single page application with loading spinners at the "page" level and on individual sub components. They all use the same "state.loading" state.

Is there a best practice or pattern here?

I'v tried this:

let loading = false
let requestsHappening = 0;

export const rootReducer = (state = initialState, action) => {
  if (isAxiosRequest(action) && !/(.*)_(SUCCESS|FAIL)/.exec(action.type)) {
    requestsHappening += 1
    loading = true
  } else if (isAxiosRequest(action)) {
    requestsHappening -= 1
    loading = requestsHappening !== 0;
  }
  switch (action.type) {
    // hundreds of lines of Redux boilerplate
    default:
      return {
        ...state,
        loading,
    }
  }
}

But I can't seem to make it work.

@jacobgavin
Copy link

I would guess that you can add interceptors like they show in the documentation and on request success use the dispatch you get from the config to dispatch something like
dispatch({ type: 'REQUEST_START' })
and in your interceptors for response
dispatch({ type: 'REQUEST_RESOLVED' })

or something like that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants