Files
pupil.brain.riken.jp/src/components/database/DatabaseSearchByIndexId.tsx
T

71 lines
2.3 KiB
TypeScript

import React, { Component } from 'react';
import { RouteComponentProps } from 'react-router';
import { Link } from 'react-router-dom';
import IndexUtil, { Index, INDEX_ID_PUBLIC } from '../../common/IndexUtil';
import ItemUtil, { SortCondition, SearchCallbackFunc } from '../../common/ItemUtil';
import DatabaseListIndex from './DatabaseListIndex';
import DatabaseListItem from './DatabaseListItem';
interface Props extends RouteComponentProps<{ id: string }> { }
interface State {
index: Index | null;
}
class DatabaseSearchByIndexId extends Component<Props, State> {
constructor(props: Props) {
super(props);
const { params } = this.props.match;
const indexId = params.id ? parseInt(params.id, 10) : INDEX_ID_PUBLIC;
this.state = {
index: IndexUtil.get(indexId),
}
this.search = this.search.bind(this);
}
componentWillReceiveProps(nextProps: Props) {
const { params } = nextProps.match;
const indexId = params.id ? parseInt(params.id, 10) : INDEX_ID_PUBLIC;
const index = IndexUtil.get(indexId);
this.setState({ index });
}
getUrl() {
if (this.state.index === null) {
return '/';
}
return IndexUtil.getUrl(this.state.index.id);
}
async search(condition: SortCondition, callback: SearchCallbackFunc) {
if (this.state.index === null) {
return { total: 0, data: [] };
}
const items = ItemUtil.getListByIndexId(this.state.index.id, condition, callback);
return items;
}
render() {
if (this.state.index === null) {
return null;
}
const baseUrl = this.getUrl();
const pIndexes = IndexUtil.getParents(this.state.index.id);
const parents = pIndexes.map((value: Index) => {
const url: string = IndexUtil.getUrl(value.id);
return <span key={value.id}>/&nbsp;<Link to={url}>{value.title}</Link>&nbsp;</span>
});
return (
<div className="list">
<h3>Listing item</h3>
<div>{parents}</div>
<DatabaseListIndex index={this.state.index} />
<DatabaseListItem url={baseUrl} search={this.search} />
</div>
);
}
}
export default DatabaseSearchByIndexId;