2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-27 14:35:33 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-06-03 10:27:11 +02:00
|
|
|
import ConsignForm from '../ascribe_forms/form_consign';
|
2015-06-11 15:03:55 +02:00
|
|
|
import UnConsignForm from '../ascribe_forms/form_unconsign';
|
2015-06-03 10:27:11 +02:00
|
|
|
import TransferForm from '../ascribe_forms/form_transfer';
|
2015-07-15 17:51:09 +02:00
|
|
|
import LoanForm from '../ascribe_forms/form_loan';
|
2015-08-26 09:50:38 +02:00
|
|
|
import LoanRequestAnswerForm from '../ascribe_forms/form_loan_request_answer';
|
2015-06-03 10:27:11 +02:00
|
|
|
import ShareForm from '../ascribe_forms/form_share_email';
|
|
|
|
import ModalWrapper from '../ascribe_modal/modal_wrapper';
|
|
|
|
import AppConstants from '../../constants/application_constants';
|
2015-05-27 14:35:33 +02:00
|
|
|
|
2015-06-09 16:10:38 +02:00
|
|
|
import GlobalNotificationModel from '../../models/global_notification_model';
|
|
|
|
import GlobalNotificationActions from '../../actions/global_notification_actions';
|
|
|
|
|
2015-08-07 15:08:02 +02:00
|
|
|
import ApiUrls from '../../constants/api_urls';
|
2015-07-03 19:08:56 +02:00
|
|
|
|
2015-08-10 15:57:20 +02:00
|
|
|
import { getAclFormMessage } from '../../utils/form_utils';
|
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
|
|
|
|
2015-05-27 14:35:33 +02:00
|
|
|
let AclButton = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
action: React.PropTypes.oneOf(AppConstants.aclList).isRequired,
|
2015-07-13 21:19:45 +02:00
|
|
|
availableAcls: React.PropTypes.object.isRequired,
|
|
|
|
pieceOrEditions: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.array
|
|
|
|
]).isRequired,
|
2015-06-02 11:38:18 +02:00
|
|
|
currentUser: React.PropTypes.object,
|
2015-08-26 09:50:38 +02:00
|
|
|
buttonAcceptName: React.PropTypes.string,
|
|
|
|
buttonAcceptClassName: React.PropTypes.string,
|
2015-07-01 10:53:40 +02:00
|
|
|
handleSuccess: React.PropTypes.func.isRequired,
|
|
|
|
className: React.PropTypes.string
|
2015-05-27 14:35:33 +02:00
|
|
|
},
|
|
|
|
|
2015-10-23 14:19:44 +02:00
|
|
|
isPiece() {
|
2015-07-15 14:03:23 +02:00
|
|
|
return this.props.pieceOrEditions.constructor !== Array;
|
2015-07-13 14:10:46 +02:00
|
|
|
},
|
2015-07-13 21:19:45 +02:00
|
|
|
|
2015-10-23 14:19:44 +02:00
|
|
|
actionProperties() {
|
|
|
|
let message = getAclFormMessage({
|
|
|
|
aclName: this.props.action,
|
|
|
|
entities: this.props.pieceOrEditions,
|
|
|
|
isPiece: this.isPiece(),
|
|
|
|
senderName: this.props.currentUser.username
|
|
|
|
});
|
2015-08-10 15:57:20 +02:00
|
|
|
|
2015-10-23 14:19:44 +02:00
|
|
|
if (this.props.action === 'acl_consign') {
|
2015-06-01 18:59:47 +02:00
|
|
|
return {
|
2015-07-03 19:08:56 +02:00
|
|
|
title: getLangText('Consign artwork'),
|
|
|
|
tooltip: getLangText('Have someone else sell the artwork'),
|
2015-07-14 14:16:51 +02:00
|
|
|
form: (
|
|
|
|
<ConsignForm
|
2015-08-10 15:57:20 +02:00
|
|
|
message={message}
|
2015-07-14 14:16:51 +02:00
|
|
|
id={this.getFormDataId()}
|
2015-08-07 15:08:02 +02:00
|
|
|
url={ApiUrls.ownership_consigns}/>
|
2015-10-23 14:19:44 +02:00
|
|
|
),
|
2015-06-09 16:10:38 +02:00
|
|
|
handleSuccess: this.showNotification
|
2015-06-05 11:06:36 +02:00
|
|
|
};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else if (this.props.action === 'acl_unconsign') {
|
2015-06-11 15:03:55 +02:00
|
|
|
return {
|
2015-07-03 19:08:56 +02:00
|
|
|
title: getLangText('Unconsign artwork'),
|
|
|
|
tooltip: getLangText('Have the owner manage his sales again'),
|
2015-07-14 17:42:15 +02:00
|
|
|
form: (
|
|
|
|
<UnConsignForm
|
2015-08-10 15:57:20 +02:00
|
|
|
message={message}
|
2015-07-14 17:42:15 +02:00
|
|
|
id={this.getFormDataId()}
|
2015-08-07 15:08:02 +02:00
|
|
|
url={ApiUrls.ownership_unconsigns}/>
|
2015-10-23 14:19:44 +02:00
|
|
|
),
|
2015-06-11 15:03:55 +02:00
|
|
|
handleSuccess: this.showNotification
|
|
|
|
};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else if (this.props.action === 'acl_transfer') {
|
2015-06-01 18:59:47 +02:00
|
|
|
return {
|
2015-07-03 19:08:56 +02:00
|
|
|
title: getLangText('Transfer artwork'),
|
|
|
|
tooltip: getLangText('Transfer the ownership of the artwork'),
|
2015-07-14 11:42:09 +02:00
|
|
|
form: (
|
|
|
|
<TransferForm
|
2015-08-10 15:57:20 +02:00
|
|
|
message={message}
|
2015-07-14 11:42:09 +02:00
|
|
|
id={this.getFormDataId()}
|
2015-08-07 15:08:02 +02:00
|
|
|
url={ApiUrls.ownership_transfers}/>
|
2015-07-15 14:03:23 +02:00
|
|
|
),
|
2015-06-09 16:10:38 +02:00
|
|
|
handleSuccess: this.showNotification
|
2015-06-05 11:06:36 +02:00
|
|
|
};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else if (this.props.action === 'acl_loan') {
|
2015-06-01 18:59:47 +02:00
|
|
|
return {
|
2015-07-03 19:08:56 +02:00
|
|
|
title: getLangText('Loan artwork'),
|
|
|
|
tooltip: getLangText('Loan your artwork for a limited period of time'),
|
2015-10-23 14:19:44 +02:00
|
|
|
form: (
|
|
|
|
<LoanForm
|
2015-08-10 15:57:20 +02:00
|
|
|
message={message}
|
2015-07-15 14:03:23 +02:00
|
|
|
id={this.getFormDataId()}
|
2015-10-23 14:19:44 +02:00
|
|
|
url={this.isPiece() ? ApiUrls.ownership_loans_pieces
|
|
|
|
: ApiUrls.ownership_loans_editions}/>
|
2015-07-15 14:03:23 +02:00
|
|
|
),
|
2015-06-09 16:10:38 +02:00
|
|
|
handleSuccess: this.showNotification
|
2015-06-05 11:06:36 +02:00
|
|
|
};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else if (this.props.action === 'acl_loan_request') {
|
2015-08-26 09:50:38 +02:00
|
|
|
return {
|
|
|
|
title: getLangText('Loan artwork'),
|
2015-08-27 11:38:21 +02:00
|
|
|
tooltip: getLangText('Someone requested you to loan your artwork for a limited period of time'),
|
2015-10-23 14:19:44 +02:00
|
|
|
form: (
|
|
|
|
<LoanRequestAnswerForm
|
2015-08-26 09:50:38 +02:00
|
|
|
message={message}
|
|
|
|
id={this.getFormDataId()}
|
2015-08-27 11:38:21 +02:00
|
|
|
url={ApiUrls.ownership_loans_pieces_request_confirm}/>
|
2015-08-26 09:50:38 +02:00
|
|
|
),
|
|
|
|
handleSuccess: this.showNotification
|
|
|
|
};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else if (this.props.action === 'acl_share') {
|
2015-06-01 18:59:47 +02:00
|
|
|
return {
|
2015-07-03 19:08:56 +02:00
|
|
|
title: getLangText('Share artwork'),
|
|
|
|
tooltip: getLangText('Share the artwork'),
|
2015-07-13 14:10:46 +02:00
|
|
|
form: (
|
|
|
|
<ShareForm
|
2015-08-10 15:57:20 +02:00
|
|
|
message={message}
|
2015-07-13 14:10:46 +02:00
|
|
|
id={this.getFormDataId()}
|
2015-10-23 14:19:44 +02:00
|
|
|
url={this.isPiece() ? ApiUrls.ownership_shares_pieces
|
|
|
|
: ApiUrls.ownership_shares_editions}/>
|
|
|
|
),
|
2015-06-09 16:10:38 +02:00
|
|
|
handleSuccess: this.showNotification
|
2015-06-05 11:06:36 +02:00
|
|
|
};
|
2015-07-13 21:19:45 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Your specified action did not match a form.');
|
2015-06-01 18:59:47 +02:00
|
|
|
}
|
|
|
|
},
|
2015-07-13 14:10:46 +02:00
|
|
|
|
2015-10-23 14:19:44 +02:00
|
|
|
showNotification(response) {
|
2015-06-09 16:10:38 +02:00
|
|
|
this.props.handleSuccess();
|
2015-10-23 14:19:44 +02:00
|
|
|
if (response.notification) {
|
2015-07-16 19:12:18 +02:00
|
|
|
let notification = new GlobalNotificationModel(response.notification, 'success');
|
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
}
|
2015-06-09 16:10:38 +02:00
|
|
|
},
|
2015-07-13 14:10:46 +02:00
|
|
|
|
|
|
|
getFormDataId(){
|
|
|
|
if (this.isPiece()) {
|
|
|
|
return {piece_id: this.props.pieceOrEditions.id};
|
2015-10-23 14:19:44 +02:00
|
|
|
} else {
|
2015-07-13 14:10:46 +02:00
|
|
|
return {bitcoin_id: this.props.pieceOrEditions.map(function(edition){
|
|
|
|
return edition.bitcoin_id;
|
|
|
|
}).join()};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-13 21:19:45 +02:00
|
|
|
// Removes the acl_ prefix and converts to upper case
|
|
|
|
sanitizeAction() {
|
2015-08-26 09:50:38 +02:00
|
|
|
if (this.props.buttonAcceptName) {
|
|
|
|
return this.props.buttonAcceptName;
|
|
|
|
}
|
2015-07-13 21:19:45 +02:00
|
|
|
return this.props.action.split('acl_')[1].toUpperCase();
|
|
|
|
},
|
|
|
|
|
2015-05-27 14:35:33 +02:00
|
|
|
render() {
|
2015-10-23 14:19:44 +02:00
|
|
|
if (this.props.availableAcls) {
|
2015-09-16 14:07:13 +02:00
|
|
|
let shouldDisplay = this.props.availableAcls[this.props.action];
|
|
|
|
let aclProps = this.actionProperties();
|
|
|
|
let buttonClassName = this.props.buttonAcceptClassName ? this.props.buttonAcceptClassName : '';
|
|
|
|
return (
|
|
|
|
<ModalWrapper
|
|
|
|
trigger={
|
|
|
|
<button className={shouldDisplay ? 'btn btn-default btn-sm ' + buttonClassName : 'hidden'}>
|
|
|
|
{this.sanitizeAction()}
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
handleSuccess={aclProps.handleSuccess}
|
|
|
|
title={aclProps.title}>
|
|
|
|
{aclProps.form}
|
|
|
|
</ModalWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2015-05-27 14:35:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-23 14:19:44 +02:00
|
|
|
export default AclButton;
|