1
0
mirror of https://github.com/flarum/core.git synced 2025-07-13 12:56:26 +02:00

Extract AlertManagerState from AlertManager (#2163)

This commit is contained in:
Alexander Skvortsov
2020-06-30 18:06:59 -04:00
committed by GitHub
parent aaebd3581f
commit ea9d601338
12 changed files with 107 additions and 103 deletions

View File

@ -1,5 +1,4 @@
import ItemList from './utils/ItemList';
import Alert from './components/Alert';
import Button from './components/Button';
import ModalManager from './components/ModalManager';
import AlertManager from './components/AlertManager';
@ -23,6 +22,7 @@ import Group from './models/Group';
import Notification from './models/Notification';
import { flattenDeep } from 'lodash-es';
import PageState from './states/PageState';
import AlertManagerState from './states/AlertManagerState';
/**
* The `App` class provides a container for an application, as well as various
@ -109,13 +109,13 @@ export default class Application {
booted = false;
/**
* An Alert that was shown as a result of an AJAX request error. If present,
* it will be dismissed on the next successful request.
* The key for an Alert that was shown as a result of an AJAX request error.
* If present, it will be dismissed on the next successful request.
*
* @type {null|Alert}
* @type {int}
* @private
*/
requestError = null;
requestErrorAlert = null;
/**
* The page the app is currently on.
@ -139,6 +139,11 @@ export default class Application {
*/
previous = new PageState(null);
/*
* An object that manages the state of active alerts.
*/
alerts = new AlertManagerState();
data;
title = '';
@ -175,7 +180,7 @@ export default class Application {
mount(basePath = '') {
this.modal = m.mount(document.getElementById('modal'), <ModalManager />);
this.alerts = m.mount(document.getElementById('alerts'), <AlertManager />);
m.mount(document.getElementById('alerts'), <AlertManager state={this.alerts} />);
this.drawer = new Drawer();
@ -313,7 +318,7 @@ export default class Application {
}
};
if (this.requestError) this.alerts.dismiss(this.requestError.alert);
if (this.requestErrorAlert) this.alerts.dismiss(this.requestErrorAlert);
// Now make the request. If it's a failure, inspect the error that was
// returned and show an alert containing its contents.
@ -322,8 +327,6 @@ export default class Application {
m.request(options).then(
(response) => deferred.resolve(response),
(error) => {
this.requestError = error;
let children;
switch (error.status) {
@ -357,7 +360,7 @@ export default class Application {
// the details property is decoded to transform escaped characters such as '\n'
const formattedError = error.response && Array.isArray(error.response.errors) && error.response.errors.map((e) => decodeURI(e.detail));
error.alert = new Alert({
error.alert = {
type: 'error',
children,
controls: isDebug && [
@ -365,7 +368,7 @@ export default class Application {
Debug
</Button>,
],
});
};
try {
options.errorHandler(error);
@ -381,7 +384,7 @@ export default class Application {
console.groupEnd();
}
this.alerts.show(error.alert);
this.requestErrorAlert = this.alerts.show(error.alert);
}
deferred.reject(error);
@ -397,7 +400,7 @@ export default class Application {
* @private
*/
showDebug(error, formattedError) {
this.alerts.dismiss(this.requestError.alert);
this.alerts.dismiss(this.requestErrorAlert);
this.modal.show(new RequestErrorModal({ error, formattedError }));
}