1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00
onion/js/components/ascribe_settings/contract_settings.js

160 lines
6.4 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';
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';
import { getLangText } from '../../utils/lang_utils';
let ContractSettings = React.createClass({
propTypes: {
defaultExpanded: React.PropTypes.bool
},
2015-09-02 14:02:23 +02:00
getInitialState(){
return ContractListStore.getState();
},
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);
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={{
singular: 'new public contract',
plural: 'new public contracts'
}}/>
);
}
2015-09-08 10:03:20 +02:00
return (
<CollapsibleParagraph
2015-09-14 17:38:26 +02:00
title={getLangText('Contracts')}
show={true}
defaultExpanded={true}>
2015-09-02 14:02:23 +02:00
<CollapsibleParagraph
2015-09-14 17:38:26 +02:00
title={getLangText('Public Contracts')}
2015-09-02 14:02:23 +02:00
show={true}
2015-09-08 10:03:20 +02:00
defaultExpanded={true}>
2015-09-14 17:38:26 +02:00
{createPublicContractForm}
{publicContracts.map((contract, i) => {
return (
<ActionPanel
key={i}
title={contract.name}
content={contract.name}
buttons={
<div className="pull-right">
2015-09-16 09:47:22 +02:00
<ContractSettingsUpdateButton contract={contract}/>
2015-09-14 17:38:26 +02:00
<a
className="btn btn-default btn-sm margin-left-2px"
href={contract.blob.url_safe}
target="_blank">
{getLangText('PREVIEW')}
2015-09-14 17:38:26 +02:00
</a>
<button
className="btn btn-default btn-sm margin-left-2px"
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
2015-09-14 17:38:26 +02:00
</button>
</div>
}
leftColumnWidth="40%"
rightColumnWidth="60%"/>
);
})}
</CollapsibleParagraph>
<CollapsibleParagraph
title={getLangText('Private Contracts')}
show={true}
defaultExpanded={true}>
<CreateContractForm
isPublic={false}
fileClassToUpload={{
singular: getLangText('new private contract'),
plural: getLangText('new private contracts')
2015-09-14 17:38:26 +02:00
}}/>
{privateContracts.map((contract, i) => {
return (
<ActionPanel
key={i}
title={contract.name}
content={contract.name}
buttons={
<div className="pull-right">
2015-09-16 09:47:22 +02:00
<ContractSettingsUpdateButton contract={contract} />
2015-09-14 17:38:26 +02:00
<a
className="btn btn-default btn-sm margin-left-2px"
href={contract.blob.url_safe}
target="_blank">
{getLangText('PREVIEW')}
2015-09-14 17:38:26 +02:00
</a>
<button
className="btn btn-default btn-sm margin-left-2px"
onClick={this.removeContract(contract)}>
{getLangText('REMOVE')}
2015-09-14 17:38:26 +02:00
</button>
</div>
}
leftColumnWidth="40%"
rightColumnWidth="60%"/>
);
})}
2015-09-02 14:02:23 +02:00
</CollapsibleParagraph>
</CollapsibleParagraph>
);
}
});
export default ContractSettings;