1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/components/ascribe_settings/contract_settings.js

184 lines
8.1 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
import React from 'react';
import CollapsibleParagraph from '../ascribe_collapsible/collapsible_paragraph';
import CreateContractForm from '../ascribe_forms/form_create_contract';
2015-09-02 14:02:23 +02:00
import ContractListStore from '../../stores/contract_list_store';
import ContractListActions from '../../actions/contract_list_actions';
2015-09-03 15:53:02 +02:00
import ActionPanel from '../ascribe_panel/action_panel';
import ContractSettingsUpdateButton from './contract_settings_update_button';
2015-09-03 15:53:02 +02:00
import GlobalNotificationModel from '../../models/global_notification_model';
import GlobalNotificationActions from '../../actions/global_notification_actions';
2015-09-18 09:46:37 +02:00
import AclProxy from '../acl_proxy';
import { getLangText } from '../../utils/lang_utils';
import { setDocumentTitle } from '../../utils/dom_utils';
2016-02-05 10:38:59 +01:00
import { truncateTextAtCharIndex } from '../../utils/general_utils';
let ContractSettings = React.createClass({
propTypes: {
// Provided from AscribeApp
currentUser: React.PropTypes.object.isRequired,
whitelabel: React.PropTypes.object.isRequired,
2016-01-11 16:26:36 +01:00
// Provided from router
location: React.PropTypes.object
},
getInitialState() {
return ContractListStore.getState();
2015-09-02 14:02:23 +02:00
},
2015-09-02 14:02:23 +02:00
componentDidMount() {
ContractListStore.listen(this.onChange);
ContractListActions.fetchContractList(true);
2015-09-02 14:02:23 +02:00
},
2015-09-02 14:02:23 +02:00
componentWillUnmount() {
ContractListStore.unlisten(this.onChange);
},
2015-09-02 14:02:23 +02:00
onChange(state) {
this.setState(state);
},
removeContract(contract) {
return () => {
ContractListActions.removeContract(contract.id)
2015-09-09 11:11:16 +02:00
.then((response) => {
ContractListActions.fetchContractList(true);
2016-02-05 10:38:59 +01:00
const notification = new GlobalNotificationModel(response.notification, 'success', 4000);
2015-09-09 11:11:16 +02:00
GlobalNotificationActions.appendGlobalNotification(notification);
})
2015-09-08 11:44:05 +02:00
.catch((err) => {
2016-02-05 10:38:59 +01:00
const notification = new GlobalNotificationModel(err, 'danger', 10000);
GlobalNotificationActions.appendGlobalNotification(notification);
2015-09-09 11:11:16 +02:00
});
};
2015-09-03 15:53:02 +02:00
},
getPublicContracts() {
return this.state.contractList.filter((contract) => contract.is_public);
2015-09-03 15:53:02 +02:00
},
getPrivateContracts() {
return this.state.contractList.filter((contract) => !contract.is_public);
2015-09-03 15:53:02 +02:00
},
render() {
const { currentUser, location, whitelabel } = this.props;
const publicContracts = this.getPublicContracts();
const privateContracts = this.getPrivateContracts();
let createPublicContractForm = null;
setDocumentTitle(getLangText('Contracts settings'));
if (publicContracts.length === 0) {
createPublicContractForm = (
<CreateContractForm
fileClassToUpload={{
2015-09-18 15:17:24 +02:00
singular: 'new contract',
plural: 'new contracts'
}}
2016-02-05 10:38:59 +01:00
isPublic={true} />
);
}
2015-09-08 10:03:20 +02:00
return (
<div className="settings-container">
2015-09-14 17:38:26 +02:00
<CollapsibleParagraph
2015-09-18 09:46:37 +02:00
title={getLangText('Contracts')}
2015-09-23 15:41:12 +02:00
defaultExpanded={true}>
2015-09-18 09:46:37 +02:00
<AclProxy
aclName="acl_edit_public_contract"
aclObject={currentUser.acl}>
2015-09-18 15:17:24 +02:00
<div>
2015-09-18 09:46:37 +02:00
{createPublicContractForm}
{publicContracts.map((contract, i) => {
return (
<ActionPanel
2016-02-05 10:38:59 +01:00
key={contract.id}
2015-09-18 09:46:37 +02:00
title={contract.name}
content={truncateTextAtCharIndex(contract.name, 120, '(...).pdf')}
2015-09-18 09:46:37 +02:00
buttons={
<div className="pull-right">
<AclProxy
aclObject={whitelabel}
aclName="acl_update_public_contract">
2016-02-05 10:38:59 +01:00
<ContractSettingsUpdateButton contract={contract} />
</AclProxy>
2015-09-18 09:46:37 +02:00
<a
className="btn btn-default btn-sm margin-left-2px"
href={contract.blob.url_safe}
target="_blank">
{getLangText('PREVIEW')}
</a>
<button
className="btn btn-danger btn-sm margin-left-2px"
2015-09-18 09:46:37 +02:00
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
</button>
</div>
}
leftColumnWidth="40%"
rightColumnWidth="60%"/>
);
})}
2015-09-18 15:17:24 +02:00
</div>
2015-09-18 09:46:37 +02:00
</AclProxy>
<AclProxy
aclName="acl_edit_private_contract"
aclObject={currentUser.acl}>
2015-09-18 15:17:24 +02:00
<div>
2015-09-18 09:46:37 +02:00
<CreateContractForm
2016-02-05 10:38:59 +01:00
fileClassToUpload={{
singular: getLangText('new contract'),
plural: getLangText('new contracts')
}}
isPublic={false} />
2015-09-18 09:46:37 +02:00
{privateContracts.map((contract, i) => {
return (
<ActionPanel
2016-02-05 10:38:59 +01:00
key={contract.id}
2015-09-18 09:46:37 +02:00
title={contract.name}
content={truncateTextAtCharIndex(contract.name, 120, '(...).pdf')}
2015-09-18 09:46:37 +02:00
buttons={
<div className="pull-right">
<AclProxy
aclObject={whitelabel}
aclName="acl_update_private_contract">
2016-02-05 10:38:59 +01:00
<ContractSettingsUpdateButton contract={contract} />
</AclProxy>
2015-09-18 09:46:37 +02:00
<a
className="btn btn-default btn-sm margin-left-2px"
href={contract.blob.url_safe}
target="_blank">
{getLangText('PREVIEW')}
</a>
<button
className="btn btn-danger btn-sm margin-left-2px"
2015-09-18 09:46:37 +02:00
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
</button>
</div>
}
leftColumnWidth="60%"
rightColumnWidth="40%"/>
2015-09-18 09:46:37 +02:00
);
})}
2015-09-18 15:17:24 +02:00
</div>
2015-09-18 09:46:37 +02:00
</AclProxy>
2015-09-02 14:02:23 +02:00
</CollapsibleParagraph>
</div>
);
}
});
export default ContractSettings;