Browse Source

ref(ui): Convert system alerts to class component (#25456)

Scott Cooper 3 years ago
parent
commit
3aab71b95f
2 changed files with 19 additions and 11 deletions
  1. 5 0
      static/app/stores/alertStore.tsx
  2. 14 11
      static/app/views/app/systemAlerts.tsx

+ 5 - 0
static/app/stores/alertStore.tsx

@@ -17,6 +17,7 @@ type Alert = {
 
 
 type AlertStoreInterface = Reflux.StoreDefinition & {
 type AlertStoreInterface = Reflux.StoreDefinition & {
   init: () => void;
   init: () => void;
+  getInitialState: () => Alert[];
   onAddAlert: (alert: Alert) => void;
   onAddAlert: (alert: Alert) => void;
   onCloseAlert: (alert: Alert, duration?: number) => void;
   onCloseAlert: (alert: Alert, duration?: number) => void;
 };
 };
@@ -36,6 +37,10 @@ const storeConfig: AlertStoreInterface & Internals = {
     this.count = 0;
     this.count = 0;
   },
   },
 
 
+  getInitialState() {
+    return this.alerts;
+  },
+
   onAddAlert(alert) {
   onAddAlert(alert) {
     const alertAlreadyExists = this.alerts.some(a => a.id === alert.id);
     const alertAlreadyExists = this.alerts.some(a => a.id === alert.id);
     if (alertAlreadyExists && alert.noDuplicates) {
     if (alertAlreadyExists && alert.noDuplicates) {

+ 14 - 11
static/app/views/app/systemAlerts.tsx

@@ -1,7 +1,5 @@
 import React from 'react';
 import React from 'react';
-import createReactClass from 'create-react-class';
 import {ThemeProvider} from 'emotion-theming';
 import {ThemeProvider} from 'emotion-theming';
-import Reflux from 'reflux';
 
 
 import AlertStore from 'app/stores/alertStore';
 import AlertStore from 'app/stores/alertStore';
 import {lightTheme} from 'app/utils/theme';
 import {lightTheme} from 'app/utils/theme';
@@ -14,19 +12,24 @@ type State = {
   alerts: Array<Alert>;
   alerts: Array<Alert>;
 };
 };
 
 
-const Alerts = createReactClass<Props, State>({
-  displayName: 'Alerts',
-  mixins: [Reflux.connect(AlertStore, 'alerts') as any],
+class Alerts extends React.Component<Props, State> {
+  state = this.getInitialState();
 
 
-  getInitialState() {
+  getInitialState(): State {
     return {
     return {
-      alerts: [],
+      alerts: AlertStore.getInitialState() as Alert[],
     };
     };
-  },
+  }
+
+  componentWillUnmount() {
+    this.unlistener?.();
+  }
+
+  unlistener = AlertStore.listen((alerts: Alert[]) => this.setState({alerts}), undefined);
 
 
   render() {
   render() {
     const {className} = this.props;
     const {className} = this.props;
-    const alerts = this.state.alerts as Array<Alert>;
+    const {alerts} = this.state;
     return (
     return (
       <ThemeProvider theme={lightTheme}>
       <ThemeProvider theme={lightTheme}>
         <div className={className}>
         <div className={className}>
@@ -36,7 +39,7 @@ const Alerts = createReactClass<Props, State>({
         </div>
         </div>
       </ThemeProvider>
       </ThemeProvider>
     );
     );
-  },
-});
+  }
+}
 
 
 export default Alerts;
 export default Alerts;