'use strict'; import React from 'react'; import Router from 'react-router'; import PieceListStore from '../stores/piece_list_store'; import PieceListActions from '../actions/piece_list_actions'; 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'; import Pagination from './ascribe_pagination/pagination'; import GlobalAction from './global_action'; import PieceListBulkModal from './ascribe_piece_list_bulk_modal/piece_list_bulk_modal'; import PieceListToolbar from './ascribe_piece_list_toolbar/piece_list_toolbar'; import AppConstants from '../constants/application_constants'; import { mergeOptions } from '../utils/general_utils'; let PieceList = React.createClass({ propTypes: { accordionListItemType: React.PropTypes.func, redirectTo: React.PropTypes.string, customSubmitButton: React.PropTypes.element, filterParams: React.PropTypes.array, orderParams: React.PropTypes.array, orderBy: React.PropTypes.string }, mixins: [Router.Navigation, Router.State], getDefaultProps() { return { accordionListItemType: AccordionListItemWallet, orderParams: ['artist_name', 'title'], filterParams: [ 'acl_transfer', 'acl_consign', { key: 'acl_create_editions', label: 'create editions' }] }; }, getInitialState() { return mergeOptions( PieceListStore.getState(), EditionListStore.getState() ); }, componentDidMount() { let page = this.getQuery().page || 1; PieceListStore.listen(this.onChange); EditionListStore.listen(this.onChange); let orderBy = this.props.orderBy ? this.props.orderBy : this.state.orderBy; 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) .then(() => PieceListActions.fetchPieceRequestActions()); } }, componentDidUpdate() { if (this.props.redirectTo && this.state.pieceListCount === 0) { // FIXME: hack to redirect out of the dispatch cycle window.setTimeout(() => this.transitionTo(this.props.redirectTo), 0); } }, componentWillUnmount() { PieceListStore.unlisten(this.onChange); EditionListStore.unlisten(this.onChange); }, onChange(state) { this.setState(state); }, paginationGoToPage(page) { 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, this.state.orderBy, this.state.orderAsc, this.state.filterBy); }; }, getPagination() { let currentPage = parseInt(this.getQuery().page, 10) || 1; let totalPages = Math.ceil(this.state.pieceListCount / this.state.pageSize); if (this.state.pieceListCount > 10) { return ( ); } }, searchFor(searchTerm) { PieceListActions.fetchPieceList(1, this.state.pageSize, searchTerm, this.state.orderBy, this.state.orderAsc, this.state.filterBy); this.transitionTo(this.getPathname(), {page: 1}); }, applyFilterBy(filterBy){ // first we need to apply the filter on the piece list PieceListActions.fetchPieceList(1, this.state.pageSize, this.state.search, 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 }); } }); }); // we have to redirect the user always to page one as it could be that there is no page two // for filtered pieces this.transitionTo(this.getPathname(), {page: 1}); }, applyOrderBy(orderBy) { PieceListActions.fetchPieceList(this.state.page, this.state.pageSize, this.state.search, orderBy, this.state.orderAsc, this.state.filterBy); }, render() { let loadingElement = (); let AccordionListItemType = this.props.accordionListItemType; // return (
{this.props.customSubmitButton} {this.state.pieceList.map((piece, i) => { return ( ); })} {this.getPagination()}
); } }); export default PieceList;