'use strict'; import React from 'react'; import classNames from 'classnames'; import EditionListActions from '../../actions/edition_list_actions'; import EditionListStore from '../../stores/edition_list_store'; import PieceListStore from '../../stores/piece_list_store'; import Button from 'react-bootstrap/lib/Button'; import CreateEditionsButton from '../ascribe_buttons/create_editions_button'; import AscribeSpinner from '../ascribe_spinner'; import { mergeOptions } from '../../utils/general_utils'; import { getLangText } from '../../utils/lang_utils'; let AccordionListItemEditionWidget = React.createClass({ propTypes: { piece: React.PropTypes.object.isRequired, toggleCreateEditionsDialog: React.PropTypes.func.isRequired, className: React.PropTypes.string, onPollingSuccess: React.PropTypes.func }, getInitialState() { return mergeOptions( EditionListStore.getState(), PieceListStore.getState() ); }, componentDidMount() { EditionListStore.listen(this.onChange); PieceListStore.listen(this.onChange); }, componentWillUnmount() { EditionListStore.unlisten(this.onChange); PieceListStore.unlisten(this.onChange); }, onChange(state) { this.setState(state); }, /** * Calls the store to either show or hide the editionListTable */ toggleTable() { const { piece: { id: pieceId } } = this.props; const { filterBy, isEditionListOpenForPieceId } = this.state; const isEditionListOpen = isEditionListOpenForPieceId[pieceId] ? isEditionListOpenForPieceId[pieceId].show : false; if (isEditionListOpen) { EditionListActions.toggleEditionList(pieceId); } else { EditionListActions.toggleEditionList(pieceId); EditionListActions.fetchEditionList({ pieceId, filterBy }); } }, /** * Depending on the state of isEditionListOpenForPieceId we either want to display * a glyphicon arrow pointing upwards or downwards */ getGlyphicon() { let pieceId = this.props.piece.id; let isEditionListOpen = this.state.isEditionListOpenForPieceId[pieceId] ? this.state.isEditionListOpenForPieceId[pieceId].show : false; if(isEditionListOpen) { // this is the loading feedback for the editions // button. // // PLEASE FUTURE TIM, DO NOT FUCKING REMOVE IT AGAIN! if(typeof this.state.editionList[pieceId] === 'undefined') { return ( ); } else { return ( ); } } else { return ( ); } }, render() { let piece = this.props.piece; let numEditions = piece.num_editions; if(numEditions <= 0) { if (piece.acl.acl_create_editions){ return ( ); } else { return null; } } else { if(piece.first_edition === null) { // user has deleted all his editions and only the piece is showing return ( ); } else { let editionMapping = piece && piece.first_edition ? piece.first_edition.num_editions_available + '/' + piece.num_editions : ''; return ( ); } } } }); export default AccordionListItemEditionWidget;