2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-11-11 15:00:04 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import moment from 'moment';
|
2017-07-17 18:37:33 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-01-27 14:42:06 +01:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-11-11 15:00:04 +01:00
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
import { Button, List, Portal, Tabs } from '@parity/ui';
|
2017-05-12 12:06:16 +02:00
|
|
|
import Editor from '@parity/ui/Editor';
|
|
|
|
import { CancelIcon, CheckIcon, DeleteIcon } from '@parity/ui/Icons';
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
import styles from './loadContract.css';
|
|
|
|
|
2017-01-27 14:42:06 +01:00
|
|
|
const REMOVAL_STYLE = {
|
|
|
|
backgroundColor: 'none',
|
|
|
|
cursor: 'default'
|
|
|
|
};
|
2017-05-12 17:06:53 +02:00
|
|
|
|
|
|
|
// const SELECTED_STYLE = {
|
|
|
|
// backgroundColor: 'rgba(255, 255, 255, 0.1)'
|
|
|
|
// };
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
export default class LoadContract extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
onLoad: PropTypes.func.isRequired,
|
|
|
|
onDelete: PropTypes.func.isRequired,
|
|
|
|
contracts: PropTypes.object.isRequired,
|
|
|
|
snippets: PropTypes.object.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
2017-05-12 17:06:53 +02:00
|
|
|
activeTab: -1,
|
2016-11-11 15:00:04 +01:00
|
|
|
selected: -1,
|
|
|
|
deleteRequest: false,
|
|
|
|
deleteId: -1
|
|
|
|
};
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { deleteRequest } = this.state;
|
|
|
|
|
|
|
|
return (
|
2017-02-22 15:26:58 +01:00
|
|
|
<Portal
|
|
|
|
buttons={ this.renderDialogActions() }
|
|
|
|
onClose={ this.onClose }
|
|
|
|
open
|
2017-01-27 14:42:06 +01:00
|
|
|
title={
|
|
|
|
deleteRequest
|
|
|
|
? (
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.title.remove'
|
|
|
|
defaultMessage='confirm removal'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.title.view'
|
|
|
|
defaultMessage='view contracts'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
>
|
|
|
|
{ this.renderBody() }
|
2017-02-22 15:26:58 +01:00
|
|
|
</Portal>
|
2016-11-11 15:00:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBody () {
|
2017-05-12 17:06:53 +02:00
|
|
|
const { activeTab, deleteRequest } = this.state;
|
|
|
|
|
|
|
|
if (deleteRequest) {
|
2016-11-11 15:00:04 +01:00
|
|
|
return this.renderConfirmRemoval();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { contracts, snippets } = this.props;
|
|
|
|
const contractsTab = Object.keys(contracts).length === 0
|
|
|
|
? null
|
|
|
|
: (
|
2017-05-12 17:06:53 +02:00
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.tab.local'
|
|
|
|
defaultMessage='Local'
|
|
|
|
/>
|
2016-11-11 15:00:04 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ styles.loadContainer }>
|
2017-05-12 17:06:53 +02:00
|
|
|
<Tabs
|
|
|
|
activeTab={ activeTab }
|
|
|
|
onChange={ this.handleChangeTab }
|
|
|
|
tabs={ [
|
|
|
|
contractsTab,
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.tab.snippets'
|
|
|
|
defaultMessage='Snippets'
|
|
|
|
/>
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
{ this.renderEditor() }
|
|
|
|
{
|
|
|
|
this.state.activeTab === 0
|
|
|
|
? (
|
|
|
|
<List
|
|
|
|
label={
|
|
|
|
<h4>
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.header.saved'
|
|
|
|
defaultMessage='Saved Contracts'
|
|
|
|
/>
|
|
|
|
</h4>
|
|
|
|
}
|
|
|
|
onChange={ this.onClickContract }
|
|
|
|
items={ this.renderContracts(contracts) }
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<List
|
|
|
|
label={
|
|
|
|
<h4>
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.header.snippets'
|
|
|
|
defaultMessage='Contract Snippets'
|
|
|
|
/>
|
|
|
|
</h4>
|
|
|
|
}
|
|
|
|
onClick={ this.onClickContract }
|
|
|
|
items={ this.renderContracts(snippets, false) }
|
2017-01-27 14:42:06 +01:00
|
|
|
/>
|
2017-05-12 17:06:53 +02:00
|
|
|
)
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderConfirmRemoval () {
|
|
|
|
const { deleteId } = this.state;
|
|
|
|
const { name, timestamp, sourcecode } = this.props.contracts[deleteId];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ styles.confirmRemoval }>
|
|
|
|
<p>
|
2017-01-27 14:42:06 +01:00
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.removal.confirm'
|
|
|
|
defaultMessage='Are you sure you want to remove the following contract from your saved contracts?'
|
|
|
|
/>
|
2016-11-11 15:00:04 +01:00
|
|
|
</p>
|
2017-05-12 17:06:53 +02:00
|
|
|
<List.Item
|
|
|
|
label={ name }
|
|
|
|
description={
|
2017-01-27 14:42:06 +01:00
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.removal.savedAt'
|
|
|
|
defaultMessage='Saved {when}'
|
|
|
|
values={ {
|
|
|
|
when: moment(timestamp).fromNow()
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
style={ REMOVAL_STYLE }
|
2016-11-11 15:00:04 +01:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div className={ styles.editor }>
|
|
|
|
<Editor
|
|
|
|
value={ sourcecode }
|
|
|
|
maxLines={ 20 }
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderEditor () {
|
|
|
|
const { contracts, snippets } = this.props;
|
|
|
|
const { selected } = this.state;
|
|
|
|
|
|
|
|
const mergedContracts = Object.assign({}, contracts, snippets);
|
|
|
|
|
|
|
|
if (selected === -1 || !mergedContracts[selected]) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { sourcecode, name } = mergedContracts[selected];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ styles.editor }>
|
|
|
|
<p>{ name }</p>
|
|
|
|
<Editor
|
|
|
|
readOnly
|
2017-01-27 14:42:06 +01:00
|
|
|
maxLines={ 20 }
|
|
|
|
value={ sourcecode }
|
2016-11-11 15:00:04 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContracts (contracts, removable = true) {
|
|
|
|
return Object
|
|
|
|
.values(contracts)
|
|
|
|
.map((contract) => {
|
|
|
|
const { id, name, timestamp, description } = contract;
|
|
|
|
const onDelete = () => this.onDeleteRequest(id);
|
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
return {
|
|
|
|
key: id,
|
|
|
|
label: name,
|
|
|
|
buttons: removable && (
|
|
|
|
<Button
|
|
|
|
icon={ <DeleteIcon /> }
|
|
|
|
onClick={ onDelete }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
description: description || (
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.contract.savedAt'
|
|
|
|
defaultMessage='Saved {when}'
|
|
|
|
values={ {
|
|
|
|
when: moment(timestamp).fromNow()
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
};
|
2016-11-11 15:00:04 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDialogActions () {
|
|
|
|
const { deleteRequest } = this.state;
|
|
|
|
|
|
|
|
if (deleteRequest) {
|
|
|
|
return [
|
|
|
|
<Button
|
2017-01-27 14:42:06 +01:00
|
|
|
icon={ <CancelIcon /> }
|
2016-11-11 15:00:04 +01:00
|
|
|
key='No'
|
2017-01-27 14:42:06 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.button.no'
|
|
|
|
defaultMessage='No'
|
|
|
|
/>
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
onClick={ this.onRejectRemoval }
|
|
|
|
/>,
|
|
|
|
<Button
|
|
|
|
icon={ <DeleteIcon /> }
|
|
|
|
key='Yes'
|
2017-01-27 14:42:06 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.button.yes'
|
|
|
|
defaultMessage='Yes'
|
|
|
|
/>
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
onClick={ this.onConfirmRemoval }
|
|
|
|
/>
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-27 14:42:06 +01:00
|
|
|
return [
|
2016-11-11 15:00:04 +01:00
|
|
|
<Button
|
2017-01-27 14:42:06 +01:00
|
|
|
icon={ <CancelIcon /> }
|
2017-05-09 17:31:48 +02:00
|
|
|
key='cancel'
|
2017-01-27 14:42:06 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.button.cancel'
|
|
|
|
defaultMessage='Cancel'
|
|
|
|
/>
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
onClick={ this.onClose }
|
2017-01-27 14:42:06 +01:00
|
|
|
/>,
|
2016-11-11 15:00:04 +01:00
|
|
|
<Button
|
2017-01-27 14:42:06 +01:00
|
|
|
disabled={ this.state.selected === -1 }
|
2016-11-11 15:00:04 +01:00
|
|
|
icon={ <CheckIcon /> }
|
2017-05-09 17:31:48 +02:00
|
|
|
key='load'
|
2017-01-27 14:42:06 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='loadContract.button.load'
|
|
|
|
defaultMessage='Load'
|
|
|
|
/>
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
onClick={ this.onLoad }
|
|
|
|
/>
|
2017-01-27 14:42:06 +01:00
|
|
|
];
|
2016-11-11 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
handleChangeTab = (event, activeTab) => {
|
|
|
|
this.setState({ activeTab, selected: -1 });
|
2016-11-11 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 14:42:06 +01:00
|
|
|
onClickContract = (event, selected) => {
|
|
|
|
this.setState({ selected });
|
2016-11-11 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onClose = () => {
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad = () => {
|
|
|
|
const { contracts, snippets } = this.props;
|
|
|
|
const { selected } = this.state;
|
|
|
|
|
|
|
|
const mergedContracts = Object.assign({}, contracts, snippets);
|
|
|
|
const contract = mergedContracts[selected];
|
|
|
|
|
|
|
|
this.props.onLoad(contract);
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
onDeleteRequest = (id) => {
|
|
|
|
this.setState({
|
|
|
|
deleteRequest: true,
|
|
|
|
deleteId: id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onConfirmRemoval = () => {
|
|
|
|
const { deleteId } = this.state;
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-11-11 15:00:04 +01:00
|
|
|
this.props.onDelete(deleteId);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
deleteRequest: false,
|
|
|
|
deleteId: -1,
|
|
|
|
selected: -1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onRejectRemoval = () => {
|
|
|
|
this.setState({
|
|
|
|
deleteRequest: false,
|
|
|
|
deleteId: -1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|