import React, { Component, ChangeEvent } from 'react'; import { RouteComponentProps, StaticContext } from 'react-router'; import { Link } from 'react-router-dom'; import qs from 'query-string'; import styles from './DatabaseList.module.css'; import treeJson from '../assets/tree.json'; import iconFolder from '../assets/images/database/icon_folder.gif'; interface ITreeJson { id: number; title: string; num_of_items: number; children: ITreeJson[]; } interface IIndexBase { id: number; title: string; } interface IIndex extends IIndexBase { num_of_indexes: number; num_of_items: number; } interface IIndexDetail extends IIndex { path: IIndexBase[]; children: IIndex[]; } interface IProps extends RouteComponentProps<{ id: string }, StaticContext, any> { } interface IState { index: IIndexDetail; orderby: string; order_dir: number; page: number; itemcount: number; itemElement: JSX.Element[]; } const PUBCIC_INDEX_ID = 3; const getIndexUrl = (id: number): string => { return '/database/list/' + String(id); } const IndexNode = (index: IIndex): JSX.Element => { const url = getIndexUrl(index.id); return (
{index.title} {index.title}
{index.num_of_indexes} indexes / {index.num_of_items} items
); }; class DatabaseList extends Component { public state: IState = { index: { id: 0, title: '', num_of_indexes: 0, num_of_items: 0, path: [], children: [] }, orderby: 'title', order_dir: 0, itemcount: 20, page: 1, itemElement: [] }; constructor(props: IProps) { super(props); const { params } = this.props.match; const query = qs.parse(this.props.location.search); const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID; const indexInfo = this.getIndexInfo(indexId); this.state.index = indexInfo; if (typeof query.orderby === 'string') { this.state.orderby = query.orderby; } if (typeof query.order_dir === 'string') { this.state.order_dir = parseInt(query.order_dir, 10); } if (typeof query.itemcount === 'string') { this.state.itemcount = parseInt(query.itemcount, 10); } if (typeof query.page === 'string') { this.state.page = parseInt(query.page, 10); } this.handleSelectOrderby = this.handleSelectOrderby.bind(this); this.handleSelectItemcount = this.handleSelectItemcount.bind(this); } getIndexId(): number { return this.state.index.id; } componentWillReceiveProps(nextProps: IProps) { const { params } = nextProps.match; const query = qs.parse(nextProps.location.search); const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID; const index = this.getIndexInfo(indexId); if (typeof query.orderby === 'string') { this.setState({ orderby: query.orderby }); } if (typeof query.order_dir === 'string') { this.setState({ order_dir: parseInt(query.order_dir, 10) }); } if (typeof query.itemcount === 'string') { this.setState({ itemcount: parseInt(query.itemcount, 10) }); } this.setState({ index }); } getLinkUrl(query: { orderby?: string, order_dir?: number, itemcount?: number, page?: number }): string { const newOrderby = query.orderby ? query.orderby : this.state.orderby; const newOrderDir = query.order_dir ? query.order_dir : this.state.order_dir; const newItemcount = query.itemcount ? query.itemcount : this.state.itemcount; const newPage = query.page ? query.page : this.state.page; const url = getIndexUrl(this.getIndexId()); let params: string[] = []; if ('title' !== newOrderby) { params.push('orderby=' + newOrderby); } if (0 !== newOrderDir) { params.push('order_dir=' + String(newOrderDir)); } if (20 !== newItemcount) { params.push('itemcount=' + String(newItemcount)); } if (1 !== newPage) { params.push('page=' + String(newPage)); } return (params.length > 0 ? url + '?' + params.join('&') : url); } handleSelectOrderby(event: ChangeEvent) { const target = event.target; const value = target.value; const url = this.getLinkUrl({ orderby: value }); this.props.history.push(url); } handleSelectItemcount(event: ChangeEvent) { const target = event.target; const value = parseInt(target.value, 10); const url = this.getLinkUrl({ itemcount: value }); this.props.history.push(url); } getIndexInfo(indexId: number): IIndexDetail { let index: IIndexDetail = { id: indexId, title: '', num_of_indexes: 0, num_of_items: 0, path: [], children: [] }; let found: boolean = false; const loop = (data: ITreeJson[]) => { data.forEach((item: ITreeJson) => { if (found) { return; } const path: IIndexBase = { id: item.id, title: item.title, } index.path.push(path); if (indexId === item.id) { index.id = item.id; index.title = item.title; index.num_of_indexes = item.children.length; index.num_of_items = item.num_of_items; found = true; item.children.forEach((item: ITreeJson) => { index.children.push({ id: item.id, title: item.title, num_of_indexes: item.children.length, num_of_items: item.num_of_items }); }); } else { loop(item.children); } if (found === false) { index.path.pop(); } }); }; loop(treeJson); return index; } renderIndexPath(): JSX.Element { let elements: JSX.Element[] = []; this.state.index.path.forEach((path: IIndexBase) => { const url: string = getIndexUrl(path.id); elements.push(/ {path.title} ); }); return
{elements}
; } renderIndexNodeList(): JSX.Element { if (this.state.index.children.length === 0) { return (<>); } let evenodd: string = 'even'; const treeList = this.state.index.children.map((cindex: IIndex) => { evenodd = evenodd === 'even' ? 'odd' : 'even'; return ( {IndexNode(cindex)} ); }); return ( {treeList}
); } renderSortCriteria(): JSX.Element { if (this.state.index.num_of_items === 0) { return (<>); } let orderDir = <>; if (this.state.order_dir === 1) { const url = this.getLinkUrl({ order_dir: 0 }); orderDir = ( ▼ ▲ ); } else { const url = this.getLinkUrl({ order_dir: 1 }); orderDir = ( ▼ ▲ ); } return (
Order by   {orderDir} No.Item per page 
); } reunderNumOfItems(): JSX.Element { if (this.state.index.num_of_items === 0) { return (

no items found

); } let startNum: number = 1 + this.state.itemcount * (this.state.page - 1); let endNum: number = this.state.itemcount * this.state.page; if (startNum > this.state.index.num_of_items) { return (

out of range

); } if (endNum > this.state.index.num_of_items) { endNum = this.state.index.num_of_items; } return (

{startNum} - {endNum} of {this.state.index.num_of_items}

); } reunderPageNavi(): JSX.Element { if (this.state.index.num_of_items === 0) { return (<>); } const maxPage = (this.state.index.num_of_items / this.state.itemcount) + 1; const link = (title: string, page: number) => { if (page === 0 || page === this.state.page) { return ({title}); } const url: string = this.getLinkUrl({ page: page }); return ({title}); } const startPage = (this.state.page - 5) > 1 ? this.state.page - 5 : 1; const endPage = (startPage + 5) > maxPage ? maxPage : startPage + 5; let pageLinks: JSX.Element[] = []; pageLinks.push(link('PREV', this.state.page - 1)); for (let i = startPage; i <= endPage; i++) { pageLinks.push(link(String(i), i)); } pageLinks.push(link('NEXT', this.state.page === maxPage ? 0 : this.state.page + 1)); return (
{pageLinks}
); } render() { const pageNavi = this.reunderPageNavi(); return (

Listing item

{this.renderIndexPath()} {this.renderIndexNodeList()} {this.renderSortCriteria()} {this.reunderNumOfItems()} {pageNavi}
BODY
{pageNavi}
); /*
*/ } } export default DatabaseList;