123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import * as ReactRouter from 'react-router';
- import {browserHistory} from 'react-router';
- import styled from '@emotion/styled';
- import pick from 'lodash/pick';
- import {Client} from 'app/api';
- import Feature from 'app/components/acl/feature';
- import Alert from 'app/components/alert';
- import Breadcrumbs from 'app/components/breadcrumbs';
- import DropdownControl, {DropdownItem} from 'app/components/dropdownControl';
- import * as Layout from 'app/components/layouts/thirds';
- import LightWeightNoProjectMessage from 'app/components/lightWeightNoProjectMessage';
- import SearchBar from 'app/components/searchBar';
- import {t} from 'app/locale';
- import {PageContent} from 'app/styles/organization';
- import space from 'app/styles/space';
- import {Organization, SelectValue} from 'app/types';
- import {decodeScalar} from 'app/utils/queryString';
- import withApi from 'app/utils/withApi';
- import withOrganization from 'app/utils/withOrganization';
- import AsyncView from 'app/views/asyncView';
- import {DashboardListItem} from '../types';
- import DashboardList from './dashboardList';
- const SORT_OPTIONS: SelectValue<string>[] = [
- {label: t('My Dashboards'), value: 'mydashboards'},
- {label: t('Dashboard Name (A-Z)'), value: 'title'},
- {label: t('Date Created (Newest)'), value: '-dateCreated'},
- {label: t('Date Created (Oldest)'), value: 'dateCreated'},
- ];
- type Props = {
- api: Client;
- organization: Organization;
- location: Location;
- router: ReactRouter.InjectedRouter;
- } & AsyncView['props'];
- type State = {
- dashboards: DashboardListItem[] | null;
- dashboardsPageLinks: string;
- } & AsyncView['state'];
- class ManageDashboards extends AsyncView<Props, State> {
- getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
- const {organization, location} = this.props;
- return [
- [
- 'dashboards',
- `/organizations/${organization.slug}/dashboards/`,
- {
- query: {
- ...pick(location.query, ['cursor', 'query']),
- sort: this.getActiveSort().value,
- per_page: '9',
- },
- },
- ],
- ];
- }
- getActiveSort() {
- const {location} = this.props;
- const urlSort = decodeScalar(location.query.sort, 'mydashboards');
- return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
- }
- onDashboardsChange() {
- this.reloadData();
- }
- handleSearch(query: string) {
- const {location, router} = this.props;
- router.push({
- pathname: location.pathname,
- query: {...location.query, cursor: undefined, query},
- });
- }
- handleSortChange = (value: string) => {
- const {location} = this.props;
- browserHistory.push({
- pathname: location.pathname,
- query: {
- ...location.query,
- cursor: undefined,
- sort: value,
- },
- });
- };
- getQuery() {
- const {query} = this.props.location.query;
- return typeof query === 'string' ? query : undefined;
- }
- renderActions() {
- const activeSort = this.getActiveSort();
- return (
- <StyledActions>
- <StyledSearchBar
- defaultQuery=""
- query={this.getQuery()}
- placeholder={t('Search Dashboards')}
- onSearch={query => this.handleSearch(query)}
- />
- <StyledDropdownControl
- buttonProps={{prefix: t('Sort By')}}
- label={activeSort.label}
- >
- {SORT_OPTIONS.map(({label, value}) => (
- <DropdownItem
- key={value}
- onSelect={this.handleSortChange}
- eventKey={value}
- isActive={value === activeSort.value}
- >
- {label}
- </DropdownItem>
- ))}
- </StyledDropdownControl>
- </StyledActions>
- );
- }
- renderNoAccess() {
- return (
- <PageContent>
- <Alert type="warning">{t("You don't have access to this feature")}</Alert>
- </PageContent>
- );
- }
- renderDashboards() {
- const {dashboards, dashboardsPageLinks} = this.state;
- const {organization, location, api} = this.props;
- return (
- <DashboardList
- api={api}
- dashboards={dashboards}
- organization={organization}
- pageLinks={dashboardsPageLinks}
- location={location}
- onDashboardsChange={() => this.onDashboardsChange()}
- />
- );
- }
- getTitle() {
- return t('Manage Dashboards');
- }
- renderBody() {
- const {organization} = this.props;
- return (
- <Feature
- organization={organization}
- features={['dashboards-manage']}
- renderDisabled={this.renderNoAccess}
- >
- <LightWeightNoProjectMessage organization={organization}>
- <Layout.Header>
- <Layout.HeaderContent>
- <Breadcrumbs
- crumbs={[
- {
- label: 'Dashboards',
- to: `/organizations/${organization.slug}/dashboards/`,
- },
- {
- label: 'Manage Dashboards',
- },
- ]}
- />
- <Layout.Title>{t('Manage Dashboards')}</Layout.Title>
- </Layout.HeaderContent>
- </Layout.Header>
- <Layout.Body>
- <Layout.Main fullWidth>
- {this.renderActions()}
- {this.renderDashboards()}
- </Layout.Main>
- </Layout.Body>
- </LightWeightNoProjectMessage>
- </Feature>
- );
- }
- }
- const StyledSearchBar = styled(SearchBar)`
- flex-grow: 1;
- margin-right: ${space(2)};
- margin-bottom: ${space(2)};
- `;
- const StyledDropdownControl = styled(DropdownControl)`
- margin-bottom: ${space(2)};
- `;
- const StyledActions = styled('div')`
- display: grid;
- grid-template-columns: auto max-content min-content;
- width: 100%;
- @media (max-width: ${p => p.theme.breakpoints[0]}) {
- grid-template-columns: auto;
- }
- margin-bottom: ${space(1)};
- align-items: center;
- `;
- export default withApi(withOrganization(ManageDashboards));
|