Skip to content

Commit

Permalink
added: a button to manually re-connect to centrifugo
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-8 committed Nov 21, 2024
1 parent c8770bb commit d6ad3a8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"CALCULATING_YOUR_POS": "Determining your position",
"TRACKING_YOUR_POS": "Tracking your position",
"CONN_LOST": "Connection lost! Reconnecting",
"CONN_LOST_IDLE": "Connection lost!",
"AUTHENTICATING": "Authenticating",
"REFUSE": "Refuse",
"ACCEPT": "Accept",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"CALCULATING_YOUR_POS": "Cálculando tu posición",
"TRACKING_YOUR_POS": "Seguimiento de su posición",
"CONN_LOST": "Conexión perdida, reconexión",
"CONN_LOST_IDLE": "Conexión perdida",
"AUTHENTICATING": "Autenticando",
"REFUSE": "Rechazar",
"ACCEPT": "Acceptar",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"CALCULATING_YOUR_POS": "Calcul de votre position",
"TRACKING_YOUR_POS": "Suivi de votre position",
"CONN_LOST": "Connexion perdue, reconnexion",
"CONN_LOST_IDLE": "Connexion perdue",
"AUTHENTICATING": "Authentification…",
"REFUSE": "Refuser",
"ACCEPT": "Accepter",
Expand Down
3 changes: 1 addition & 2 deletions src/navigation/restaurant/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default function DashboardPage({ navigation, route }) {
state => state.app.isInternetReachable,
);
const isLoading = useSelector(selectIsLoading);
const isCentrifugoConnected = useSelector(selectIsCentrifugoConnected);

const { navigate } = navigation;

Expand Down Expand Up @@ -171,7 +170,7 @@ export default function DashboardPage({ navigation, route }) {
}
/>
)}
<WebSocketIndicator connected={isCentrifugoConnected} />
<WebSocketIndicator />
<OrdersToPrintQueue />
<DatePickerHeader
date={date}
Expand Down
60 changes: 45 additions & 15 deletions src/navigation/restaurant/components/WebSocketIndicator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
import { Text } from 'native-base';
import { Button, Text } from 'native-base';
import React from 'react';
import { withTranslation } from 'react-i18next';
import { useTranslation } from 'react-i18next';
import { ActivityIndicator, StyleSheet, View } from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import {
selectIsCentrifugoConnected,
selectIsCentrifugoConnecting,
} from '../../../redux/App/selectors';
import { connect } from '../../../redux/middlewares/CentrifugoMiddleware/actions';

const WebSocketIndicator = ({ connected, t }) => (
<View
style={[
styles.container,
connected ? styles.connected : styles.disconnected,
]}>
<Text style={styles.text}>
{connected ? t('WAITING_FOR_ORDER') : t('CONN_LOST')}
</Text>
<ActivityIndicator size="small" color="white" animating={connected} />
</View>
);
const WebSocketIndicator = () => {
const connecting = useSelector(selectIsCentrifugoConnecting);
const connected = useSelector(selectIsCentrifugoConnected);

const dispatch = useDispatch();

const { t } = useTranslation();

return (
<View
style={[
styles.container,
connected ? styles.connected : styles.disconnected,
]}>
<Text style={styles.text}>
{connected
? t('WAITING_FOR_ORDER')
: connecting
? t('CONN_LOST')
: t('CONN_LOST_IDLE')}
</Text>
{connected ? (
<ActivityIndicator size="small" color="white" animating={true} />
) : null}
{!connected && !connecting ? (
<Button
variant="link"
onPress={() => {
dispatch(connect());
}}>
<Text style={styles.text}>{t('RETRY')}</Text>
</Button>
) : null}
</View>
);
};

const styles = StyleSheet.create({
container: {
Expand All @@ -41,4 +71,4 @@ const styles = StyleSheet.create({
},
});

export default withTranslation()(WebSocketIndicator);
export default WebSocketIndicator;
15 changes: 15 additions & 0 deletions src/redux/App/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {

import { EVENT as EVENT_ORDER } from '../../domain/Order';
import { EVENT as EVENT_TASK_COLLECTION } from '../../domain/TaskCollection';
import { connect } from '../middlewares/CentrifugoMiddleware/actions';

const initialState = {
customBuild: !!Config.DEFAULT_SERVER,
Expand Down Expand Up @@ -93,6 +94,7 @@ const initialState = {
loginByEmailErrors: {},
isBackgroundGeolocationEnabled: false,
hasDisclosedBackgroundPermission: false,
isCentrifugoConnecting: false,
isCentrifugoConnected: false,
modal: {
show: false,
Expand Down Expand Up @@ -168,15 +170,28 @@ export default (state = initialState, action = {}) => {
loading: action.payload,
};

case connect.type: {
if (state.isCentrifugoConnected) {
return state;
}

return {
...state,
isCentrifugoConnecting: true,
};
}

case connected.type:
return {
...state,
isCentrifugoConnecting: false,
isCentrifugoConnected: true,
};

case disconnected.type:
return {
...state,
isCentrifugoConnecting: false,
isCentrifugoConnected: false,
};

Expand Down
3 changes: 3 additions & 0 deletions src/redux/App/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export const selectIsLoading = createSelector(
},
);

export const selectIsCentrifugoConnecting = state =>
state.app.isCentrifugoConnecting;

export const selectIsCentrifugoConnected = state =>
state.app.isCentrifugoConnected;

Expand Down

0 comments on commit d6ad3a8

Please sign in to comment.