onBackClick()}>
-
+
(back ? onBackClick() : null)}>
+ {back ?
: ''}
+
onBackClick()}>
+ {' '}
+ {serverOnline ? 'En ligne' : 'Hors ligne'}
- ) : (
- ''
- )}
+
window.location.reload()}>
{time} - {name}
diff --git a/src/reducers/login.ts b/src/reducers/login.ts
index 5e93a47..7270583 100644
--- a/src/reducers/login.ts
+++ b/src/reducers/login.ts
@@ -3,12 +3,13 @@ import { toast } from 'react-toastify';
import { Socket } from '../utils/socket';
import { clearOrders, setOrders } from './orders';
import { clearBasket } from './basket';
-import { LoginState, User, Action, Dispatch } from '../types';
+import { LoginState, User, Action, Dispatch, ApiLoginResponse } from '../types';
import { clearPromotions, setPromotions } from './promotions';
import { getOrders } from '../utils/orders';
import { getCategories } from '../utils/categories';
import { setCategories } from './categories';
import { getPromotions } from '../utils/promotions';
+import { setServerOffline, setServerOnline } from './server';
const initialState: LoginState = {
token: null,
@@ -105,10 +106,11 @@ export const autoLogin = () => async (dispatch: Dispatch) => {
const oldToken: string = localStorage.getItem(BOUFFE_TOKEN);
try {
- const res = await API.post
('/auth/refreshToken', { token: oldToken });
- const { token, name, key } = res.data;
+ const res = await API.post('/auth/refreshToken', { token: oldToken });
+ const { token, name, key, isOnline } = res.data;
dispatch(setUser({ token, name, key }));
+ dispatch(isOnline ? setServerOnline() : setServerOffline());
dispatch(fetchData());
} catch (err) {
dispatch(logout());
@@ -120,10 +122,12 @@ export const autoLogin = () => async (dispatch: Dispatch) => {
};
export const tryLogin = (pin: string) => async (dispatch: Dispatch) => {
- const res = await API.post(`/auth/login`, { pin });
- const { token, name, key } = res.data;
+ const res = await API.post(`/auth/login`, { pin });
+ const { token, name, key, isOnline } = res.data;
toast.success('Connexion validée');
dispatch(setLoading(true));
dispatch(setUser({ token, name, key }));
+ dispatch(isOnline ? setServerOnline() : setServerOffline());
dispatch(fetchData());
};
+
diff --git a/src/reducers/server.ts b/src/reducers/server.ts
index 1c26c96..67c6bf0 100644
--- a/src/reducers/server.ts
+++ b/src/reducers/server.ts
@@ -2,10 +2,13 @@ import { Action, ServerState } from '../types';
const initialState: ServerState = {
socketConnected: false,
+ internetConnected: false,
};
export const SET_SOCKET_CONNECTED = 'SET_SOCKET_CONNECTED';
export const SET_SOCKET_DISCONNECTED = 'SET_SOCKET_DISCONNECTED';
+export const SET_SERVER_ONLINE = 'SET_SERVER_ONLINE';
+export const SET_SERVER_OFFLINE = 'SET_SERVER_OFFLINE';
export default (state = initialState, action: Action) => {
switch (action.type) {
@@ -21,6 +24,17 @@ export default (state = initialState, action: Action) => {
socketConnected: false,
};
+ case SET_SERVER_ONLINE:
+ return {
+ ...state,
+ internetConnected: true,
+ };
+
+ case SET_SERVER_OFFLINE:
+ return {
+ ...state,
+ internetConnected: false,
+ };
default:
return state;
}
@@ -33,3 +47,11 @@ export const setSocketConnected = () => ({
export const setSocketDisconnected = () => ({
type: SET_SOCKET_DISCONNECTED,
});
+
+export const setServerOnline = () => ({
+ type: SET_SERVER_ONLINE,
+});
+
+export const setServerOffline = () => ({
+ type: SET_SERVER_OFFLINE,
+});
diff --git a/src/types.ts b/src/types.ts
index 808ec92..5f5e09a 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -60,6 +60,10 @@ export interface User {
key: string;
}
+export interface ApiLoginResponse extends User {
+ isOnline: boolean;
+}
+
export interface Promotion extends Price {
name: string;
key: string;
@@ -109,4 +113,5 @@ export interface LoginState extends User {
export interface ServerState {
socketConnected: boolean;
+ internetConnected: boolean;
}
diff --git a/src/utils/socket.ts b/src/utils/socket.ts
index 4b83ae1..064d72a 100644
--- a/src/utils/socket.ts
+++ b/src/utils/socket.ts
@@ -3,7 +3,7 @@ import { setOrders } from '../reducers/orders';
import { toast } from 'react-toastify';
import { Order, Category, Dispatch } from '../types';
import { setCategories } from '../reducers/categories';
-import { setSocketDisconnected, setSocketConnected } from '../reducers/server';
+import { setSocketDisconnected, setSocketConnected, setServerOnline, setServerOffline } from '../reducers/server';
let socket: ClientSocket | undefined = undefined;
@@ -22,6 +22,15 @@ export const Socket = {
dispatch(setCategories(categories));
});
+ socket.on('networkStatus', ({ online }) => {
+ dispatch(online ? setServerOnline() : setServerOffline());
+ if (!online) {
+ toast.warning("Turbobouffe a été deconnecté d'internet. Passage en mode hors ligne", { autoClose: false });
+ } else {
+ toast.info('Turbobouff est de nouveau en ligne !', { autoClose: false });
+ }
+ });
+
socket.on('disconnect', (reason: string) => {
if (reason === 'transport close' || reason === 'ping timeout') {
toast.error('Extinction du serveur...');