]> source.dussan.org Git - sonarqube.git/commitdiff
fix projects component to not set state when unmounted
authorStas Vilchik <stas.vilchik@sonarsource.com>
Tue, 11 Dec 2018 08:43:04 +0000 (09:43 +0100)
committerSonarTech <sonartech@sonarsource.com>
Tue, 8 Jan 2019 19:21:06 +0000 (20:21 +0100)
server/sonar-web/src/main/js/apps/quality-gates/components/Projects.tsx

index 279bc01bbe6f65bb88f252e1d3b1b94b620af000..84105505fba82d1c570a2b87db47cb3a75f906cf 100644 (file)
@@ -39,12 +39,18 @@ interface State {
 }
 
 export default class Projects extends React.PureComponent<Props, State> {
+  mounted = false;
   state: State = { projects: [], selectedProjects: [] };
 
   componentDidMount() {
+    this.mounted = true;
     this.handleSearch('', Filter.Selected);
   }
 
+  componentWillUnmount() {
+    this.mounted = false;
+  }
+
   handleSearch = (query: string, selected: string) => {
     return searchGates({
       gateId: this.props.qualityGate.id,
@@ -53,12 +59,14 @@ export default class Projects extends React.PureComponent<Props, State> {
       query: query !== '' ? query : undefined,
       selected
     }).then(data => {
-      this.setState({
-        projects: data.results,
-        selectedProjects: data.results
-          .filter(project => project.selected)
-          .map(project => project.id)
-      });
+      if (this.mounted) {
+        this.setState({
+          projects: data.results,
+          selectedProjects: data.results
+            .filter(project => project.selected)
+            .map(project => project.id)
+        });
+      }
     });
   };
 
@@ -68,9 +76,11 @@ export default class Projects extends React.PureComponent<Props, State> {
       organization: this.props.organization,
       projectId: id
     }).then(() => {
-      this.setState(state => ({
-        selectedProjects: [...state.selectedProjects, id]
-      }));
+      if (this.mounted) {
+        this.setState(state => ({
+          selectedProjects: [...state.selectedProjects, id]
+        }));
+      }
     });
   };
 
@@ -81,9 +91,11 @@ export default class Projects extends React.PureComponent<Props, State> {
       projectId: id
     }).then(
       () => {
-        this.setState(state => ({
-          selectedProjects: without(state.selectedProjects, id)
-        }));
+        if (this.mounted) {
+          this.setState(state => ({
+            selectedProjects: without(state.selectedProjects, id)
+          }));
+        }
       },
       () => {}
     );