Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add snackbar on maintenance message #1902

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ import { getComputedLanguage } from '../utils/language';
import AppTopBar from './app-top-bar';
import { StudyContainer } from './study-container';
import { fetchValidateUser } from '../services/user-admin';
import { connectNotificationsWsUpdateConfig } from '../services/config-notification';
import {
connectGlobalNotificationsWs,
connectNotificationsWsUpdateConfig,
} from '../services/config-notification';
import {
fetchConfigParameter,
fetchConfigParameters,
Expand All @@ -105,6 +108,7 @@ import {
} from '../services/utils';
import { getOptionalServices } from '../services/study';
import { defaultOptionalServicesState } from 'redux/reducer';
import { closeSnackbar, enqueueSnackbar } from 'notistack';

const noUserManager = { instance: null, error: null };

Expand Down Expand Up @@ -141,6 +145,12 @@ const App = () => {

const [tabIndex, setTabIndex] = useState(0);

const [maintenanceSnackBarId, setMaintenanceSnackBarId] = useState(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happend if we recieve a second message before the first one expire ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now the previous msg is cancelled when receiving a new one


const HEADER_MAINTENANCE = 'maintenance';

const HEADER_CANCEL_MAINTENANCE = 'cancelMaintenance';

const updateParams = useCallback(
(params) => {
console.debug('received UI parameters : ', params);
Expand Down Expand Up @@ -380,6 +390,53 @@ const App = () => {
return ws;
}, [updateParams, snackError, dispatch]);

const connectGlobalNotifications = useCallback(() => {
const ws = connectGlobalNotificationsWs();
ws.onmessage = function (event) {
let eventData = JSON.parse(event.data);
if (eventData.headers.messageType === HEADER_MAINTENANCE) {
if (eventData.headers.duration) {
setMaintenanceSnackBarId(
enqueueSnackbar(eventData.payload, {
autoHideDuration: eventData.headers.duration * 1000,
variant: 'info',
style: {
whiteSpace: 'pre-line',
},
anchorOrigin: {
vertical: 'top',
horizontal: 'center',
},
})
);
} else {
setMaintenanceSnackBarId(
enqueueSnackbar(eventData.payload, {
variant: 'info',
persist: true,
style: {
whiteSpace: 'pre-line',
},
anchorOrigin: {
vertical: 'top',
horizontal: 'center',
},
})
);
}
} else if (
eventData.headers.messageType === HEADER_CANCEL_MAINTENANCE
) {
//nothing happens if the id is null or if the snackbar it references is already closed
closeSnackbar(maintenanceSnackBarId);
}
};
ws.onerror = function (event) {
console.error('Unexpected Notification WebSocket error', event);
};
return ws;
}, [maintenanceSnackBarId]);

// Can't use lazy initializer because useRouteMatch is a hook
const [initialMatchSilentRenewCallbackUrl] = useState(
useMatch({
Expand Down Expand Up @@ -525,15 +582,18 @@ const App = () => {
);

const ws = connectNotificationsUpdateConfig();
const ws2 = connectGlobalNotifications();
return function () {
ws.close();
ws2.close();
};
}
}, [
user,
dispatch,
updateParams,
connectNotificationsUpdateConfig,
connectGlobalNotifications,
snackError,
]);

Expand Down
16 changes: 16 additions & 0 deletions src/services/config-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ export function connectNotificationsWsUpdateConfig() {
};
return reconnectingWebSocket;
}

export function connectGlobalNotificationsWs() {
const webSocketBaseUrl = getWsBase();
const webSocketUrl =
webSocketBaseUrl + PREFIX_CONFIG_NOTIFICATION_WS + '/global';

const reconnectingWebSocket = new ReconnectingWebSocket(() =>
getUrlWithToken(webSocketUrl)
);
reconnectingWebSocket.onopen = function () {
console.info(
'Connected Websocket for global messages ' + webSocketUrl + ' ...'
);
};
return reconnectingWebSocket;
}
Loading