Skip to content

Commit

Permalink
Encapsulate alertstate, alertmanagerstate
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed May 28, 2020
1 parent dec92ef commit c977a2d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions js/src/common/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }));
}
Expand Down
4 changes: 2 additions & 2 deletions js/src/common/components/AlertManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default class AlertManager extends Component {
view() {
return (
<div className="AlertManager">
{Object.entries(this.state.activeAlerts).map(([key, state]) => (
{Object.entries(this.state.getActiveAlerts()).map(([key, state]) => (
<div className="AlertManager-alert">
{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) })}
</div>
))}
</div>
Expand Down
10 changes: 7 additions & 3 deletions js/src/common/states/AlertManagerState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions js/src/common/states/AlertState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down

0 comments on commit c977a2d

Please sign in to comment.