1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-25 18:56:28 +02:00

Bring over changes for acl_button from Lumenus

This commit is contained in:
Brett Sun 2015-10-29 17:04:31 +01:00
parent 590818cf0b
commit c242cffdbd
2 changed files with 85 additions and 58 deletions

View File

@ -16,7 +16,7 @@ import GlobalNotificationActions from '../../actions/global_notification_actions
import ApiUrls from '../../constants/api_urls';
import { getAclFormMessage } from '../../utils/form_utils';
import { getAclFormMessage, getAclFormDataId } from '../../utils/form_utils';
import { getLangText } from '../../utils/lang_utils';
let AclButton = React.createClass({
@ -30,32 +30,37 @@ let AclButton = React.createClass({
currentUser: React.PropTypes.object,
buttonAcceptName: React.PropTypes.string,
buttonAcceptClassName: React.PropTypes.string,
email: React.PropTypes.string,
handleSuccess: React.PropTypes.func.isRequired,
className: React.PropTypes.string
},
isPiece(){
isPiece() {
return this.props.pieceOrEditions.constructor !== Array;
},
actionProperties(){
actionProperties() {
let message = getAclFormMessage({
aclName: this.props.action,
entities: this.props.pieceOrEditions,
isPiece: this.isPiece(),
senderName: this.props.currentUser.username
});
let message = getAclFormMessage(this.props.action, this.getTitlesString(), this.props.currentUser.username);
if (this.props.action === 'acl_consign'){
if (this.props.action === 'acl_consign') {
return {
title: getLangText('Consign artwork'),
tooltip: getLangText('Have someone else sell the artwork'),
form: (
<ConsignForm
email={this.props.email}
message={message}
id={this.getFormDataId()}
url={ApiUrls.ownership_consigns}/>
),
),
handleSuccess: this.showNotification
};
}
if (this.props.action === 'acl_unconsign'){
} else if (this.props.action === 'acl_unconsign') {
return {
title: getLangText('Unconsign artwork'),
tooltip: getLangText('Have the owner manage his sales again'),
@ -64,10 +69,10 @@ let AclButton = React.createClass({
message={message}
id={this.getFormDataId()}
url={ApiUrls.ownership_unconsigns}/>
),
),
handleSuccess: this.showNotification
};
}else if (this.props.action === 'acl_transfer') {
} else if (this.props.action === 'acl_transfer') {
return {
title: getLangText('Transfer artwork'),
tooltip: getLangText('Transfer the ownership of the artwork'),
@ -79,32 +84,33 @@ let AclButton = React.createClass({
),
handleSuccess: this.showNotification
};
}
else if (this.props.action === 'acl_loan'){
} else if (this.props.action === 'acl_loan') {
return {
title: getLangText('Loan artwork'),
tooltip: getLangText('Loan your artwork for a limited period of time'),
form: (<LoanForm
form: (
<LoanForm
email={this.props.email}
message={message}
id={this.getFormDataId()}
url={this.isPiece() ? ApiUrls.ownership_loans_pieces : ApiUrls.ownership_loans_editions}/>
url={this.isPiece() ? ApiUrls.ownership_loans_pieces
: ApiUrls.ownership_loans_editions}/>
),
handleSuccess: this.showNotification
};
}
else if (this.props.action === 'acl_loan_request'){
} else if (this.props.action === 'acl_loan_request') {
return {
title: getLangText('Loan artwork'),
tooltip: getLangText('Someone requested you to loan your artwork for a limited period of time'),
form: (<LoanRequestAnswerForm
form: (
<LoanRequestAnswerForm
message={message}
id={this.getFormDataId()}
url={ApiUrls.ownership_loans_pieces_request_confirm}/>
),
handleSuccess: this.showNotification
};
}
else if (this.props.action === 'acl_share'){
} else if (this.props.action === 'acl_share') {
return {
title: getLangText('Share artwork'),
tooltip: getLangText('Share the artwork'),
@ -112,8 +118,9 @@ let AclButton = React.createClass({
<ShareForm
message={message}
id={this.getFormDataId()}
url={this.isPiece() ? ApiUrls.ownership_shares_pieces : ApiUrls.ownership_shares_editions }/>
),
url={this.isPiece() ? ApiUrls.ownership_shares_pieces
: ApiUrls.ownership_shares_editions}/>
),
handleSuccess: this.showNotification
};
} else {
@ -121,36 +128,16 @@ let AclButton = React.createClass({
}
},
showNotification(response){
showNotification(response) {
this.props.handleSuccess();
if(response.notification) {
if (response.notification) {
let notification = new GlobalNotificationModel(response.notification, 'success');
GlobalNotificationActions.appendGlobalNotification(notification);
}
},
// plz move to share form
getTitlesString(){
if (this.isPiece()){
return '\"' + this.props.pieceOrEditions.title + '\"';
}
else {
return this.props.pieceOrEditions.map(function(edition) {
return '- \"' + edition.title + ', ' + getLangText('edition') + ' ' + edition.edition_number + '\"\n';
}).join('');
}
},
getFormDataId(){
if (this.isPiece()) {
return {piece_id: this.props.pieceOrEditions.id};
}
else {
return {bitcoin_id: this.props.pieceOrEditions.map(function(edition){
return edition.bitcoin_id;
}).join()};
}
return getAclFormDataId(this.isPiece(), this.props.pieceOrEditions);
},
// Removes the acl_ prefix and converts to upper case
@ -162,7 +149,7 @@ let AclButton = React.createClass({
},
render() {
if (this.props.availableAcls){
if (this.props.availableAcls) {
let shouldDisplay = this.props.availableAcls[this.props.action];
let aclProps = this.actionProperties();
let buttonClassName = this.props.buttonAcceptClassName ? this.props.buttonAcceptClassName : '';
@ -184,4 +171,4 @@ let AclButton = React.createClass({
}
});
export default AclButton;
export default AclButton;

View File

@ -2,14 +2,42 @@
import { getLangText } from './lang_utils';
import AppConstants from '../constants/application_constants';
/**
* Get the data ids of the given piece or editions.
* @param {boolean} isPiece Is the given entities parameter a piece? (False: array of editions)
* @param {(object|object[])} pieceOrEditions Piece or array of editions
* @return {(object|object[])} Data IDs of the pieceOrEditions for the form
*/
export function getAclFormDataId(isPiece, pieceOrEditions) {
if (isPiece) {
return {piece_id: pieceOrEditions.id};
} else {
return {bitcoin_id: pieceOrEditions.map(function(edition){
return edition.bitcoin_id;
}).join()};
}
}
/**
* Generates a message for submitting a form
* @param {string} aclName Enum name of a acl
* @param {string} entities Already computed name of entities
* @param {string} senderName Name of the sender
* @return {string} Completed message
* @param {object} options Options object for creating the message:
* @param {string} options.aclName Enum name of an acl
* @param {(object|object[])} options.entities Piece or array of Editions
* @param {boolean} options.isPiece Is the given entities parameter a piece? (False: array of editions)
* @param {string} [options.senderName] Name of the sender
* @return {string} Completed message
*/
export function getAclFormMessage(aclName, entities, senderName) {
export function getAclFormMessage(options) {
if (!options || options.aclName === undefined || options.isPiece === undefined ||
!(typeof options.entities === 'object' || options.entities.constructor === Array)) {
throw new Error('You must specify an acl class, entities in the correct format, and entity type');
}
let aclName = options.aclName;
let entityTitles = options.isPiece ? getTitlesStringOfPiece(options.entities)
: getTitlesStringOfEditions(options.entities);
let message = '';
message += getLangText('Hi');
@ -32,7 +60,7 @@ export function getAclFormMessage(aclName, entities, senderName) {
}
message += ':\n';
message += entities;
message += entityTitles;
if(aclName === 'acl_transfer' || aclName === 'acl_loan' || aclName === 'acl_consign') {
message += getLangText('to you');
@ -44,10 +72,22 @@ export function getAclFormMessage(aclName, entities, senderName) {
throw new Error('Your specified aclName did not match a an acl class.');
}
message += '\n\n';
message += getLangText('Truly yours,');
message += '\n';
message += senderName;
if (options.senderName) {
message += '\n\n';
message += getLangText('Truly yours,');
message += '\n';
message += options.senderName;
}
return message;
}
}
function getTitlesStringOfPiece(piece){
return '\"' + piece.title + '\"';
}
function getTitlesStringOfEditions(editions) {
return editions.map(function(edition) {
return '- \"' + edition.title + ', ' + getLangText('edition') + ' ' + edition.edition_number + '\"\n';
}).join('');
}