From 36a4866a99ec0f608f1af6a8897f9769a24f62c8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Tue, 30 May 2017 10:11:27 +0200 Subject: [PATCH] SONAR-9254 Add analysis date sorting on projects page --- .../apps/projects/components/AllProjects.js | 9 +---- .../components/DefaultPageSelector.js | 37 ++++++++++++++----- .../src/main/js/apps/projects/store/utils.js | 2 + .../src/main/js/apps/projects/utils.js | 3 ++ .../resources/org/sonar/l10n/core.properties | 1 + 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/projects/components/AllProjects.js b/server/sonar-web/src/main/js/apps/projects/components/AllProjects.js index 46d752be2db..f1f83563352 100644 --- a/server/sonar-web/src/main/js/apps/projects/components/AllProjects.js +++ b/server/sonar-web/src/main/js/apps/projects/components/AllProjects.js @@ -95,13 +95,8 @@ export default class AllProjects extends React.PureComponent { } }; - handleSortChange = (sort: string, desc: boolean) => { - if (sort === 'name' && !desc) { - this.updateLocationQuery({ sort: undefined }); - } else { - this.updateLocationQuery({ sort: (desc ? '-' : '') + sort }); - } - }; + handleSortChange = (sort: string, desc: boolean) => + this.updateLocationQuery({ sort: (desc ? '-' : '') + sort }); handleQueryChange() { const query = parseUrlQuery(this.props.location.query); diff --git a/server/sonar-web/src/main/js/apps/projects/components/DefaultPageSelector.js b/server/sonar-web/src/main/js/apps/projects/components/DefaultPageSelector.js index aa306689acd..b8673473218 100644 --- a/server/sonar-web/src/main/js/apps/projects/components/DefaultPageSelector.js +++ b/server/sonar-web/src/main/js/apps/projects/components/DefaultPageSelector.js @@ -29,11 +29,14 @@ import { searchProjects } from '../../../api/components'; type Props = { currentUser: { isLoggedIn: boolean }, location: { query: {} }, - router: { replace: (path: string) => void } + router: { + replace: (location: { pathname?: string, query?: { [string]: string } }) => void + } }; type State = { - shouldBeRedirected?: boolean + shouldBeRedirected?: boolean, + shouldForceSorting?: string }; class DefaultPageSelector extends React.PureComponent { @@ -53,35 +56,49 @@ class DefaultPageSelector extends React.PureComponent { if (prevProps.location !== this.props.location) { this.defineIfShouldBeRedirected(); } else if (this.state.shouldBeRedirected === true) { - this.props.router.replace('/projects/favorite'); + this.props.router.replace({ ...this.props.location, pathname: '/projects/favorite' }); + } else if (this.state.shouldForceSorting != null) { + this.props.router.replace({ + ...this.props.location, + query: { + ...this.props.location.query, + sort: this.state.shouldForceSorting + } + }); } } defineIfShouldBeRedirected() { if (Object.keys(this.props.location.query).length > 0) { // show ALL projects when there are some filters - this.setState({ shouldBeRedirected: false }); + this.setState({ shouldBeRedirected: false, shouldForceSorting: undefined }); } else if (!this.props.currentUser.isLoggedIn) { // show ALL projects if user is anonymous - this.setState({ shouldBeRedirected: false }); + if (!this.props.location.query || !this.props.location.query.sort) { + // force default sorting to last analysis date + this.setState({ shouldBeRedirected: false, shouldForceSorting: '-analysis_date' }); + } else { + this.setState({ shouldBeRedirected: false, shouldForceSorting: undefined }); + } } else if (isFavoriteSet()) { // show FAVORITE projects if "favorite" setting is explicitly set - this.setState({ shouldBeRedirected: true }); + this.setState({ shouldBeRedirected: true, shouldForceSorting: undefined }); } else if (isAllSet()) { // show ALL projects if "all" setting is explicitly set - this.setState({ shouldBeRedirected: false }); + this.setState({ shouldBeRedirected: false, shouldForceSorting: undefined }); } else { // otherwise, request favorites - this.setState({ shouldBeRedirected: undefined }); + this.setState({ shouldBeRedirected: undefined, shouldForceSorting: undefined }); searchProjects({ filter: 'isFavorite', ps: 1 }).then(r => { // show FAVORITE projects if there are any - this.setState({ shouldBeRedirected: r.paging.total > 0 }); + this.setState({ shouldBeRedirected: r.paging.total > 0, shouldForceSorting: undefined }); }); } } render() { - if (this.state.shouldBeRedirected == null || this.state.shouldBeRedirected === true) { + const { shouldBeRedirected, shouldForceSorting } = this.state; + if (shouldBeRedirected == null || shouldBeRedirected === true || shouldForceSorting != null) { return null; } else { return ( diff --git a/server/sonar-web/src/main/js/apps/projects/store/utils.js b/server/sonar-web/src/main/js/apps/projects/store/utils.js index 13db6e4c7cb..2fdac846544 100644 --- a/server/sonar-web/src/main/js/apps/projects/store/utils.js +++ b/server/sonar-web/src/main/js/apps/projects/store/utils.js @@ -78,6 +78,7 @@ export const parseUrlQuery = urlQuery => ({ export const mapMetricToProperty = metricKey => { const map = { + analysisDate: 'analysis_date', reliability_rating: 'reliability', new_reliability_rating: 'new_reliability', security_rating: 'security', @@ -100,6 +101,7 @@ export const mapMetricToProperty = metricKey => { export const mapPropertyToMetric = property => { const map = { + analysis_date: 'analysisDate', reliability: 'reliability_rating', new_reliability: 'new_reliability_rating', security: 'security_rating', diff --git a/server/sonar-web/src/main/js/apps/projects/utils.js b/server/sonar-web/src/main/js/apps/projects/utils.js index ab4d157c3a2..85d898e64a0 100644 --- a/server/sonar-web/src/main/js/apps/projects/utils.js +++ b/server/sonar-web/src/main/js/apps/projects/utils.js @@ -49,6 +49,7 @@ export const saveFavorite = () => save(LOCALSTORAGE_FAVORITE); export const SORTING_METRICS = [ { value: 'name' }, + { value: 'analysis_date' }, { value: 'reliability' }, { value: 'security' }, { value: 'maintainability' }, @@ -59,6 +60,7 @@ export const SORTING_METRICS = [ export const SORTING_LEAK_METRICS = [ { value: 'name' }, + { value: 'analysis_date' }, { value: 'new_reliability', complement: 'on_new_code' }, { value: 'new_security', complement: 'on_new_code' }, { value: 'new_maintainability', complement: 'on_new_code' }, @@ -68,6 +70,7 @@ export const SORTING_LEAK_METRICS = [ ]; export const SORTING_SWITCH = { + analysis_date: 'analysis_date', name: 'name', reliability: 'new_reliability', security: 'new_security', diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index 957a773a6b1..2a0e497caa8 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -869,6 +869,7 @@ projects.sort_by=Sort by projects.sort_ascending=Result sorted in ascending order projects.sort_descending=Result sorted in descending order projects.sorting.name=Name (default) +projects.sorting.analysis_date=Last analysis date projects.sorting.reliability=Reliability projects.sorting.security=Security projects.sorting.maintainability=Maintainability -- 2.39.5