Ric
Ric

Reputation: 29

Use redux-thunk with redux-promise-middleware in the correct way

I'm working in a project with react and redux, I'm enough new so I'm trying to understand better how to use redux-thunk and redux-promise together. Below you can see my files, in my actions I created a fetch generic function apiFetch() in order to use every time I need to fetch. This function return a promise, that I'm going to resolve in loadBooks(), the code is working and the records are uploaded but when I check the log of the actions I see that the first action is undefined, after there is BOOKS_LOADING, LOAD_BOOKS, BOOKS_LOADING and LOAD_BOOKS_SUCCESS.

I've 2 questions about that:

1) Why is the first action undefined and I've LOAD_BOOKS instead than LOAD_BOOKS_START?

action @ 22:54:37.403 undefined
core.js:112  prev state Object {activeBook: null, booksListing: Object}
core.js:116  action function (dispatch) {
        var url = './src/data/payload.json';
        dispatch(booksIsLoading(true));
        return dispatch({
          type: 'LOAD_BOOKS',
          payload: new Promise(function (resolve) {
…
core.js:124  next state Object {activeBook: null, booksListing: Object}
action @ 22:54:37.404 BOOKS_LOADING
action @ 22:54:37.413 LOAD_BOOKS
action @ 22:54:39.420 BOOKS_LOADING
action @ 22:54:39.425 LOAD_BOOKS_SUCCESS

2) If for example the url for the fetch is wrong, I expected to see the action LOAD_BOOKS_ERROR, instead this is the result of the log:

action @ 23:06:06.837 undefined action @ 23:06:06.837 BOOKS_LOADING
action @ 23:06:06.846 LOAD_BOOKS GET
http://localhost:8000/src/data/payldoad.json 404 (Not Found) error
apiFetch Error: request failed at index.js:66 error 
TypeError: Cannot read property 'json' of undefined at index.js:90

If I don't use apiFetch(), but normal fetch function, all is working correctly, also the part of the error, with the exception that anyway LOAD_BOOKS is not LOAD_BOOKS_START.

Thank you in advance for any help!

configureStore.js

import { createStore, applyMiddleware, compose, preloadedState } from 'redux';
import reducers from './configureReducer';
import configureMiddleware from './configureMiddleware';

const middleware = configureMiddleware();
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ||   compose;
const store = createStore(reducers, preloadedState, composeEnhancers(applyMiddleware(...middleware)));
export default store;

actions/index.js

import fetch from 'isomorphic-fetch';

export const booksIsLoading = (bool) => {
  return {
    type: 'BOOKS_LOADING',
    booksLoading: bool,
  };
};

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

export const apiFetch = (url) => {
  const getPromise = () => (
    fetch(url, {
      method: 'GET',
    })
      .then((response) => {
        if (response.status !== 200) {
          throw Error('request failed');
        }
        return response;
      })
      .catch((err) => {
        console.log('error apiFetch', err);
        // dispatch(fetchBooksError(true));
      })
  );
  return getPromise();
};

export const loadBooks = () => (dispatch) => {
  const url = './src/data/payload.json';
  dispatch(booksIsLoading(true));
  return dispatch({
    type: 'LOAD_BOOKS',
    payload: new Promise((resolve) => {
      delay(2000).then(() => {
        apiFetch(`${url}`)
          // fetch(`${url}`, {
          //   method: 'GET',
          // })
          .then((response) => {
            resolve(response.json());
            dispatch(booksIsLoading(false));
          }).catch((err) => {
            console.log('error', err);
          });
      });
    }),
  });
};

constants/application.js

export const LOAD_BOOKS = 'LOAD_BOOKS';

reducers/reducer_book.js

import initialState from '../model.js';
import * as types from '../constants/application';

export default function (state = initialState, action) {
  switch (action.type) {
    case `${types.LOAD_BOOKS}_SUCCESS`: {
      console.log('reducer', action.payload);
      const data = action.payload.data.items;
      const items = Object.values(data);

      if (items.length > 0) {
        return {
          ...state,
          books: Object.values(data),
          booksFetched: true,
          booksError: false,
        };
      }

      return state;
    }

    case `${types.LOAD_BOOKS}_ERROR`: {
      return {
        ...state,
        booksError: true,
      };
    }

    case 'BOOKS_LOADING':
      return {
        ...state,
        booksLoading: action.booksLoading,
      };

    default:
      return state;
  }
}

Upvotes: 2

Views: 2461

Answers (1)

Crazywako
Crazywako

Reputation: 362

In which order did you specify middlewares?

Following usage makes action go undefined:

'applyMiddleware(reduxPromiseMiddleware(), reduxThunk)'

Please change the order to: ( thunk first! )

'applyMiddleware(reduxThunk, reduxPromiseMiddleware())'

Upvotes: 1

Related Questions