From e4437479c49dda5133791785dc28c2424cec433d Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 24 Aug 2017 16:24:48 +0900 Subject: [PATCH] pager util --- .../commons/react/component/Pager.tsx | 94 +++++++++++++++++++ .../history/react/components/HistoryList.tsx | 66 +------------ .../target/react/components/TargetList.tsx | 25 +++-- 3 files changed, 114 insertions(+), 71 deletions(-) create mode 100644 src/ts/@overflow/commons/react/component/Pager.tsx diff --git a/src/ts/@overflow/commons/react/component/Pager.tsx b/src/ts/@overflow/commons/react/component/Pager.tsx new file mode 100644 index 0000000..9666b96 --- /dev/null +++ b/src/ts/@overflow/commons/react/component/Pager.tsx @@ -0,0 +1,94 @@ +import * as React from 'react'; +import { + Menu, + Icon, + Container, +} from 'semantic-ui-react'; +import Page from '@overflow/commons/api/model/Page'; + +export interface Props { + page: Page; + onPageChange(pageNo: number): void; +} + +export interface State { + current: number; +} + +export class Pager extends React.Component { + + private countPerPage: number = 10; + + constructor(props: Props, context: State) { + super(props, context); + this.state = { + current: 0, + }; + } + + public handlePaging = (pageNo: number) => { + if (pageNo === this.state.current) { + return; + } + this.setState({ + current: pageNo, + }); + this.props.onPageChange(pageNo); + } + + public handlePrevPage = (pn: string) => { + let pageNo = 0; + if (pn === 'P') { + pageNo = this.state.current - 1; + } else if (pn === 'N') { + pageNo = this.state.current + 1; + } + this.setState({ + current: pageNo, + }); + this.props.onPageChange(pageNo); + } + + public renderPagination = (): JSX.Element => { + let pageObj = this.props.page; + + if (pageObj === undefined) { + return null; + } + let keyIdx = 0; + let totalPage = pageObj.totalPages; + let currPage = pageObj.number; + let elems = new Array; + let prevIndicator = pageObj.first ? + : + + + ; + elems.push(prevIndicator); + for (let i = 0; i < totalPage; i++) { + if (i === currPage) { + elems.push({i + 1}); + } else { + elems.push({i + 1}); + } + } + let nextIndicator = pageObj.last ? + : + + + ; + elems.push(nextIndicator); + + return + {elems} + ; + } + + public render(): JSX.Element { + return ( +
+ {this.renderPagination()} +
+ ); + } +} diff --git a/src/ts/@overflow/history/react/components/HistoryList.tsx b/src/ts/@overflow/history/react/components/HistoryList.tsx index bcffc04..addb9f3 100644 --- a/src/ts/@overflow/history/react/components/HistoryList.tsx +++ b/src/ts/@overflow/history/react/components/HistoryList.tsx @@ -5,6 +5,8 @@ import Probe from '@overflow/probe/api/model/Probe'; import History from '@overflow/history/api/model/History'; import Page from '@overflow/commons/api/model/Page'; import MetaHistoryType from '@overflow/meta/api/model/MetaHistoryType'; +import { Pager } from '@overflow/commons/react/component/Pager'; + export interface StateProps { histories: Page; @@ -19,7 +21,6 @@ export interface DispatchProps { export type Props = StateProps & DispatchProps; export interface State { - page: number; isTypedHistory: boolean; } @@ -30,7 +31,6 @@ export class HistoryList extends React.Component { constructor(props: Props, context: State) { super(props, context); this.state = { - page: 0, isTypedHistory: false, }; } @@ -85,76 +85,16 @@ export class HistoryList extends React.Component { } public handlePaging = (pageNo: number) => { - if (pageNo === this.state.page) { - return; - } - this.setState({ - page: pageNo, - }); let probe: Probe = { id: Number(1), }; if (this.state.isTypedHistory) { this.getTypedHistory(pageNo); } else { - // this.props.onReadAllByProbe(probe, String(pageNo), String(this.countPerPage)); this.getAllHistory(pageNo); } } - public handlePrevPage = (pn: string) => { - let p = 0; - if (pn === 'P') { - p = this.state.page - 1; - } else if (pn === 'N') { - p = this.state.page + 1; - } - this.setState({ - page: p, - }); - let probe: Probe = { - id: Number(1), - }; - if (this.state.isTypedHistory) { - this.getTypedHistory(p); - } else { - this.getAllHistory(p); - } - } - - public renderPagination = (pageObj: Page): (JSX.Element | JSX.Element[]) => { - if (pageObj === undefined) { - return null; - } - let keyIdx = 0; - let totalPage = pageObj.totalPages; - let currPage = pageObj.number; - let elems = new Array; - let prevIndicator = pageObj.first ? - : - - - ; - elems.push(prevIndicator); - for (let i = 0; i < totalPage; i++) { - if (i === currPage) { - elems.push({i + 1}); - } else { - elems.push({i + 1}); - } - } - let nextIndicator = pageObj.last ? - : - - - ; - elems.push(nextIndicator); - - return - {elems} - ; - } - public render(): JSX.Element { let historyList: JSX.Element = ( @@ -192,7 +132,7 @@ export class HistoryList extends React.Component { - {this.renderPagination(this.props.histories)} + diff --git a/src/ts/@overflow/target/react/components/TargetList.tsx b/src/ts/@overflow/target/react/components/TargetList.tsx index b3d6d6a..0f934e9 100644 --- a/src/ts/@overflow/target/react/components/TargetList.tsx +++ b/src/ts/@overflow/target/react/components/TargetList.tsx @@ -8,7 +8,7 @@ import { ListContainer } from '@overflow/commons/react/component/ListContainer'; import DiscoveryContainer from '@overflow/discovery/react/Discovery'; import Host from '@overflow/discovery/api/model/Host'; import Page from '@overflow/commons/api/model/Page'; - +import { Pager } from '@overflow/commons/react/component/Pager'; import * as Utils from '@overflow/commons/util/Utils'; export interface StateProps { @@ -27,7 +27,6 @@ export type Props = StateProps & DispatchProps; export interface State { selected: Infra; openAddTarget: boolean; - page: number; } export class TargetList extends React.Component { @@ -38,26 +37,25 @@ export class TargetList extends React.Component { this.state = { selected: null, openAddTarget: false, - page: 0, }; } public componentWillMount(): void { - this.getTargetList(); + this.getTargetList(0); } - public getTargetList(): void { + public getTargetList(pageNo: number): void { if (this.props.probeId === undefined) { // FIXME: get domain let domain: Domain = { id: 1, }; - this.props.onReadAllByDomain(domain, String(0), String(this.countPerPage)); + this.props.onReadAllByDomain(domain, String(pageNo), String(this.countPerPage)); } else { let probe: Probe = { id: Number(this.props.probeId), }; - this.props.onReadAllByProbe(probe, String(0), String(this.countPerPage)); + this.props.onReadAllByProbe(probe, String(pageNo), String(this.countPerPage)); } } @@ -90,7 +88,11 @@ export class TargetList extends React.Component { public onRefreshList = () => { console.log('onRefreshList'); this.setState({ openAddTarget: false }); - this.getTargetList(); + this.getTargetList(0); + } + + public handlePaging = (pageNo: number) => { + this.getTargetList(pageNo); } public handleFilter(filterStr: string): void { @@ -141,6 +143,13 @@ export class TargetList extends React.Component { ))} + + + + + + +