1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 09:50:31 +01:00
onion/js/components/ascribe_forms/form_request_action.js

144 lines
4.4 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
2015-07-14 17:42:15 +02:00
import AclButton from './../ascribe_buttons/acl_button';
import ActionPanel from '../ascribe_panel/action_panel';
2015-08-10 11:57:38 +02:00
import Form from './form';
import GlobalNotificationModel from '../../models/global_notification_model';
import GlobalNotificationActions from '../../actions/global_notification_actions';
import ApiUrls from '../../constants/api_urls';
2015-07-14 17:42:15 +02:00
2015-07-14 11:42:09 +02:00
import { getLangText } from '../../utils/lang_utils.js';
2015-06-16 09:59:09 +02:00
2015-08-10 11:57:38 +02:00
let RequestActionForm = React.createClass({
2015-08-10 11:57:38 +02:00
propTypes: {
editions: React.PropTypes.arrayOf(React.PropTypes.object),
currentUser: React.PropTypes.object,
handleSuccess: React.PropTypes.func
},
2015-08-10 11:57:38 +02:00
getUrls() {
let edition = this.props.editions[0];
2015-08-10 11:57:38 +02:00
let urls = {};
if (edition.request_action === 'consign'){
urls.accept = ApiUrls.ownership_consigns_confirm;
urls.deny = ApiUrls.ownership_consigns_deny;
} else if (edition.request_action === 'unconsign'){
urls.accept = ApiUrls.ownership_unconsigns;
urls.deny = ApiUrls.ownership_unconsigns_deny;
} else if (edition.request_action === 'loan'){
urls.accept = ApiUrls.ownership_loans_confirm;
urls.deny = ApiUrls.ownership_loans_deny;
}
2015-08-10 11:57:38 +02:00
return urls;
},
2015-08-10 11:57:38 +02:00
getBitcoinIds(){
return this.props.editions.map(function(edition){
return edition.bitcoin_id;
});
},
getFormData() {
return {
bitcoin_id: this.getBitcoinIds().join()
};
},
2015-08-10 11:57:38 +02:00
showNotification(option, action, owner) {
return () => {
2015-08-10 14:15:32 +02:00
let message = getLangText('You have successfully') + ' ' + option + ' the ' + action + ' request ' + getLangText('from') + ' ' + owner;
2015-08-10 11:57:38 +02:00
let notification = new GlobalNotificationModel(message, 'success');
GlobalNotificationActions.appendGlobalNotification(notification);
if(this.props.handleSuccess) {
this.props.handleSuccess();
}
};
},
getContent() {
let edition = this.props.editions[0];
let message = edition.owner + ' ' + getLangText('requests you') + ' ' + edition.request_action + ' ' + getLangText('this edition%s', '.');
return (
<span>
{message}
</span>
);
},
2015-08-10 14:07:23 +02:00
getAcceptButtonForm(urls) {
let edition = this.props.editions[0];
2015-08-10 11:57:38 +02:00
if(edition.request_action === 'unconsign') {
return (
2015-07-14 17:42:15 +02:00
<AclButton
availableAcls={{'acl_unconsign': true}}
action="acl_unconsign"
pieceOrEditions={this.props.editions}
currentUser={this.props.currentUser}
handleSuccess={this.props.handleSuccess} />
);
2015-08-10 11:57:38 +02:00
} else {
return (
2015-08-10 14:06:01 +02:00
<Form
url={urls.accept}
getFormData={this.getFormData}
2015-08-10 14:20:26 +02:00
handleSuccess={this.showNotification(getLangText('accepted'), edition.request_action, edition.owner)}
2015-08-10 14:15:32 +02:00
isInline={true}
2015-08-10 14:06:01 +02:00
className='inline pull-right'>
<button
type="submit"
className='btn btn-default btn-sm ascribe-margin-1px'>
{getLangText('ACCEPT')}
</button>
</Form>
);
}
2015-08-10 11:57:38 +02:00
},
getButtonForm() {
let edition = this.props.editions[0];
2015-08-10 11:57:38 +02:00
let urls = this.getUrls();
2015-08-10 14:07:23 +02:00
let acceptButtonForm = this.getAcceptButtonForm(urls);
2015-08-10 11:57:38 +02:00
return (
<div>
<Form
url={urls.deny}
2015-08-10 14:15:32 +02:00
isInline={true}
2015-08-10 11:57:38 +02:00
getFormData={this.getFormData}
2015-08-10 14:20:26 +02:00
handleSuccess={this.showNotification(getLangText('denied'), edition.request_action, edition.owner)}
2015-08-10 11:57:38 +02:00
className='inline pull-right'>
<button
type="submit"
className='btn btn-danger btn-delete btn-sm ascribe-margin-1px'>
{getLangText('REJECT')}
</button>
</Form>
2015-08-10 14:06:01 +02:00
{acceptButtonForm}
2015-08-10 11:57:38 +02:00
</div>
);
},
2015-08-10 11:57:38 +02:00
render() {
return (
<ActionPanel
content={this.getContent()}
2015-08-10 11:57:38 +02:00
buttons={this.getButtonForm()}/>
);
}
});
export default RequestActionForm;