Skip to content

Commit

Permalink
Move page size select to the Paginator component (#5064)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldutra authored Jul 27, 2020
1 parent 7804dfd commit f3a47a9
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 92 deletions.
22 changes: 18 additions & 4 deletions client/app/components/Paginator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ import React from "react";
import PropTypes from "prop-types";
import Pagination from "antd/lib/pagination";

export default function Paginator({ page, itemsPerPage, totalCount, onChange }) {
if (totalCount <= itemsPerPage) {
const MIN_ITEMS_PER_PAGE = 5;

export default function Paginator({ page, showPageSizeSelect, pageSize, onPageSizeChange, totalCount, onChange }) {
if (totalCount <= (showPageSizeSelect ? MIN_ITEMS_PER_PAGE : pageSize)) {
return null;
}
return (
<div className="paginator-container">
<Pagination defaultCurrent={page} defaultPageSize={itemsPerPage} total={totalCount} onChange={onChange} />
<Pagination
showSizeChanger={showPageSizeSelect}
pageSizeOptions={["5", "10", "20", "50", "100"]}
onShowSizeChange={(_, size) => onPageSizeChange(size)}
defaultCurrent={page}
pageSize={pageSize}
total={totalCount}
onChange={onChange}
/>
</div>
);
}

Paginator.propTypes = {
page: PropTypes.number.isRequired,
itemsPerPage: PropTypes.number.isRequired,
showPageSizeSelect: PropTypes.bool,
pageSize: PropTypes.number.isRequired,
totalCount: PropTypes.number.isRequired,
onPageSizeChange: PropTypes.func,
onChange: PropTypes.func,
};

Paginator.defaultProps = {
showPageSizeSelect: false,
onChange: () => {},
onPageSizeChange: () => {},
};
6 changes: 0 additions & 6 deletions client/app/components/groups/DetailsPageSidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ export default function DetailsPageSidebar({
return (
<React.Fragment>
<Sidebar.Menu items={items} selected={controller.params.currentPage} />
<Sidebar.PageSizeSelect
className="m-b-10"
options={controller.pageSizeOptions}
value={controller.itemsPerPage}
onChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
/>
{canAddMembers && (
<Button className="w-100 m-t-5" type="primary" onClick={onAddMembersClick}>
<i className="fa fa-plus m-r-5" />
Expand Down
25 changes: 0 additions & 25 deletions client/app/components/items-list/components/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useState, useCallback, useEffect } from "react";
import PropTypes from "prop-types";
import Input from "antd/lib/input";
import AntdMenu from "antd/lib/menu";
import Select from "antd/lib/select";
import TagsList from "@/components/TagsList";

/*
Expand Down Expand Up @@ -147,27 +146,3 @@ Tags.propTypes = {
url: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
};

/*
PageSizeSelect
*/

export function PageSizeSelect({ options, value, onChange, ...props }) {
return (
<div {...props}>
<Select className="w-100" defaultValue={value} onChange={onChange}>
{map(options, option => (
<Select.Option key={option} value={option}>
{option} results
</Select.Option>
))}
</Select>
</div>
);
}

PageSizeSelect.propTypes = {
options: PropTypes.arrayOf(PropTypes.number).isRequired,
value: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
};
51 changes: 20 additions & 31 deletions client/app/pages/admin/OutdatedQueries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { map } from "lodash";
import React from "react";

import Switch from "antd/lib/switch";
import * as Grid from "antd/lib/grid";
import routeWithUserSession from "@/components/ApplicationArea/routeWithUserSession";
import Paginator from "@/components/Paginator";
import { QueryTagsControl } from "@/components/tags-control/TagsControl";
Expand All @@ -15,7 +14,6 @@ import { ItemsSource } from "@/components/items-list/classes/ItemsSource";
import { StateStorage } from "@/components/items-list/classes/StateStorage";

import LoadingState from "@/components/items-list/components/LoadingState";
import { PageSizeSelect } from "@/components/items-list/components/Sidebar";
import ItemsTable, { Columns } from "@/components/items-list/components/ItemsTable";

import { axios } from "@/services/axios";
Expand Down Expand Up @@ -92,35 +90,24 @@ class OutdatedQueries extends React.Component {
const { controller } = this.props;
return (
<Layout activeTab={controller.params.currentPage}>
<Grid.Row className="m-15">
<Grid.Col span={16}>
<div>
<label htmlFor="auto-update-switch" className="m-0">
Auto update
</label>
<Switch
id="auto-update-switch"
className="m-l-10"
checked={this.state.autoUpdate}
onChange={autoUpdate => this.setState({ autoUpdate })}
/>
<div className="m-15">
<div>
<label htmlFor="auto-update-switch" className="m-0">
Auto update
</label>
<Switch
id="auto-update-switch"
className="m-l-10"
checked={this.state.autoUpdate}
onChange={autoUpdate => this.setState({ autoUpdate })}
/>
</div>
{controller.params.lastUpdatedAt && (
<div className="m-t-5">
Last updated: <TimeAgo date={controller.params.lastUpdatedAt * 1000} />
</div>
{controller.params.lastUpdatedAt && (
<div className="m-t-5">
Last updated: <TimeAgo date={controller.params.lastUpdatedAt * 1000} />
</div>
)}
</Grid.Col>
<Grid.Col span={8}>
{controller.isLoaded && !controller.isEmpty && (
<PageSizeSelect
options={controller.pageSizeOptions}
value={controller.itemsPerPage}
onChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
/>
)}
</Grid.Col>
</Grid.Row>
)}
</div>
{!controller.isLoaded && <LoadingState />}
{controller.isLoaded && controller.isEmpty && (
<div className="text-center p-15">There are no outdated queries.</div>
Expand All @@ -135,8 +122,10 @@ class OutdatedQueries extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/alerts/AlertsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ class AlertsList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
10 changes: 3 additions & 7 deletions client/app/pages/dashboards/DashboardList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ class DashboardList extends React.Component {
/>
<Sidebar.Menu items={this.sidebarMenu} selected={controller.params.currentPage} />
<Sidebar.Tags url="api/dashboards/tags" onChange={controller.updateSelectedTags} />
<Sidebar.PageSizeSelect
className="m-b-10"
options={controller.pageSizeOptions}
value={controller.itemsPerPage}
onChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
/>
</Layout.Sidebar>
<Layout.Content>
{controller.isLoaded ? (
Expand All @@ -123,8 +117,10 @@ class DashboardList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/groups/GroupDataSources.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ class GroupDataSources extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/groups/GroupMembers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ class GroupMembers extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/groups/GroupsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ class GroupsList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
10 changes: 3 additions & 7 deletions client/app/pages/queries-list/QueriesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ class QueriesList extends React.Component {
/>
<Sidebar.Menu items={this.sidebarMenu} selected={controller.params.currentPage} />
<Sidebar.Tags url="api/queries/tags" onChange={controller.updateSelectedTags} />
<Sidebar.PageSizeSelect
className="m-b-10"
options={controller.pageSizeOptions}
value={controller.itemsPerPage}
onChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
/>
</Layout.Sidebar>
<Layout.Content>
{!controller.isLoaded && <LoadingState />}
Expand All @@ -156,8 +150,10 @@ class QueriesList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/query-snippets/QuerySnippetsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ class QuerySnippetsList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down
10 changes: 3 additions & 7 deletions client/app/pages/users/UsersList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,6 @@ class UsersList extends React.Component {
<Layout.Sidebar className="m-b-0">
<Sidebar.SearchInput value={controller.searchTerm} onChange={controller.updateSearch} />
<Sidebar.Menu items={this.sidebarMenu} selected={controller.params.currentPage} />
<Sidebar.PageSizeSelect
className="m-b-10"
options={controller.pageSizeOptions}
value={controller.itemsPerPage}
onChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
/>
</Layout.Sidebar>
<Layout.Content>
{!controller.isLoaded && <LoadingState className="" />}
Expand All @@ -234,8 +228,10 @@ class UsersList extends React.Component {
toggleSorting={controller.toggleSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
itemsPerPage={controller.itemsPerPage}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
/>
Expand Down

0 comments on commit f3a47a9

Please sign in to comment.