Search code examples
angularreduxredux-offline

Redux state is missing "offline" part from redux offline


I'm using Redux Offline in my Angular project, and now I want to enable cancelling an item in the outbox while in offline mode. Basically, I just want to filter on the outbox array to remove items I want to cancel/modify.

My problem is it is missing (undefined) from the state I receive in the top level reducer. I can see offline in the Redux Dev tool, and I can also get it via NgRedux.select() and NgRedux.getState().

Is there a reason why redux offline is not present in the state received in the reducers? Can I add it, and then change it via reducers? Or is there another to access and modify the outbox?

package.json:

"dependencies": {
 "@angular-redux/router": "^7.0.0",
 "@angular-redux/store": "^9.0.0",
 "@angular/animations": "^6.0.3",
 "@angular/common": "^6.0.3",
 "@angular/compiler": "^6.0.3",
 "@angular/core": "^6.0.3",
 "@angular/forms": "^6.0.3",
 "@angular/http": "^6.0.3",
 "@angular/platform-browser": "^6.0.3",
 "@angular/platform-browser-dynamic": "^6.0.3",
 "@angular/pwa": "^0.6.7",
 "@angular/router": "^6.0.3",
 "@angular/service-worker": "^6.0.3",
 "@redux-offline/redux-offline": "^2.3.3",
 "core-js": "^2.5.7",
 "reduce-reducers": "^0.3.0",
 "redux": "^4.0.0",
 "redux-observable": "1.0.0-alpha.2",
 "rxjs": "^6.2.0",
 "rxjs-compat": "^6.2.0",
 "zone.js": "^0.8.26"
},
"devDependencies": {
 "redux-devtools-extension": "^2.13.2",
 "@angular-devkit/build-angular": "^0.6.7",
 "@angular/cli": "^6.0.7",
 "@angular/compiler-cli": "^6.0.3",
 "@angular/language-service": "^6.0.3",
 "@types/jasmine": "^2.8.8",
 "@types/jasminewd2": "~2.0.3",
 "@types/node": "^8.10.18",
 "codelyzer": "^4.3.0",
 "jasmine-core": "~2.99.1",
 "jasmine-spec-reporter": "~4.2.1",
 "karma": "~1.7.1",
 "karma-chrome-launcher": "~2.2.0",
 "karma-coverage-istanbul-reporter": "^1.4.3",
 "karma-jasmine": "^1.1.2",
 "karma-jasmine-html-reporter": "^0.2.2",
 "opn-cli": "^3.1.0",
 "protractor": "^5.3.2",
 "ts-node": "~5.0.1",
 "tslint": "~5.9.1",
 "typescript": "~2.7.2"
}

Please write a comment if you need any more information.


Solution

  • redux-offline injects it's own reducer dynamically so you'll not be able to access it via your top level reducers. redux-offline exposes it's queue via the config which will allow you to update the outbox to remove/add actions. If you want to remove an offline action whilst the user is offline I'd suggest making an offline action call with a specific action type (e.g. 'CANCEL_OFFLINE_ACTION') and handling it in queue.enqueue to remove the action in the outbox that you don't want to propagate.

    Here's a rough example:

    // the action to dispatch
    const action = {
      type: 'CANCEL_OFFLINE_ACTION',
      payload: {
        id: 123,
      },
      meta: {
        offline: {
          effect: {
            url: ''
          },
        },
      },
    };
    
    // the custom enqueue action
    function enqueue(outbox, incomingAction) {
      return incomingAction.type !== 'CANCEL_OFFLINE_ACTION' ? [...outbox, incomingAction] :
        outbox.filter(
          outboxAction => outboxAction.payload.id !== incomingAction.payload.id,
        );
    }