1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/ascribe_buttons/acl_button_list.js

126 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-06-05 15:17:35 +02:00
'use strict';
import React from 'react/addons';
2015-06-03 10:27:11 +02:00
import UserActions from '../../actions/user_actions';
import UserStore from '../../stores/user_store';
import ConsignButton from './acls/consign_button';
import EmailButton from './acls/email_button';
import LoanButton from './acls/loan_button';
import LoanRequestButton from './acls/loan_request_button';
import TransferButton from './acls/transfer_button';
import UnconsignButton from './acls/unconsign_button';
2015-06-03 10:27:11 +02:00
import { mergeOptions } from '../../utils/general_utils';
2015-06-03 10:27:11 +02:00
let AclButtonList = React.createClass({
2015-06-05 15:17:35 +02:00
propTypes: {
2015-06-05 16:20:28 +02:00
className: React.PropTypes.string,
pieceOrEditions: React.PropTypes.oneOfType([
2015-07-13 17:09:44 +02:00
React.PropTypes.object,
React.PropTypes.array
]).isRequired,
availableAcls: React.PropTypes.object.isRequired,
2015-09-28 22:09:25 +02:00
buttonsStyle: React.PropTypes.object,
handleSuccess: React.PropTypes.func.isRequired,
2015-07-13 17:09:44 +02:00
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element
])
2015-06-03 10:27:11 +02:00
},
2015-09-29 10:31:13 +02:00
2015-06-05 15:17:35 +02:00
getInitialState() {
return mergeOptions(
UserStore.getState(),
{
buttonListSize: 0
}
);
2015-06-03 10:27:11 +02:00
},
2015-06-03 11:49:39 +02:00
componentDidMount() {
2015-06-03 10:27:11 +02:00
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser.defer();
window.addEventListener('resize', this.handleResize);
window.dispatchEvent(new Event('resize'));
2015-06-03 10:27:11 +02:00
},
2015-10-23 11:24:26 +02:00
componentDidUpdate(prevProps) {
if(prevProps.availableAcls && prevProps.availableAcls !== this.props.availableAcls) {
window.dispatchEvent(new Event('resize'));
}
},
2015-06-05 15:17:35 +02:00
componentWillUnmount() {
2015-06-03 10:27:11 +02:00
UserStore.unlisten(this.onChange);
window.removeEventListener('resize', this.handleResize);
},
handleResize() {
this.setState({
buttonListSize: this.refs.buttonList.getDOMNode().offsetWidth
});
2015-06-03 10:27:11 +02:00
},
2015-06-03 11:49:39 +02:00
2015-06-05 15:17:35 +02:00
onChange(state) {
this.setState(state);
},
renderChildren() {
const { children } = this.props;
const { buttonListSize } = this.state;
return React.Children.map(children, (child) => {
return React.addons.cloneWithProps(child, { buttonListSize });
});
},
2015-06-03 10:27:11 +02:00
render() {
const { className,
buttonsStyle,
availableAcls,
pieceOrEditions,
handleSuccess } = this.props;
const { currentUser } = this.state;
2015-06-03 11:49:39 +02:00
return (
<div className={className}>
<span ref="buttonList" style={buttonsStyle}>
<EmailButton
availableAcls={availableAcls}
pieceOrEditions={pieceOrEditions}
currentUser={currentUser}
handleSuccess={handleSuccess} />
<TransferButton
availableAcls={availableAcls}
pieceOrEditions={pieceOrEditions}
currentUser={currentUser}
handleSuccess={handleSuccess}/>
<ConsignButton
availableAcls={availableAcls}
pieceOrEditions={pieceOrEditions}
currentUser={currentUser}
handleSuccess={handleSuccess} />
<UnconsignButton
availableAcls={availableAcls}
pieceOrEditions={pieceOrEditions}
currentUser={currentUser}
handleSuccess={handleSuccess} />
<LoanButton
availableAcls={availableAcls}
pieceOrEditions={pieceOrEditions}
currentUser={currentUser}
handleSuccess={handleSuccess} />
{this.renderChildren()}
2015-09-28 22:09:25 +02:00
</span>
2015-06-03 11:49:39 +02:00
</div>
);
2015-06-03 10:27:11 +02:00
}
});
export default AclButtonList;