Skip to content

Commit

Permalink
Merge pull request #1596 from tomivm/fix/offline-alerts
Browse files Browse the repository at this point in the history
Fix/connection setter
  • Loading branch information
martinbedouret authored Nov 18, 2023
2 parents 312e244 + b8602ba commit df0820e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/components/App/App.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import {
DISABLE_TOUR,
ENABLE_ALL_TOURS,
SET_UNLOGGED_USER_LOCATION,
UPDATE_SYMBOLS_SETTINGS
UPDATE_SYMBOLS_SETTINGS,
UPDATE_CONNECTIVITY
} from './App.constants';

import { updateIsInFreeCountry } from '../../providers/SubscriptionProvider/SubscriptionProvider.actions';

export function updateConnectivity({ isConnected = false }) {
return {
type: UPDATE_CONNECTIVITY,
payload: isConnected
};
}

export function updateDisplaySettings(payload = {}) {
return {
type: UPDATE_DISPLAY_SETTINGS,
Expand Down
36 changes: 34 additions & 2 deletions src/components/App/App.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { DISPLAY_SIZE_STANDARD } from '../Settings/Display/Display.constants';
import {
updateUserDataFromAPI,
updateLoggedUserLocation,
updateUnloggedUserLocation
updateUnloggedUserLocation,
updateConnectivity
} from '../App/App.actions';
import { isCordova, isElectron } from '../../cordova-util';
export class AppContainer extends Component {
Expand Down Expand Up @@ -85,6 +86,36 @@ export class AppContainer extends Component {
localizeUser();

if (isCordova()) initCVAGa4();

const configureConnectionStatus = () => {
const { updateConnectivity } = this.props;
const setAsOnline = () => {
updateConnectivity({ isConnected: true });
};

const setAsOffline = () => {
updateConnectivity({ isConnected: false });
};

const addConnectionEventListeners = () => {
window.addEventListener('offline', setAsOffline);
window.addEventListener('online', setAsOnline);
};

const setCurrentConnectionStatus = () => {
if (!navigator.onLine) {
setAsOffline();
return;
}
setAsOnline();
return;
};

setCurrentConnectionStatus();
addConnectionEventListeners();
};

configureConnectionStatus();
}

handleNewContentAvailable = () => {
Expand Down Expand Up @@ -148,7 +179,8 @@ const mapDispatchToProps = {
showNotification,
updateUserDataFromAPI,
updateLoggedUserLocation,
updateUnloggedUserLocation
updateUnloggedUserLocation,
updateConnectivity
};

export default connect(
Expand Down
16 changes: 0 additions & 16 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { persistStore } from 'redux-persist';
import { UPDATE_CONNECTIVITY } from './components/App/App.constants';
import googleAnalytics from './analytics';
import createReducer from './reducers';

Expand All @@ -26,21 +25,6 @@ export default function configureStore(initialState = {}) {
composeEnhancers(...enhancers)
);

// TODO refactor not here
window.addEventListener('offline', () => {
store.dispatch({
type: UPDATE_CONNECTIVITY,
payload: false
});
});

window.addEventListener('online', () => {
store.dispatch({
type: UPDATE_CONNECTIVITY,
payload: true
});
});

const persistor = persistStore(store);

return { persistor, store };
Expand Down

0 comments on commit df0820e

Please sign in to comment.