114 lines
4.0 KiB
TypeScript
114 lines
4.0 KiB
TypeScript
import React, { Component, ChangeEvent } from 'react';
|
|
import { RouteComponentProps, StaticContext } from 'react-router';
|
|
import Index, { IIndexDetail, PUBCIC_INDEX_ID } from '../common/Index';
|
|
import Item, { ISortCondition, SORT_CONDITION_DEFAULT } from '../common/Item';
|
|
import DatabaseListItem from './DatabaseListItem';
|
|
import styles from './DatabaseList.module.css';
|
|
|
|
interface IProps extends RouteComponentProps<{ id: string }, StaticContext, any> { }
|
|
|
|
interface IState {
|
|
index: IIndexDetail;
|
|
condition: ISortCondition;
|
|
items: {
|
|
total: number;
|
|
data: any[];
|
|
}
|
|
}
|
|
|
|
class DatabaseList extends Component<IProps, IState> {
|
|
|
|
public state: IState = {
|
|
index: {
|
|
id: 0,
|
|
title: '',
|
|
num_of_indexes: 0,
|
|
num_of_items: 0,
|
|
path: [],
|
|
children: []
|
|
},
|
|
condition: Object.assign({}, SORT_CONDITION_DEFAULT),
|
|
items: {
|
|
total: 0,
|
|
data: []
|
|
}
|
|
};
|
|
|
|
constructor(props: IProps) {
|
|
super(props);
|
|
const { params } = this.props.match;
|
|
const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID;
|
|
this.state.index = Index.getDetail(indexId);
|
|
this.state.condition = Item.getSortConditionByQuery(this.props.location.search);
|
|
this.state.items = Item.getListByIndexId(this.state.index.id, this.state.condition);
|
|
this.handleSelectOrderby = this.handleSelectOrderby.bind(this);
|
|
this.handleSelectItemcount = this.handleSelectItemcount.bind(this);
|
|
}
|
|
|
|
componentWillReceiveProps(nextProps: IProps) {
|
|
const { params } = nextProps.match;
|
|
const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID;
|
|
const index = Index.getDetail(indexId);
|
|
const condition = Item.getSortConditionByQuery(nextProps.location.search);
|
|
const items = Item.getListByIndexId(indexId, condition);
|
|
this.setState({ index, condition, items });
|
|
}
|
|
|
|
getIndexId(): number {
|
|
return this.state.index.id;
|
|
}
|
|
|
|
handleSelectOrderby(event: ChangeEvent<HTMLSelectElement>) {
|
|
const target = event.target;
|
|
const value = target.value;
|
|
const baseUrl = Index.getUrl(this.getIndexId());
|
|
const url = Item.getListUrl(baseUrl, this.state.condition, { orderby: value });
|
|
this.props.history.push(url);
|
|
}
|
|
|
|
handleSelectItemcount(event: ChangeEvent<HTMLSelectElement>) {
|
|
const target = event.target;
|
|
const value = parseInt(target.value, 10);
|
|
const baseUrl = Index.getUrl(this.getIndexId());
|
|
const url = Item.getListUrl(baseUrl, this.state.condition, { limit: value });
|
|
this.props.history.push(url);
|
|
}
|
|
|
|
renderItemList(): JSX.Element {
|
|
let evenodd: string = 'even';
|
|
const itemList = this.state.items.data.map((item) => {
|
|
evenodd = evenodd === 'even' ? 'odd' : 'even';
|
|
return (<tr key={item.item_id} className={evenodd}><td><DatabaseListItem id={item.item_id} /></td></tr>);
|
|
});
|
|
if (itemList.length === 0) {
|
|
return (<></>);
|
|
}
|
|
return (
|
|
<table className={styles.listTable}>
|
|
<tbody>
|
|
{itemList}
|
|
</tbody>
|
|
</table>
|
|
);
|
|
}
|
|
|
|
render() {
|
|
const baseUrl = Index.getUrl(this.getIndexId());
|
|
const pageNavi = Item.renderPageNavi(baseUrl, this.state.items.total, this.state.condition);
|
|
return (
|
|
<div className={styles.list}>
|
|
<h3>Listing item</h3>
|
|
{Index.renderIndexPath(this.state.index)}
|
|
{Index.renderList(this.state.index)}
|
|
{Item.renderSortCriteria(baseUrl, this.state.items.total, this.state.condition, this.handleSelectOrderby, this.handleSelectItemcount)}
|
|
{Item.renderNumOfItems(this.state.items.total, this.state.condition)}
|
|
{pageNavi}
|
|
{this.renderItemList()}
|
|
{pageNavi}
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default DatabaseList;
|