Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Disable fetch calls when offline (#522)
Browse files Browse the repository at this point in the history
* Disable fetch calls when offline

* Move online check to doFetchWithResponse
  • Loading branch information
csduarte authored and enahum committed Jun 7, 2018
1 parent bdbe023 commit 8870c69
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/client/client4.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class Client4 {
this.userId = '';
this.diagnosticId = '';
this.includeCookies = true;
this.online = true;

this.translations = {
connectionError: 'There appears to be a problem with your internet connection.',
Expand All @@ -49,6 +50,10 @@ export default class Client4 {
this.url = url;
}

setOnline(online) {
this.online = online;
}

setUserAgent(userAgent) {
this.userAgent = userAgent;
}
Expand Down Expand Up @@ -2394,6 +2399,13 @@ export default class Client4 {
};

doFetchWithResponse = async (url, options) => {
if (!this.online) {
throw {
message: 'no internet connection',
url,
};
}

const response = await fetch(url, this.getOptions(options));
const headers = parseAndMergeNestedHeaders(response.headers);

Expand Down
2 changes: 2 additions & 0 deletions src/store/configureStore.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {REHYDRATE} from 'redux-persist/constants';
import {createOfflineReducer, networkStatusChangedAction, offlineCompose} from 'redux-offline';
import defaultOfflineConfig from 'redux-offline/lib/defaults';
import createActionBuffer from 'redux-action-buffer';
import {Client4} from 'client';

const devToolsEnhancer = (
typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__ ? // eslint-disable-line no-underscore-dangle
Expand Down Expand Up @@ -74,6 +75,7 @@ export default function configureServiceStore(preloadedState, appReducer, userOf

if (baseOfflineConfig.detectNetwork) {
baseOfflineConfig.detectNetwork((online) => {
Client4.setOnline(online);
store.dispatch(networkStatusChangedAction(online));
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/store/configureStore.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {REHYDRATE} from 'redux-persist/constants';
import {createOfflineReducer, networkStatusChangedAction, offlineCompose} from 'redux-offline';
import defaultOfflineConfig from 'redux-offline/lib/defaults';
import createActionBuffer from 'redux-action-buffer';
import {Client4} from 'client';

import {General} from 'constants';
import serviceReducer from 'reducers';
Expand Down Expand Up @@ -68,6 +69,7 @@ export default function configureOfflineServiceStore(preloadedState, appReducer,

if (baseOfflineConfig.detectNetwork) {
baseOfflineConfig.detectNetwork((online) => {
Client4.setOnline(online);
store.dispatch(networkStatusChangedAction(online));
});
}
Expand Down

0 comments on commit 8870c69

Please sign in to comment.