1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 01:25:17 +01:00
onion/js/components/piece_list.js

214 lines
8.5 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
import { History } from 'react-router';
import PieceListStore from '../stores/piece_list_store';
import PieceListActions from '../actions/piece_list_actions';
2015-05-20 15:22:29 +02:00
2015-08-07 13:05:50 +02:00
import EditionListStore from '../stores/edition_list_store';
import EditionListActions from '../actions/edition_list_actions';
import AccordionList from './ascribe_accordion_list/accordion_list';
import AccordionListItemWallet from './ascribe_accordion_list/accordion_list_item_wallet';
import AccordionListItemTableEditions from './ascribe_accordion_list/accordion_list_item_table_editions';
2015-05-27 13:57:11 +02:00
import Pagination from './ascribe_pagination/pagination';
import PieceListFilterDisplay from './piece_list_filter_display';
import PieceListBulkModal from './ascribe_piece_list_bulk_modal/piece_list_bulk_modal';
2015-06-02 10:10:09 +02:00
import PieceListToolbar from './ascribe_piece_list_toolbar/piece_list_toolbar';
2015-10-08 11:12:15 +02:00
import AscribeSpinner from './ascribe_spinner';
2015-08-07 13:05:50 +02:00
import { mergeOptions } from '../utils/general_utils';
import { getLangText } from '../utils/lang_utils';
import { setDocumentTitle } from '../utils/dom_utils';
2015-05-20 16:44:45 +02:00
2015-05-19 17:16:01 +02:00
let PieceList = React.createClass({
propTypes: {
2015-08-11 17:12:12 +02:00
accordionListItemType: React.PropTypes.func,
2015-07-14 21:13:15 +02:00
redirectTo: React.PropTypes.string,
2015-08-14 14:58:23 +02:00
customSubmitButton: React.PropTypes.element,
filterParams: React.PropTypes.array,
orderParams: React.PropTypes.array,
orderBy: React.PropTypes.string,
location: React.PropTypes.object
},
mixins: [History],
2015-06-16 09:27:04 +02:00
getDefaultProps() {
return {
2015-08-14 14:58:23 +02:00
accordionListItemType: AccordionListItemWallet,
orderParams: ['artist_name', 'title'],
2015-09-16 23:36:21 +02:00
filterParams: [{
label: getLangText('Show works I can'),
items: [
'acl_transfer',
'acl_consign',
'acl_create_editions'
]
2015-09-16 23:36:21 +02:00
}]
};
},
2015-05-21 12:12:25 +02:00
getInitialState() {
2015-08-07 13:05:50 +02:00
return mergeOptions(
PieceListStore.getState(),
EditionListStore.getState()
);
2015-05-21 12:12:25 +02:00
},
componentDidMount() {
let page = this.props.location.query.page || 1;
2015-08-07 13:05:50 +02:00
2015-05-26 11:47:56 +02:00
PieceListStore.listen(this.onChange);
2015-08-07 13:05:50 +02:00
EditionListStore.listen(this.onChange);
let orderBy = this.props.orderBy ? this.props.orderBy : this.state.orderBy;
2015-08-21 16:14:15 +02:00
if (this.state.pieceList.length === 0 || this.state.page !== page){
PieceListActions.fetchPieceList(page, this.state.pageSize, this.state.search,
orderBy, this.state.orderAsc, this.state.filterBy);
}
},
2015-07-13 22:05:16 +02:00
componentDidUpdate() {
if (this.props.redirectTo && this.state.unfilteredPieceListCount === 0) {
// FIXME: hack to redirect out of the dispatch cycle
2015-10-01 11:24:19 +02:00
window.setTimeout(() => this.history.pushState(null, this.props.redirectTo, this.props.location.query), 0);
2015-07-13 22:05:16 +02:00
}
},
2015-05-22 17:11:17 +02:00
componentWillUnmount() {
PieceListStore.unlisten(this.onChange);
2015-08-07 13:05:50 +02:00
EditionListStore.unlisten(this.onChange);
2015-05-22 17:11:17 +02:00
},
2015-05-26 13:14:35 +02:00
onChange(state) {
this.setState(state);
2015-05-22 17:11:17 +02:00
},
2015-05-22 12:58:06 +02:00
paginationGoToPage(page) {
2015-08-07 13:05:50 +02:00
return () => {
// if the users clicks a pager of the pagination,
// the site should go to the top
document.body.scrollTop = document.documentElement.scrollTop = 0;
PieceListActions.fetchPieceList(page, this.state.pageSize, this.state.search,
2015-09-16 23:36:21 +02:00
this.state.orderBy, this.state.orderAsc,
this.state.filterBy);
2015-08-07 13:05:50 +02:00
};
},
getPagination() {
let currentPage = parseInt(this.props.location.query.page, 10) || 1;
let totalPages = Math.ceil(this.state.pieceListCount / this.state.pageSize);
2015-10-09 02:00:02 +02:00
if (this.state.pieceListCount > 20) {
return (
<Pagination
currentPage={currentPage}
totalPages={totalPages}
goToPage={this.paginationGoToPage} />
);
}
},
2015-06-16 09:27:04 +02:00
searchFor(searchTerm) {
PieceListActions.fetchPieceList(1, this.state.pageSize, searchTerm, this.state.orderBy,
this.state.orderAsc, this.state.filterBy);
this.history.pushState(null, this.props.location.pathname, {page: 1});
2015-06-16 09:27:04 +02:00
},
2015-08-14 14:58:23 +02:00
applyFilterBy(filterBy){
2015-08-07 13:05:50 +02:00
// first we need to apply the filter on the piece list
2015-08-06 13:15:52 +02:00
PieceListActions.fetchPieceList(1, this.state.pageSize, this.state.search,
2015-08-07 13:05:50 +02:00
this.state.orderBy, this.state.orderAsc, filterBy)
.then(() => {
// but also, we need to filter all the open edition lists
this.state.pieceList
.forEach((piece) => {
// but only if they're actually open
if(this.state.isEditionListOpenForPieceId[piece.id].show) {
EditionListActions.refreshEditionList({
pieceId: piece.id,
filterBy
});
}
});
});
2015-08-06 13:15:52 +02:00
// we have to redirect the user always to page one as it could be that there is no page two
// for filtered pieces
this.history.pushState(null, this.props.location.pathname, {page: 1});
2015-08-06 13:15:52 +02:00
},
applyOrderBy(orderBy) {
PieceListActions.fetchPieceList(this.state.page, this.state.pageSize, this.state.search,
2015-08-14 14:58:23 +02:00
orderBy, this.state.orderAsc, this.state.filterBy);
2015-05-22 12:58:06 +02:00
},
render() {
let loadingElement = <AscribeSpinner color='dark-blue' size='lg'/>;
let AccordionListItemType = this.props.accordionListItemType;
2015-08-28 15:24:32 +02:00
setDocumentTitle(getLangText('Collection'));
return (
<div>
2015-07-14 18:54:47 +02:00
<PieceListToolbar
className="ascribe-piece-list-toolbar"
2015-08-06 13:15:52 +02:00
searchFor={this.searchFor}
searchQuery={this.state.search}
2015-08-14 14:58:23 +02:00
filterParams={this.props.filterParams}
orderParams={this.props.orderParams}
2015-08-06 13:15:52 +02:00
filterBy={this.state.filterBy}
2015-08-14 14:58:23 +02:00
orderBy={this.state.orderBy}
applyFilterBy={this.applyFilterBy}
applyOrderBy={this.applyOrderBy}>
2015-10-12 12:08:53 +02:00
{this.props.customSubmitButton ?
this.props.customSubmitButton :
<button className="btn btn-default btn-ascribe-add">
<span className="icon-ascribe icon-ascribe-add" />
</button>
2015-10-12 12:08:53 +02:00
}
2015-07-14 21:13:15 +02:00
</PieceListToolbar>
<PieceListBulkModal className="ascribe-piece-list-bulk-modal" />
2015-09-16 23:36:21 +02:00
<PieceListFilterDisplay
filterBy={this.state.filterBy}
filterParams={this.props.filterParams}/>
<AccordionList
className="ascribe-accordion-list"
changeOrder={this.accordionChangeOrder}
itemList={this.state.pieceList}
2015-07-07 09:22:46 +02:00
count={this.state.pieceListCount}
orderBy={this.state.orderBy}
orderAsc={this.state.orderAsc}
search={this.state.search}
searchFor={this.searchFor}
page={this.state.page}
2015-06-16 09:57:14 +02:00
pageSize={this.state.pageSize}
loadingElement={loadingElement}>
{this.state.pieceList.map((piece, i) => {
return (
<AccordionListItemType
className="col-xs-12 col-sm-10 col-md-8 col-lg-8 col-sm-offset-1 col-md-offset-2 col-lg-offset-2 ascribe-accordion-list-item"
content={piece}
key={i}>
<AccordionListItemTableEditions
2015-07-09 16:29:10 +02:00
className="ascribe-accordion-list-item-table col-xs-12 col-sm-10 col-md-8 col-lg-8 col-sm-offset-1 col-md-offset-2 col-lg-offset-2"
parentId={piece.id} />
</AccordionListItemType>
);
})}
</AccordionList>
{this.getPagination()}
</div>
);
}
});
export default PieceList;