From: Grégoire Aubert Date: Fri, 7 Jul 2017 08:35:18 +0000 (+0200) Subject: SONAR-9403 Save custom graph in the localstorage on the project activity page X-Git-Tag: 6.5-RC1~21 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5df7ff772f32072fdc35cee3be3b60c0572c5e75;p=sonarqube.git SONAR-9403 Save custom graph in the localstorage on the project activity page --- diff --git a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.js b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.js index 3ebe68f48de..afe28a6ebe1 100644 --- a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.js +++ b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.js @@ -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: { diff --git a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityGraphs.js b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityGraphs.js index 13d42349be3..d6b96f42f42 100644 --- a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityGraphs.js +++ b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityGraphs.js @@ -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) { diff --git a/server/sonar-web/src/main/js/helpers/storage.js b/server/sonar-web/src/main/js/helpers/storage.js index fc7ec4d0ad5..d9bee866b8b 100644 --- a/server/sonar-web/src/main/js/helpers/storage.js +++ b/server/sonar-web/src/main/js/helpers/storage.js @@ -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) => + save(PROJECT_ACTIVITY_GRAPH_CUSTOM, metrics ? metrics.join(',') : ''); +export const getCustomGraph = (): Array => + (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';