1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-23 01:36:28 +02:00
onion/js/components/ascribe_settings/contract_settings.js

177 lines
7.7 KiB
JavaScript
Raw 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';
import UserStore from '../../stores/user_store';
import UserActions from '../../actions/user_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 { mergeOptions } from '../../utils/general_utils';
let ContractSettings = React.createClass({
2015-09-02 14:02:23 +02:00
getInitialState(){
return mergeOptions(
ContractListStore.getState(),
UserStore.getState()
);
2015-09-02 14:02:23 +02:00
},
2015-09-02 14:02:23 +02:00
componentDidMount() {
ContractListStore.listen(this.onChange);
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser();
ContractListActions.fetchContractList(true);
2015-09-02 14:02:23 +02:00
},
2015-09-02 14:02:23 +02:00
componentWillUnmount() {
UserStore.unlisten(this.onChange);
2015-09-02 14:02:23 +02:00
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);
2015-09-09 11:11:16 +02:00
let notification = new GlobalNotificationModel(response.notification, 'success', 4000);
GlobalNotificationActions.appendGlobalNotification(notification);
})
2015-09-08 11:44:05 +02:00
.catch((err) => {
let 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
},
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
},
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() {
2015-09-03 15:53:02 +02:00
let publicContracts = this.getPublicContracts();
let privateContracts = this.getPrivateContracts();
let createPublicContractForm = null;
if(publicContracts.length === 0) {
createPublicContractForm = (
<CreateContractForm
isPublic={true}
fileClassToUpload={{
2015-09-18 15:17:24 +02:00
singular: 'new contract',
plural: 'new contracts'
}}/>
);
}
2015-09-08 10:03:20 +02:00
return (
<div className="settings-container">
<AclProxy
aclName="acl_view_settings_contract"
aclObject={this.state.currentUser.acl}>
<CollapsibleParagraph
title={getLangText('Contracts')}
show={true}
defaultExpanded={true}>
<AclProxy
aclName="acl_edit_public_contract"
aclObject={this.state.currentUser.acl}>
<div>
{createPublicContractForm}
{publicContracts.map((contract, i) => {
return (
<ActionPanel
key={i}
title={contract.name}
content={contract.name}
buttons={
<div className="pull-right">
<ContractSettingsUpdateButton contract={contract}/>
<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"
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
</button>
</div>
}
leftColumnWidth="40%"
rightColumnWidth="60%"/>
);
})}
</div>
</AclProxy>
<AclProxy
aclName="acl_edit_private_contract"
aclObject={this.state.currentUser.acl}>
<div>
<CreateContractForm
isPublic={false}
fileClassToUpload={{
singular: getLangText('new contract'),
plural: getLangText('new contracts')
}}/>
{privateContracts.map((contract, i) => {
return (
<ActionPanel
key={i}
title={contract.name}
content={contract.name}
buttons={
<div className="pull-right">
<ContractSettingsUpdateButton contract={contract} />
<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"
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
</button>
</div>
}
leftColumnWidth="40%"
rightColumnWidth="60%"/>
);
})}
</div>
</AclProxy>
</CollapsibleParagraph>
</AclProxy>
</div>
);
}
});
export default ContractSettings;