]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9403 Save custom graph in the localstorage on the project activity page
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Fri, 7 Jul 2017 08:35:18 +0000 (10:35 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Thu, 13 Jul 2017 12:34:17 +0000 (14:34 +0200)
server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.js
server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityGraphs.js
server/sonar-web/src/main/js/helpers/storage.js

index 3ebe68f48de1fe9b79f806dc0283a4e052eb892f..afe28a6ebe1fbfc4fc9f68bc58c98e98878704fe 100644 (file)
@@ -29,10 +29,11 @@ import { getAllTimeMachineData } from '../../../api/time-machine';
 import { getMetrics } from '../../../api/metrics';
 import * as api from '../../../api/projectActivity';
 import * as actions from '../actions';
-import { getGraph, saveGraph } from '../../../helpers/storage';
+import { getCustomGraph, getGraph } from '../../../helpers/storage';
 import {
   customMetricsChanged,
   getHistoryMetrics,
+  isCustomGraph,
   parseQuery,
   serializeQuery,
   serializeUrlQuery
@@ -78,9 +79,13 @@ class ProjectActivityAppContainer extends React.PureComponent {
     };
 
     if (this.shouldRedirect()) {
+      const newQuery = { ...this.state.query, graph: getGraph() };
+      if (isCustomGraph(newQuery.graph)) {
+        newQuery.customMetrics = getCustomGraph();
+      }
       this.props.router.replace({
         pathname: props.location.pathname,
-        query: serializeUrlQuery({ ...this.state.query, graph: getGraph() })
+        query: serializeUrlQuery(newQuery)
       });
     }
   }
@@ -259,7 +264,6 @@ class ProjectActivityAppContainer extends React.PureComponent {
       ...this.state.query,
       ...newQuery
     });
-    saveGraph(query.graph);
     this.props.router.push({
       pathname: this.props.location.pathname,
       query: {
index 13d42349be3a24deeba72a3719795e07386ca2ea..d6b96f42f42603902ffd179748ba5d83eb3316ef 100644 (file)
@@ -23,8 +23,10 @@ import { debounce, findLast, maxBy, minBy, sortBy } from 'lodash';
 import ProjectActivityGraphsHeader from './ProjectActivityGraphsHeader';
 import GraphsZoom from './GraphsZoom';
 import GraphsHistory from './GraphsHistory';
+import { getCustomGraph, saveCustomGraph, saveGraph } from '../../../helpers/storage';
 import {
   datesQueryChanged,
+  isCustomGraph,
   generateSeries,
   getDisplayedHistoryMetrics,
   historyQueryChanged
@@ -107,15 +109,26 @@ export default class ProjectActivityGraphs extends React.PureComponent {
     }
   };
 
-  addCustomMetric = (metric: string) =>
-    this.props.updateQuery({ customMetrics: [...this.props.query.customMetrics, metric] });
+  addCustomMetric = (metric: string) => {
+    const customMetrics = [...this.props.query.customMetrics, metric];
+    saveCustomGraph(customMetrics);
+    this.props.updateQuery({ customMetrics });
+  };
 
-  removeCustomMetric = (removedMetric: string) =>
-    this.props.updateQuery({
-      customMetrics: this.props.query.customMetrics.filter(metric => metric !== removedMetric)
-    });
+  removeCustomMetric = (removedMetric: string) => {
+    const customMetrics = this.props.query.customMetrics.filter(metric => metric !== removedMetric);
+    saveCustomGraph(customMetrics);
+    this.props.updateQuery({ customMetrics });
+  };
 
-  updateGraph = (graph: string) => this.props.updateQuery({ graph });
+  updateGraph = (graph: string) => {
+    saveGraph(graph);
+    if (isCustomGraph(graph) && this.props.query.customMetrics.length <= 0) {
+      this.props.updateQuery({ graph, customMetrics: getCustomGraph() });
+    } else {
+      this.props.updateQuery({ graph, customMetrics: [] });
+    }
+  };
 
   updateGraphZoom = (graphStartDate: ?Date, graphEndDate: ?Date) => {
     if (graphEndDate != null && graphStartDate != null) {
index fc7ec4d0ad54133ff0704a1ddfe6c622fabcc6d0..d9bee866b8ba8f04ebee3f7dc3be048450e8a3cb 100644 (file)
@@ -27,6 +27,7 @@ const PROJECTS_VISUALIZATION = 'sonarqube.projects.visualization';
 const PROJECTS_SORT = 'sonarqube.projects.sort';
 
 const PROJECT_ACTIVITY_GRAPH = 'sonarqube.project_activity.graph';
+const PROJECT_ACTIVITY_GRAPH_CUSTOM = 'sonarqube.project_activity.graph.custom';
 
 const save = (key: string, value: ?string) => {
   try {
@@ -63,5 +64,11 @@ export const getVisualization = () => window.localStorage.getItem(PROJECTS_VISUA
 export const saveSort = (sort: ?string) => save(PROJECTS_SORT, sort);
 export const getSort = () => window.localStorage.getItem(PROJECTS_SORT);
 
+export const saveCustomGraph = (metrics: ?Array<string>) =>
+  save(PROJECT_ACTIVITY_GRAPH_CUSTOM, metrics ? metrics.join(',') : '');
+export const getCustomGraph = (): Array<string> =>
+  (window.localStorage.getItem(PROJECT_ACTIVITY_GRAPH_CUSTOM) || '').split(',');
+
 export const saveGraph = (graph: ?string) => save(PROJECT_ACTIVITY_GRAPH, graph);
-export const getGraph = () => window.localStorage.getItem(PROJECT_ACTIVITY_GRAPH) || 'overview';
+export const getGraph = (): string =>
+  window.localStorage.getItem(PROJECT_ACTIVITY_GRAPH) || 'overview';