2015-06-11 15:03:55 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
import Form from './form';
|
|
|
|
|
2015-10-30 11:10:31 +01:00
|
|
|
import LoanRequestButton from '../ascribe_buttons/acls/loan_request_button';
|
|
|
|
import UnconsignButton from '../ascribe_buttons/acls/unconsign_button';
|
|
|
|
|
|
|
|
import ActionPanel from '../ascribe_panel/action_panel';
|
|
|
|
|
2015-09-03 15:17:12 +02:00
|
|
|
import NotificationActions from '../../actions/notification_actions';
|
2015-09-01 19:14:48 +02:00
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
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-10-30 11:46:01 +01:00
|
|
|
import { getAclFormDataId } from '../../utils/form_utils';
|
2015-07-14 11:42:09 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils.js';
|
2015-06-16 09:59:09 +02:00
|
|
|
|
2015-06-11 15:03:55 +02:00
|
|
|
let RequestActionForm = React.createClass({
|
2015-08-10 11:57:38 +02:00
|
|
|
propTypes: {
|
2016-02-05 11:53:33 +01:00
|
|
|
notifications: React.PropTypes.object.isRequired,
|
2015-08-24 10:48:40 +02:00
|
|
|
pieceOrEditions: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.array
|
|
|
|
]).isRequired,
|
2016-02-05 11:53:33 +01:00
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
currentUser: React.PropTypes.object,
|
|
|
|
handleSuccess: React.PropTypes.func
|
|
|
|
},
|
2015-06-11 15:03:55 +02:00
|
|
|
|
2015-10-30 11:46:01 +01:00
|
|
|
isPiece() {
|
2015-08-24 10:48:40 +02:00
|
|
|
return this.props.pieceOrEditions.constructor !== Array;
|
|
|
|
},
|
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
getUrls() {
|
|
|
|
let urls = {};
|
|
|
|
|
2015-10-30 11:46:01 +01:00
|
|
|
if (this.props.notifications.action === 'consign') {
|
2015-08-10 11:57:38 +02:00
|
|
|
urls.accept = ApiUrls.ownership_consigns_confirm;
|
|
|
|
urls.deny = ApiUrls.ownership_consigns_deny;
|
2015-10-30 11:46:01 +01:00
|
|
|
} else if (this.props.notifications.action === 'unconsign') {
|
2015-08-10 11:57:38 +02:00
|
|
|
urls.accept = ApiUrls.ownership_unconsigns;
|
|
|
|
urls.deny = ApiUrls.ownership_unconsigns_deny;
|
2015-10-30 11:46:01 +01:00
|
|
|
} else if (this.props.notifications.action === 'loan' && !this.isPiece()) {
|
2015-08-10 11:57:38 +02:00
|
|
|
urls.accept = ApiUrls.ownership_loans_confirm;
|
|
|
|
urls.deny = ApiUrls.ownership_loans_deny;
|
2015-10-30 11:46:01 +01:00
|
|
|
} else if (this.props.notifications.action === 'loan' && this.isPiece()) {
|
2015-08-24 10:48:40 +02:00
|
|
|
urls.accept = ApiUrls.ownership_loans_pieces_confirm;
|
|
|
|
urls.deny = ApiUrls.ownership_loans_pieces_deny;
|
2015-10-30 11:46:01 +01:00
|
|
|
} else if (this.props.notifications.action === 'loan_request' && this.isPiece()) {
|
2015-08-27 11:38:21 +02:00
|
|
|
urls.accept = ApiUrls.ownership_loans_pieces_request_confirm;
|
|
|
|
urls.deny = ApiUrls.ownership_loans_pieces_request_deny;
|
2015-06-11 15:03:55 +02:00
|
|
|
}
|
2015-08-10 11:57:38 +02:00
|
|
|
|
|
|
|
return urls;
|
2015-06-11 15:03:55 +02:00
|
|
|
},
|
|
|
|
|
2015-10-30 11:46:01 +01:00
|
|
|
getFormData() {
|
|
|
|
return getAclFormDataId(this.isPiece(), this.props.pieceOrEditions);
|
2015-06-11 15:03:55 +02:00
|
|
|
},
|
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
showNotification(option, action, owner) {
|
|
|
|
return () => {
|
2015-11-02 15:21:33 +01:00
|
|
|
const message = getLangText('You have successfully %s the %s request from %s', getLangText(option), getLangText(action), owner);
|
2015-10-30 11:46:01 +01:00
|
|
|
const notifications = new GlobalNotificationModel(message, 'success');
|
2015-09-03 17:25:22 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notifications);
|
2015-08-10 11:57:38 +02:00
|
|
|
|
2015-09-01 19:14:48 +02:00
|
|
|
this.handleSuccess();
|
2015-08-10 11:57:38 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-09-01 19:14:48 +02:00
|
|
|
handleSuccess() {
|
2015-10-30 11:46:01 +01:00
|
|
|
if (this.isPiece()) {
|
2015-09-03 15:17:12 +02:00
|
|
|
NotificationActions.fetchPieceListNotifications();
|
2015-10-30 11:46:01 +01:00
|
|
|
} else {
|
2015-09-03 15:17:12 +02:00
|
|
|
NotificationActions.fetchEditionListNotifications();
|
|
|
|
}
|
2015-10-30 11:46:01 +01:00
|
|
|
|
|
|
|
if (typeof this.props.handleSuccess === 'function') {
|
2015-09-01 19:14:48 +02:00
|
|
|
this.props.handleSuccess();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-10 09:22:39 +02:00
|
|
|
getContent() {
|
|
|
|
return (
|
|
|
|
<span>
|
2015-09-03 17:25:22 +02:00
|
|
|
{this.props.notifications.action_str + ' by ' + this.props.notifications.by}
|
2015-08-10 09:22:39 +02:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-08-10 14:07:23 +02:00
|
|
|
getAcceptButtonForm(urls) {
|
2015-10-30 11:46:01 +01:00
|
|
|
if (this.props.notifications.action === 'unconsign') {
|
2015-08-10 11:57:38 +02:00
|
|
|
return (
|
2015-10-30 11:10:31 +01:00
|
|
|
<UnconsignButton
|
2015-07-14 17:42:15 +02:00
|
|
|
availableAcls={{'acl_unconsign': true}}
|
2015-08-27 11:38:21 +02:00
|
|
|
buttonAcceptClassName='inline pull-right btn-sm ascribe-margin-1px'
|
2015-08-26 09:50:38 +02:00
|
|
|
pieceOrEditions={this.props.pieceOrEditions}
|
|
|
|
currentUser={this.props.currentUser}
|
2015-09-01 19:14:48 +02:00
|
|
|
handleSuccess={this.handleSuccess} />
|
2015-08-26 09:50:38 +02:00
|
|
|
);
|
2015-10-30 11:46:01 +01:00
|
|
|
} else if (this.props.notifications.action === 'loan_request') {
|
2015-08-26 09:50:38 +02:00
|
|
|
return (
|
2015-10-30 11:10:31 +01:00
|
|
|
<LoanRequestButton
|
2015-08-26 09:50:38 +02:00
|
|
|
availableAcls={{'acl_loan_request': true}}
|
|
|
|
buttonAcceptName="LOAN"
|
2015-08-27 11:38:21 +02:00
|
|
|
buttonAcceptClassName='inline pull-right btn-sm ascribe-margin-1px'
|
2015-08-24 10:48:40 +02:00
|
|
|
pieceOrEditions={this.props.pieceOrEditions}
|
2015-07-14 17:42:15 +02:00
|
|
|
currentUser={this.props.currentUser}
|
2015-09-01 19:14:48 +02:00
|
|
|
handleSuccess={this.handleSuccess} />
|
2015-07-14 17:42:15 +02:00
|
|
|
);
|
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-24 10:48:40 +02:00
|
|
|
handleSuccess={
|
2015-11-02 15:21:33 +01:00
|
|
|
this.showNotification('accepted', this.props.notifications.action, this.props.notifications.by)
|
2015-08-24 10:48:40 +02:00
|
|
|
}
|
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-06-11 15:03:55 +02:00
|
|
|
);
|
|
|
|
}
|
2015-08-10 11:57:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getButtonForm() {
|
2015-10-30 11:46:01 +01:00
|
|
|
const urls = this.getUrls();
|
|
|
|
const 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-24 10:48:40 +02:00
|
|
|
handleSuccess={
|
2015-11-02 15:21:33 +01:00
|
|
|
this.showNotification('denied', this.props.notifications.action, this.props.notifications.by)
|
2015-08-24 10:48:40 +02:00
|
|
|
}
|
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'>
|
2015-10-30 11:46:01 +01:00
|
|
|
{getLangText('REJECT')}
|
2015-08-10 11:57:38 +02:00
|
|
|
</button>
|
|
|
|
</Form>
|
2015-08-10 14:06:01 +02:00
|
|
|
{acceptButtonForm}
|
2015-08-10 11:57:38 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2015-08-10 09:22:39 +02:00
|
|
|
|
2015-08-10 11:57:38 +02:00
|
|
|
render() {
|
2015-06-11 15:03:55 +02:00
|
|
|
return (
|
2015-08-10 09:22:39 +02:00
|
|
|
<ActionPanel
|
|
|
|
content={this.getContent()}
|
2015-10-30 11:46:01 +01:00
|
|
|
buttons={this.getButtonForm()} />
|
2015-06-11 15:03:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-10-30 11:10:31 +01:00
|
|
|
export default RequestActionForm;
|