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 React, { Component, PropTypes } from 'react';
|
|
|
|
import Dropzone from 'react-dropzone';
|
|
|
|
import FileUploadIcon from 'material-ui/svg-icons/file/file-upload';
|
|
|
|
import ContentClear from 'material-ui/svg-icons/content/clear';
|
|
|
|
import ActionDoneAll from 'material-ui/svg-icons/action/done-all';
|
|
|
|
|
2016-11-26 17:06:37 +01:00
|
|
|
import Button from '../../Button';
|
|
|
|
import Modal from '../../Modal';
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
import styles from './import.css';
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
step: 0,
|
|
|
|
show: false,
|
|
|
|
validate: false,
|
|
|
|
validationBody: null,
|
2016-11-14 18:34:05 +01:00
|
|
|
error: false,
|
|
|
|
errorText: '',
|
2016-11-11 15:00:04 +01:00
|
|
|
content: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class ActionbarImport extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
onConfirm: PropTypes.func.isRequired,
|
|
|
|
renderValidation: PropTypes.func,
|
|
|
|
className: PropTypes.string,
|
|
|
|
title: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
title: 'Import from a file'
|
|
|
|
};
|
|
|
|
|
|
|
|
state = Object.assign({}, initialState);
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { className } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
className={ className }
|
|
|
|
icon={ <FileUploadIcon /> }
|
|
|
|
label='import'
|
|
|
|
onClick={ this.onOpenModal }
|
|
|
|
/>
|
|
|
|
{ this.renderModal() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderModal () {
|
|
|
|
const { title, renderValidation } = this.props;
|
2016-11-14 18:34:05 +01:00
|
|
|
const { show, step, error } = this.state;
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
if (!show) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasSteps = typeof renderValidation === 'function';
|
|
|
|
|
2016-11-14 18:34:05 +01:00
|
|
|
const steps = hasSteps ? [ 'select a file', error ? 'error' : 'validate' ] : null;
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
actions={ this.renderActions() }
|
|
|
|
title={ title }
|
|
|
|
steps={ steps }
|
|
|
|
current={ step }
|
|
|
|
visible
|
|
|
|
>
|
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderActions () {
|
2016-11-14 18:34:05 +01:00
|
|
|
const { validate, error } = this.state;
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
const cancelBtn = (
|
|
|
|
<Button
|
|
|
|
key='cancel'
|
|
|
|
label='Cancel'
|
|
|
|
icon={ <ContentClear /> }
|
|
|
|
onClick={ this.onCloseModal }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2016-11-14 18:34:05 +01:00
|
|
|
if (error) {
|
|
|
|
return [ cancelBtn ];
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:00:04 +01:00
|
|
|
if (validate) {
|
|
|
|
const confirmBtn = (
|
|
|
|
<Button
|
|
|
|
key='confirm'
|
|
|
|
label='Confirm'
|
|
|
|
icon={ <ActionDoneAll /> }
|
|
|
|
onClick={ this.onConfirm }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return [ cancelBtn, confirmBtn ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ cancelBtn ];
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBody () {
|
2016-11-14 18:34:05 +01:00
|
|
|
const { validate, errorText, error } = this.state;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>An error occured: { errorText }</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-11-11 15:00:04 +01:00
|
|
|
|
|
|
|
if (validate) {
|
|
|
|
return this.renderValidation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderFileSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderFileSelect () {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Dropzone
|
|
|
|
onDrop={ this.onDrop }
|
|
|
|
multiple={ false }
|
|
|
|
className={ styles.importZone }
|
|
|
|
>
|
|
|
|
<div>Drop a file here, or click to select a file to upload.</div>
|
|
|
|
</Dropzone>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderValidation () {
|
|
|
|
const { validationBody } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p className={ styles.desc }>
|
|
|
|
Confirm that this is what was intended to import.
|
|
|
|
</p>
|
|
|
|
<div>
|
|
|
|
{ validationBody }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onDrop = (files) => {
|
|
|
|
const { renderValidation } = this.props;
|
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
const content = e.target.result;
|
|
|
|
|
|
|
|
if (typeof renderValidation !== 'function') {
|
|
|
|
this.props.onConfirm(content);
|
|
|
|
return this.onCloseModal();
|
|
|
|
}
|
|
|
|
|
2016-11-14 18:34:05 +01:00
|
|
|
const validationBody = renderValidation(content);
|
|
|
|
|
|
|
|
if (validationBody && validationBody.error) {
|
|
|
|
return this.setState({
|
|
|
|
step: 1,
|
|
|
|
error: true,
|
|
|
|
errorText: validationBody.error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:00:04 +01:00
|
|
|
this.setState({
|
|
|
|
step: 1,
|
|
|
|
validate: true,
|
2016-11-14 18:34:05 +01:00
|
|
|
validationBody,
|
2016-11-11 15:00:04 +01:00
|
|
|
content
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
onConfirm = () => {
|
|
|
|
const { content } = this.state;
|
|
|
|
|
|
|
|
this.props.onConfirm(content);
|
|
|
|
return this.onCloseModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
onOpenModal = () => {
|
|
|
|
this.setState({ show: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
onCloseModal = () => {
|
|
|
|
this.setState(initialState);
|
|
|
|
}
|
|
|
|
}
|