0

I'am trying to implement Redux-offline in my react-native app for this i have installed the module and i have added offline to my createStore method :

 const store = createStore(
  myReducer,
  compose(
    offline(offlineConfig),
    applyMiddleware(thunk, promiseMiddleware())
  )
);

this is the action that uses redux-offline :

export const addResources = resource => ({
  type: "ADD_RESOURCES",
  payload: resource,
  meta: {
    offline: {
      // the network action to execute:
      effect: {
        url: "https://reqres.in/api/users",
        method: "POST",
        json: {
          body: { name: "paul rudd", movies: ["Fake data", "Fake text"] }
        }
      },
      // action to dispatch when effect succeeds:
      commit: { type: "FOLLOW_ADD_RESOURCE", meta: { resource } },
      // action to dispatch if network action fails permanently:
      rollback: { type: "ADD_RESOURCE_ROLLBACK", meta: { resource } }
    }
  }
});

for the sake of explanation i'am using a sample dummy API that accepts the creation of new users and return an id as a response. My problem is that the commit action never gets called after dispatching my ADD_RESOURCES action, on the other hand therollback gets called if i'am sending a bad request.

This is my reducer:

let tempList = state.list.concat();
  switch (action.type) {
    case ADD_RESOURCES:
      console.log("add resources action");
      return Object.assign({}, state, {
        list: tempList
      });
    case FOLLOW_ADD_RESOURCE:
      console.log("  *** FOLLOW_ADD_RESOURCE **", res);
      return Object.assign({}, state, {
        list: tempList
      });
    case ADD_RESOURCE_ROLLBACK:
      console.log("ADD_RESOURCE_ROLLBACK");
      return Object.assign({}, state, {
        list: tempList
      });
    default:
      return state;
  }

PS: i'am testing this on Pixel 2 xl API 27 emulator, with and without wifi and 3G internet connection.

As i said the commit action never get dispatched, does anyone know what did i get wrong ?

Hamdi Gatri
  • 101
  • 2
  • 12
  • The way that I read this, you are posting to the internet when you are offline. Wouldn't that be the reason the commit does not get called? – Francois Nadeau Jan 31 '19 at 16:15
  • `commit` will be called only after the server returns a positive response. What happens when you put your device to `online`? – Prasun Jan 31 '19 at 17:09
  • @FrancoisNadeau , i'am testing this while my device is online, and its the same issue, commit action don't get called. – Hamdi Gatri Feb 03 '19 at 17:51
  • @Prasun my device (emulator) is online and commit action don't get dispatched – Hamdi Gatri Feb 03 '19 at 17:52
  • Ok, did you check `redux-logger`, is there any logging like `Offline/SCHEDULE_RETRY`? It might happen that your request is failing and `redux-offline` is re-scheduling based on the default `retry` strategy. – Prasun Feb 04 '19 at 03:51

0 Answers0