diff options
author | Stas Vilchik <stas-vilchik@users.noreply.github.com> | 2017-03-14 10:18:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 10:18:31 +0100 |
commit | 7abce8dd065133efb26e60b92404620bb879ff23 (patch) | |
tree | fd1c28b338fc893d5e77e3fc59d2a0ecbea57608 /server/sonar-web/src/main/js | |
parent | e0489108823967b6d6ac2a49be3a8a449b13116a (diff) | |
download | sonarqube-7abce8dd065133efb26e60b92404620bb879ff23.tar.gz sonarqube-7abce8dd065133efb26e60b92404620bb879ff23.zip |
SONAR-8452 change default projects page (#1777)
Diffstat (limited to 'server/sonar-web/src/main/js')
6 files changed, 80 insertions, 25 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 b4d8d44a06c..f7fbe17c899 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 @@ -22,7 +22,6 @@ import ProjectsListContainer from './ProjectsListContainer'; import ProjectsListFooterContainer from './ProjectsListFooterContainer'; import PageSidebar from './PageSidebar'; import { parseUrlQuery } from '../store/utils'; -import { saveAll, saveFavorite } from '../utils'; export default class AllProjects extends React.Component { static propTypes = { @@ -36,14 +35,6 @@ export default class AllProjects extends React.Component { }; componentDidMount () { - // do not touch organization-level page - if (!this.props.organization) { - if (this.props.isFavorite) { - saveFavorite(); - } else { - saveAll(); - } - } this.handleQueryChange(); } 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 eb487b5eb56..745b9696bf5 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 @@ -23,23 +23,65 @@ import { connect } from 'react-redux'; import { withRouter } from 'react-router'; import AllProjectsContainer from './AllProjectsContainer'; import { getCurrentUser } from '../../../store/rootReducer'; -import { shouldRedirectToFavorite } from '../utils'; +import { isFavoriteSet, isAllSet } from '../utils'; +import { searchProjects } from '../../../api/components'; + +type Props = { + currentUser: { isLoggedIn: boolean }, + location: { query: {} }, + router: { replace: (path: string) => void } +}; + +type State = { + shouldBeRedirected?: boolean +}; class DefaultPageSelector extends React.PureComponent { - props: { - currentUser: { isLoggedIn: boolean }, - location: {}, - router: { replace: (path: string) => void } - }; + props: Props; + state: State; + + constructor (props: Props) { + super(props); + this.state = {}; + } componentDidMount () { - if (shouldRedirectToFavorite(this.props.currentUser)) { + this.defineIfShouldBeRedirected(); + } + + componentDidUpdate (prevProps: Props) { + if (prevProps.location !== this.props.location) { + this.defineIfShouldBeRedirected(); + } else if (this.state.shouldBeRedirected === true) { this.props.router.replace('/projects/favorite'); } } + defineIfShouldBeRedirected () { + if (Object.keys(this.props.location.query).length > 0) { + // show ALL projects when there are some filters + this.setState({ shouldBeRedirected: false }); + } else if (!this.props.currentUser.isLoggedIn) { + // show ALL projects if user is anonymous + this.setState({ shouldBeRedirected: false }); + } else if (isFavoriteSet()) { + // show FAVORITE projects if "favorite" setting is explicitly set + this.setState({ shouldBeRedirected: true }); + } else if (isAllSet()) { + // show ALL projects if "all" setting is explicitly set + this.setState({ shouldBeRedirected: false }); + } else { + // otherwise, request favorites + this.setState({ shouldBeRedirected: undefined }); + searchProjects({ filter: 'isFavorite', ps: 1 }).then(r => { + // show FAVORITE projects if there are any + this.setState({ shouldBeRedirected: r.paging.total > 0 }); + }); + } + } + render () { - if (shouldRedirectToFavorite(this.props.currentUser)) { + if (this.state.shouldBeRedirected == null || this.state.shouldBeRedirected === true) { return null; } else { return ( diff --git a/server/sonar-web/src/main/js/apps/projects/components/FavoriteFilter.js b/server/sonar-web/src/main/js/apps/projects/components/FavoriteFilter.js index ebf2305ab13..af20cd02d1f 100644 --- a/server/sonar-web/src/main/js/apps/projects/components/FavoriteFilter.js +++ b/server/sonar-web/src/main/js/apps/projects/components/FavoriteFilter.js @@ -17,12 +17,25 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +// @flow import React from 'react'; import { IndexLink, Link } from 'react-router'; import { translate } from '../../../helpers/l10n'; -import { saveAll } from '../utils'; +import { saveAll, saveFavorite } from '../utils'; + +export default class FavoriteFilter extends React.PureComponent { + handleSaveFavorite = () => { + if (!this.props.organization) { + saveFavorite(); + } + }; + + handleSaveAll = () => { + if (!this.props.organization) { + saveAll(); + } + }; -export default class FavoriteFilter extends React.Component { render () { if (!this.props.user.isLoggedIn) { return null; @@ -43,7 +56,8 @@ export default class FavoriteFilter extends React.Component { id="favorite-projects" to={pathnameForFavorite} className="button" - activeClassName="button-active"> + activeClassName="button-active" + onClick={this.handleSaveFavorite}> {translate('my_favorites')} </Link> <IndexLink @@ -51,7 +65,7 @@ export default class FavoriteFilter extends React.Component { to={pathnameForAll} className="button" activeClassName="button-active" - onClick={saveAll}> + onClick={this.handleSaveAll}> {translate('all')} </IndexLink> </div> diff --git a/server/sonar-web/src/main/js/apps/projects/components/NoFavoriteProjects.js b/server/sonar-web/src/main/js/apps/projects/components/NoFavoriteProjects.js index 9bab0776673..072e51fe86f 100644 --- a/server/sonar-web/src/main/js/apps/projects/components/NoFavoriteProjects.js +++ b/server/sonar-web/src/main/js/apps/projects/components/NoFavoriteProjects.js @@ -28,7 +28,7 @@ export default class NoFavoriteProjects extends React.Component { <h3>{translate('projects.no_favorite_projects')}</h3> <p className="big-spacer-top">{translate('projects.no_favorite_projects.engagement')}</p> <p className="big-spacer-top"> - <Link to="/projects" className="button">{translate('projects.explore_projects')}</Link> + <Link to="/projects/all" className="button">{translate('projects.explore_projects')}</Link> </p> </div> ); diff --git a/server/sonar-web/src/main/js/apps/projects/routes.js b/server/sonar-web/src/main/js/apps/projects/routes.js index 4ed8f72a50a..8c389ca9d77 100644 --- a/server/sonar-web/src/main/js/apps/projects/routes.js +++ b/server/sonar-web/src/main/js/apps/projects/routes.js @@ -22,10 +22,17 @@ import { Route, IndexRoute } from 'react-router'; import App from './components/App'; import DefaultPageSelector from './components/DefaultPageSelector'; import FavoriteProjectsContainer from './components/FavoriteProjectsContainer'; +import { saveAll } from './utils'; export default ( <Route component={App}> <IndexRoute component={DefaultPageSelector}/> + <Route + path="all" + onEnter={(_, replace) => { + saveAll(); + replace('/projects'); + }}/> <Route path="favorite" component={FavoriteProjectsContainer}/> </Route> ); 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 598a8cf73c4..cf393674817 100644 --- a/server/sonar-web/src/main/js/apps/projects/utils.js +++ b/server/sonar-web/src/main/js/apps/projects/utils.js @@ -22,13 +22,14 @@ const LOCALSTORAGE_KEY = 'sonarqube.projects.default'; const LOCALSTORAGE_FAVORITE = 'favorite'; const LOCALSTORAGE_ALL = 'all'; -const isFavoriteSet = (): boolean => { +export const isFavoriteSet = (): boolean => { const setting = window.localStorage.getItem(LOCALSTORAGE_KEY); return setting === LOCALSTORAGE_FAVORITE; }; -export const shouldRedirectToFavorite = (currentUser: { isLoggedIn: boolean }) => { - return currentUser.isLoggedIn && isFavoriteSet(); +export const isAllSet = (): boolean => { + const setting = window.localStorage.getItem(LOCALSTORAGE_KEY); + return setting === LOCALSTORAGE_ALL; }; const save = (value: string) => { |