From c977a2de8dacdd03e9b77ea6b507899b639b60ba Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Thu, 28 May 2020 16:43:31 -0400 Subject: [PATCH] Encapsulate alertstate, alertmanagerstate --- js/src/common/Application.js | 4 ++-- js/src/common/components/AlertManager.js | 4 ++-- js/src/common/states/AlertManagerState.js | 10 +++++++--- js/src/common/states/AlertState.js | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/js/src/common/Application.js b/js/src/common/Application.js index 7afabbb526..aceaf7a5a6 100644 --- a/js/src/common/Application.js +++ b/js/src/common/Application.js @@ -291,7 +291,7 @@ export default class Application { } }; - if (this.requestError) this.alerts.dismiss(this.requestError.alert.key); + if (this.requestError) this.alerts.dismiss(this.requestError.alert.getKey()); // Now make the request. If it's a failure, inspect the error that was // returned and show an alert containing its contents. @@ -375,7 +375,7 @@ export default class Application { * @private */ showDebug(error, formattedError) { - this.alerts.dismiss(this.requestError.alert.key); + this.alerts.dismiss(this.requestError.alert.getKey()); this.modal.show(new RequestErrorModal({ error, formattedError })); } diff --git a/js/src/common/components/AlertManager.js b/js/src/common/components/AlertManager.js index 25dbbcce1a..344f2d558d 100644 --- a/js/src/common/components/AlertManager.js +++ b/js/src/common/components/AlertManager.js @@ -13,9 +13,9 @@ export default class AlertManager extends Component { view() { return (
- {Object.entries(this.state.activeAlerts).map(([key, state]) => ( + {Object.entries(this.state.getActiveAlerts()).map(([key, state]) => (
- {state.alertClass.component({ ...state.attrs, ondismiss: this.state.dismiss.bind(this.state, key) })} + {state.getClass().component({ ...state.getAttrs(), ondismiss: this.state.dismiss.bind(this.state, key) })}
))}
diff --git a/js/src/common/states/AlertManagerState.js b/js/src/common/states/AlertManagerState.js index d5537e329e..94a6bdf78f 100644 --- a/js/src/common/states/AlertManagerState.js +++ b/js/src/common/states/AlertManagerState.js @@ -3,14 +3,18 @@ export default class AlertManagerState { this.activeAlerts = {}; } + getActiveAlerts() { + return this.activeAlerts; + } + /** * Show an Alert in the alerts area. */ - show(state) { - this.activeAlerts[state.key] = state; + show(alert) { + this.activeAlerts[alert.getKey()] = state; m.redraw(); - return state.key; + return alert.getKey(); } /** diff --git a/js/src/common/states/AlertState.js b/js/src/common/states/AlertState.js index a4ef825f0c..208ea327c4 100644 --- a/js/src/common/states/AlertState.js +++ b/js/src/common/states/AlertState.js @@ -7,6 +7,26 @@ export default class AlertState { this.alertKey = alertKey; } + getAttrs() { + return this.attrs; + } + + setAttrs(attrs) { + this.attrs = attrs; + } + + getClass() { + return this.alertClass; + } + + setClass(alertClass) { + this.alertClass = alertClass; + } + + getKey() { + return this.key; + } + static genAlertId() { return Math.floor(Math.random() * 100000000); // Generate a big random integer to avoid collisions. }