From e49456490ce156f0f229be7297813098a3b14062 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Thu, 7 Jan 2021 14:59:40 +0100 Subject: [PATCH] Move all notifications to NotificationFactory --- src/common/api/rest-api.js | 15 +-- src/common/api/websocket-api.js | 11 +- src/common/array-store.js | 20 +-- .../data-managers/notifications-factory.js | 118 ++++++++++++++++-- src/ic/ic-store.js | 10 +- src/scenario/scenario-store.js | 10 +- src/signal/signal-store.js | 11 +- src/signal/signals-data-manager.js | 41 ++---- src/user/user.js | 15 +-- src/user/users-store.js | 17 +-- src/user/users.js | 9 +- 11 files changed, 150 insertions(+), 127 deletions(-) diff --git a/src/common/api/rest-api.js b/src/common/api/rest-api.js index 084456c..ecda441 100644 --- a/src/common/api/rest-api.js +++ b/src/common/api/rest-api.js @@ -18,19 +18,8 @@ import request from 'superagent/lib/client'; import Promise from 'es6-promise'; import NotificationsDataManager from '../data-managers/notifications-data-manager'; +import NotificationsFactory from "../data-managers/notifications-factory"; -// TODO: Add this to a central pool of notifications -const SERVER_NOT_REACHABLE_NOTIFICATION = { - title: 'Server not reachable', - message: 'The server could not be reached. Please try again later.', - level: 'error' - }; - -const REQUEST_TIMEOUT_NOTIFICATION = { - title: 'Request timeout', - message: 'Request timed out. Please try again later.', - level: 'error' - }; // Check if the error was due to network failure, timeouts, etc. // Can be used for the rest of requests @@ -41,7 +30,7 @@ function isNetworkError(err) { if (err.status == null || err.status === 500 || err.response == null) { result = true; - let notification = err.timeout? REQUEST_TIMEOUT_NOTIFICATION : SERVER_NOT_REACHABLE_NOTIFICATION; + let notification = err.timeout? NotificationsFactory.REQUEST_TIMEOUT : NotificationsFactory.SERVER_NOT_REACHABLE; NotificationsDataManager.addNotification(notification); } return result; diff --git a/src/common/api/websocket-api.js b/src/common/api/websocket-api.js index 1462f39..e55290a 100644 --- a/src/common/api/websocket-api.js +++ b/src/common/api/websocket-api.js @@ -15,6 +15,7 @@ * along with VILLASweb. If not, see . ******************************************************************************/ import NotificationsDataManager from "../data-managers/notifications-data-manager"; +import NotificationsFactory from "../data-managers/notifications-factory"; import AppDispatcher from '../app-dispatcher'; class WebsocketAPI { @@ -70,7 +71,7 @@ class WebsocketAPI { AppDispatcher.dispatch({ type: 'websocket/connected', data: this.websocketurl, - }); + }); this.wasConnected = true; if ('onOpen' in this.callbacks) @@ -88,12 +89,8 @@ class WebsocketAPI { type: 'websocket/connection-error', data: this.websocketurl, }); - const IC_WEBSOCKET_CONNECTION_ERROR = { - title: 'Websocket connection warning', - message: "Connection to " + this.websocketurl + " dropped. Attempt reconnect in 1 sec", - level: 'warning' - }; - NotificationsDataManager.addNotification(IC_WEBSOCKET_CONNECTION_ERROR); + + NotificationsDataManager.addNotification(NotificationsFactory.WEBSOCKET_CONNECTION_WARN(this.websocketurl)); console.log("Connection to " + this.websocketurl + " dropped. Attempt reconnect in 1 sec"); window.setTimeout(() => { this.reconnect(); }, 1000); } diff --git a/src/common/array-store.js b/src/common/array-store.js index 1ab4b7f..ff9238c 100644 --- a/src/common/array-store.js +++ b/src/common/array-store.js @@ -19,6 +19,7 @@ import { ReduceStore } from 'flux/utils'; import AppDispatcher from './app-dispatcher'; import NotificationsDataManager from '../common/data-managers/notifications-data-manager'; +import NotificationsFactory from "./data-managers/notifications-factory"; class ArrayStore extends ReduceStore { constructor(type, dataManager) { @@ -83,13 +84,7 @@ class ArrayStore extends ReduceStore { case this.type + '/load-error': if (action.error && !action.error.handled && action.error.response) { - const USER_LOAD_ERROR_NOTIFICATION = { - title: 'Failed to load', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(USER_LOAD_ERROR_NOTIFICATION); - + NotificationsDataManager.addNotification(NotificationsFactory.LOAD_ERROR(action.error.response.body.message)); } return super.reduce(state, action); @@ -120,17 +115,10 @@ class ArrayStore extends ReduceStore { return (item.id !== action.data); }); } - + case this.type + '/remove-error': if (action.error && !action.error.handled && action.error.response) { - - const USER_REMOVE_ERROR_NOTIFICATION = { - title: 'Failed to add remove ', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(USER_REMOVE_ERROR_NOTIFICATION); - + NotificationsDataManager.addNotification(NotificationsFactory.DELETE_ERROR(action.error.response.body.message)); } return super.reduce(state, action); diff --git a/src/common/data-managers/notifications-factory.js b/src/common/data-managers/notifications-factory.js index a23768f..325815c 100644 --- a/src/common/data-managers/notifications-factory.js +++ b/src/common/data-managers/notifications-factory.js @@ -18,13 +18,117 @@ class NotificationsFactory { // This is an example - static get EXAMPLE_NOTIFICATION() { - return { - title: 'Example notification', - message: 'Write something here that describes what happend.', - level: 'warning' - }; - } + static get EXAMPLE_NOTIFICATION() { + return { + title: 'Example notification', + message: 'Write something here that describes what happend.', + level: 'warning' + }; + } + + static get SERVER_NOT_REACHABLE() { + return { + title: 'Server not reachable', + message: 'The server could not be reached. Please try again later.', + level: 'error' + }; + } + + static get REQUEST_TIMEOUT() { + return { + title: 'Request timeout', + message: 'Request timed out. Please try again later.', + level: 'error' + }; + } + + static ADD_ERROR(message) { + return { + title: "Add Error", + message: message, + level: 'error' + }; + } + + static UPDATE_ERROR(message) { + return { + title: 'Update Error ', + message: message, + level: 'error' + }; + } + + static UPDATE_WARNING(message) { + return { + title: 'Update Warning ', + message: message, + level: 'warning' + }; + } + + static LOAD_ERROR(message) { + return { + title: 'Failed to load', + message: message, + level: 'error' + }; + } + + static DELETE_ERROR(message) { + return { + title: 'Failed to delete ', + message: message, + level: 'error' + }; + } + + static WEBSOCKET_CONNECTION_WARN(websocket_url) { + return { + title: 'Websocket connection warning', + message: "Connection to " + websocket_url + " dropped. Attempt reconnect in 1 sec", + level: 'warning' + }; + } + + static WEBSOCKET_URL_WARN(ic_name, ic_uuid) { + return { + title: 'Websocket connection warning', + message: "Websocket URL parameter not available for IC " + ic_name + "(" + ic_uuid + "), connection not possible", + level: 'warning' + }; + } + + static SCENARIO_USERS_ERROR(message) { + return { + title: 'Failed to modify scenario users ', + message: message, + level: 'error' + }; + } + + static AUTOCONF_INFO() { + return { + title: 'Auto-configuration info', + message: 'Signal configuration loaded successfully.', + level: 'info' + }; + } + + static AUTOCONF_WARN(message) { + return { + title: 'Auto-configuration warning', + message: message, + level: 'warning' + }; + } + + static AUTOCONF_ERROR(message) { + return { + title: 'Auto-configuration error', + message: message, + level: 'error' + }; + } } diff --git a/src/ic/ic-store.js b/src/ic/ic-store.js index d73e555..456aba8 100644 --- a/src/ic/ic-store.js +++ b/src/ic/ic-store.js @@ -19,6 +19,7 @@ import ArrayStore from '../common/array-store'; import ICsDataManager from './ics-data-manager'; import ICDataDataManager from './ic-data-data-manager'; import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class InfrastructureComponentStore extends ArrayStore { constructor() { @@ -47,14 +48,7 @@ class InfrastructureComponentStore extends ArrayStore { if (ic.websocketurl != null && ic.websocketurl !== '') { ICDataDataManager.open(ic.websocketurl, ic.id); } else { - - // TODO add to pool of notifications - const IC_WEBSOCKET_URL_ERROR = { - title: 'Websocket connection warning', - message: "Websocket URL parameter not available for IC " + ic.name + "(" + ic.uuid + "), connection not possible", - level: 'warning' - }; - NotificationsDataManager.addNotification(IC_WEBSOCKET_URL_ERROR); + NotificationsDataManager.addNotification(NotificationsFactory.WEBSOCKET_URL_WARN(ic.name, ic.uuid)); } } return super.reduce(state, action); diff --git a/src/scenario/scenario-store.js b/src/scenario/scenario-store.js index 24b04cf..f88625c 100644 --- a/src/scenario/scenario-store.js +++ b/src/scenario/scenario-store.js @@ -19,6 +19,7 @@ import ScenariosDataManager from './scenarios-data-manager'; import ArrayStore from '../common/array-store'; import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class ScenarioStore extends ArrayStore{ @@ -89,14 +90,7 @@ class ScenarioStore extends ArrayStore{ case 'scenarios/users-error': if (action.error && !action.error.handled && action.error.response) { - - const SCENARIO_USERS_ERROR_NOTIFICATION = { - title: 'Failed to modify scenario users ', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(SCENARIO_USERS_ERROR_NOTIFICATION); - + NotificationsDataManager.addNotification(NotificationsFactory.SCENARIO_USERS_ERROR(action.error.response.body.message)); } return super.reduce(state, action); diff --git a/src/signal/signal-store.js b/src/signal/signal-store.js index 82ce6bf..175467c 100644 --- a/src/signal/signal-store.js +++ b/src/signal/signal-store.js @@ -18,6 +18,7 @@ import ArrayStore from '../common/array-store'; import SignalsDataManager from './signals-data-manager' import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class SignalStore extends ArrayStore{ constructor() { @@ -45,14 +46,8 @@ class SignalStore extends ArrayStore{ case 'signals/autoconfig-error': if (action.error && !action.error.handled && action.error.response) { - - const SIGNAL_AUTOCONF_ERROR_NOTIFICATION = { - title: 'Failed to load signal config ', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_ERROR_NOTIFICATION); - + NotificationsDataManager.addNotification( + NotificationsFactory.AUTOCONF_ERROR(action.error.response.body.message)); } return super.reduce(state, action); diff --git a/src/signal/signals-data-manager.js b/src/signal/signals-data-manager.js index 9d61584..3cc6493 100644 --- a/src/signal/signals-data-manager.js +++ b/src/signal/signals-data-manager.js @@ -19,6 +19,7 @@ import RestDataManager from '../common/data-managers/rest-data-manager'; import RestAPI from "../common/api/rest-api"; import AppDispatcher from "../common/app-dispatcher"; import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class SignalsDataManager extends RestDataManager{ @@ -62,12 +63,8 @@ class SignalsDataManager extends RestDataManager{ saveSignals(nodes, token, configID, socketname){ if(nodes.length === 0){ - const SIGNAL_AUTOCONF_ERROR_NOTIFICATION = { - title: 'Failed to load nodes ', - message: 'VILLASnode returned empty response', - level: 'error' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.AUTOCONF_ERROR('Failed to load nodes, VILLASnode returned empty response')); return; } @@ -79,12 +76,9 @@ class SignalsDataManager extends RestDataManager{ console.warn("Could not parse the following node config because it lacks a name parameter:", nodeConfig); } else if(nodeConfig.name === socketname){ if(configured){ - const SIGNAL_AUTOCONF_WARNING_NOTIFICATION = { - title: 'There might be a problem with the signal auto-config', - message: 'VILLASnode returned multiple node configurations for the websocket ' + socketname + '. This is a problem of the VILLASnode.', - level: 'warning' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_WARNING_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.AUTOCONF_WARN('VILLASnode returned multiple node configurations for the websocket ' + + socketname + '. This is a problem of the VILLASnode.')); continue; } // signals are not yet configured: @@ -92,12 +86,8 @@ class SignalsDataManager extends RestDataManager{ let index_out = 1 if(!nodeConfig.in.hasOwnProperty("signals")){ - const SIGNAL_AUTOCONF_ERROR_NOTIFICATION = { - title: 'Failed to load in signal config ', - message: 'No field for in signals contained in response.', - level: 'error' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.AUTOCONF_ERROR('Failed to load in signal config, no field for in signals contained in response.')); error = true; } else{ @@ -128,12 +118,8 @@ class SignalsDataManager extends RestDataManager{ } if(!nodeConfig.out.hasOwnProperty("signals")){ - const SIGNAL_AUTOCONF_ERROR_NOTIFICATION = { - title: 'Failed to load out signal config ', - message: 'No field for out signals contained in response.', - level: 'error' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.AUTOCONF_ERROR('Failed to load out signal config, no field for out signals contained in response.')); error=true; }else { @@ -173,12 +159,7 @@ class SignalsDataManager extends RestDataManager{ } if(!error) { - const SIGNAL_AUTOCONF_INFO_NOTIFICATION = { - title: 'Signal configuration loaded successfully.', - message: '', - level: 'info' - }; - NotificationsDataManager.addNotification(SIGNAL_AUTOCONF_INFO_NOTIFICATION); + NotificationsDataManager.addNotification(NotificationsFactory.AUTOCONF_INFO()); } } diff --git a/src/user/user.js b/src/user/user.js index 850b663..2026771 100644 --- a/src/user/user.js +++ b/src/user/user.js @@ -25,6 +25,7 @@ import UsersStore from './users-store'; import Icon from '../common/icon'; import EditOwnUserDialog from './edit-own-user' import NotificationsDataManager from "../common/data-managers/notifications-data-manager" +import NotificationsFactory from "../common/data-managers/notifications-factory"; class User extends Component { static getStores() { @@ -81,12 +82,7 @@ class User extends Component { updatedData.password = data.password; updatedData.oldPassword = data.oldPassword; } else if (data.password !== '' && data.password !== data.confirmpassword) { - const USER_UPDATE_ERROR_NOTIFICATION = { - title: 'Update Error ', - message: 'New password not correctly confirmed', - level: 'error' - }; - NotificationsDataManager.addNotification(USER_UPDATE_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification(NotificationsFactory.UPDATE_ERROR('New password not correctly confirmed')); return } @@ -102,12 +98,7 @@ class User extends Component { token: this.state.token }); } else { - const USER_UPDATE_WARNING_NOTIFICATION = { - title: 'Update Warning ', - message: 'No update requested, no input data', - level: 'warning' - }; - NotificationsDataManager.addNotification(USER_UPDATE_WARNING_NOTIFICATION); + NotificationsDataManager.addNotification(NotificationsFactory.UPDATE_WARNING('No update requested, no input data')); } } } diff --git a/src/user/users-store.js b/src/user/users-store.js index dd155b9..8bda26a 100644 --- a/src/user/users-store.js +++ b/src/user/users-store.js @@ -18,6 +18,7 @@ import ArrayStore from '../common/array-store'; import UsersDataManager from './users-data-manager'; import NotificationsDataManager from '../common/data-managers/notifications-data-manager'; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class UsersStore extends ArrayStore { constructor() { @@ -30,12 +31,8 @@ class UsersStore extends ArrayStore { case this.type + '/add-error': if (action.error && !action.error.handled && action.error.response) { // If it was an error and hasn't been handled, user could not be added - const USER_ADD_ERROR_NOTIFICATION = { - title: 'Failed to add new user', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(USER_ADD_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.ADD_ERROR('Failed to add new user: ' + action.error.response.body.message)); } return super.reduce(state, action); @@ -43,12 +40,8 @@ class UsersStore extends ArrayStore { case this.type + '/edit-error': if (action.error && !action.error.handled && action.error.response) { // If it was an error and hasn't been handled, user couldn't not be updated - const USER_EDIT_ERROR_NOTIFICATION = { - title: 'Failed to edit user', - message: action.error.response.body.message, - level: 'error' - }; - NotificationsDataManager.addNotification(USER_EDIT_ERROR_NOTIFICATION); + NotificationsDataManager.addNotification( + NotificationsFactory.UPDATE_ERROR('Failed to edit user: ' + action.error.response.body.message)); } return super.reduce(state, action); diff --git a/src/user/users.js b/src/user/users.js index f6f03df..af4fbaa 100644 --- a/src/user/users.js +++ b/src/user/users.js @@ -30,6 +30,7 @@ import EditUserDialog from './edit-user'; import DeleteDialog from '../common/dialogs/delete-dialog'; import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../common/data-managers/notifications-factory"; class Users extends Component { static getStores() { @@ -97,12 +98,8 @@ class Users extends Component { token: this.state.token }); } else{ - const USER_UPDATE_ERROR_NOTIFICATION = { - title: 'Update Error ', - message: 'New password not correctly confirmed', - level: 'error' - }; - NotificationsDataManager.addNotification(USER_UPDATE_ERROR_NOTIFICATION) + + NotificationsDataManager.addNotification(NotificationsFactory.UPDATE_ERROR("New password not correctly confirmed")) } } }