// eslint-disable-next-line no-unused-vars
class EnhanceInstructionPage extends React.Component {
constructor(props) {
super(props);
let initTab = () => { return { current_page: 1, total_pages: 1, num_items: 0, total_hits: 0, items: [] }; };
this.state = { tabs: _.times(2, initTab) };
this.state = this.buildStateFromProps(props);
}
buildStateFromProps(props) {
return {
items: props.results,
current_page: props.pagination.current_page,
per_page: props.pagination.per_page,
order: props.pagination.order,
filterbar: props.filterbar,
activeTab: props.tab,
tabs: this.updateTabs(props)
};
}
updateTabs(props) {
this.state.tabs[props.tab] = {
current_page: props.pagination.current_page,
total_pages: props.pagination.total_pages,
num_items: props.pagination.num_items,
total_hits: props.pagination.total_hits,
items: props.results
};
return this.state.tabs;
}
fetch(newState) {
const tab = newState.activeTab;
const current_page = newState.current_page || newState.tabs[tab].current_page;
const url = Routes.lcms_engine_enhance_instruction_index_path({
per_page: newState.per_page,
order: newState.order,
page: current_page,
tab: tab,
...newState.filterbar
});
return $.getJSON(url).then(x => {
this.setState(this.buildStateFromProps(x));
});
}
handlePageClick(data) {
const selected = data.selected;
const newState = _.assign({}, this.state, { current_page: selected + 1 });
this.fetch(newState);
}
handleChangePerPage(event) {
const newPerPage = event.target.value;
const newState = _.assign({}, this.state, { per_page: newPerPage, current_page: 1 });
this.fetch(newState);
}
handleTabChange(idxTab) {
if (idxTab !== (this.state.activeTab + 1)) {
const newState = _.assign({}, this.state, { activeTab: idxTab - 1, current_page: 1 });
this.fetch(newState);
}
}
componentWillMount() {
urlHistory.emptyState();
}
componentWillUpdate(_nextProps, nextState) {
urlHistory.updatePaginationParams(nextState);
}
renderTab(title, idx) {
const tabData = this.state.tabs[idx];
return (