Partial import processing
This commit is contained in:
@@ -1,38 +1,55 @@
|
||||
import React from 'react'
|
||||
import {Component} from 'react';
|
||||
import {connect} from 'react-redux';
|
||||
import './SkynetImport.css'
|
||||
import Box from '../../components/UI/Box/Box';
|
||||
import Button from '../../components/UI/Button/Button';
|
||||
import {
|
||||
displaySkynetImport, importSkylinks
|
||||
} from '../../redux/actions/skynet_actions';
|
||||
import {displaySkynetImport} from '../../redux/actions/skynet_actions';
|
||||
//import {ImportList} from './ImportList/ImportList'
|
||||
import IPCContainer from '../IPCContainer/IPCContainer';
|
||||
import {notifyApplicationBusy} from '../../redux/actions/common_actions';
|
||||
import {
|
||||
notifyError,
|
||||
notifyInfo
|
||||
} from '../../redux/actions/error_actions';
|
||||
|
||||
const Constants = require('../../constants');
|
||||
|
||||
const mapStateToProps = (state, ownProps) => {
|
||||
return {
|
||||
AppBusy: state.common.AppBusy,
|
||||
};
|
||||
};
|
||||
const mapDispatchToProps = dispatch => {
|
||||
return {
|
||||
displaySkynetImport: display => dispatch(displaySkynetImport(display)),
|
||||
notifyInfo: msg => dispatch(notifyInfo('Import Syntax', msg)),
|
||||
notifyApplicationBusy: busy => dispatch(notifyApplicationBusy(busy, true)),
|
||||
notifyError: msg => dispatch(notifyError(msg)),
|
||||
importSkylinks: (version, jsonArray) => dispatch(importSkylinks(version, jsonArray)),
|
||||
notifyInfo: msg => dispatch(notifyInfo('Import Syntax', msg)),
|
||||
}
|
||||
};
|
||||
|
||||
export default connect(null, mapDispatchToProps)(class extends Component {
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(class extends IPCContainer {
|
||||
|
||||
state = {
|
||||
import_list: '',
|
||||
import_text: '',
|
||||
imports_array: [],
|
||||
second_stage: false,
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
this.setRequestHandler(Constants.IPC_Import_Skylinks_Reply, this.onImportSkylinksReply);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
super.componentWillUnmount();
|
||||
}
|
||||
|
||||
displaySyntax = () => {
|
||||
const msg = '!alternate!To import Skylinks into the root directory, list each Skylink on a new line:\n' +
|
||||
' AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA\n' +
|
||||
' AACyjmDGoHqY7mTaxi-rkpnKUJGZK1B4UhrF74Nv6tY6Cg\n' +
|
||||
'\n' +
|
||||
'To import Skylinks to new or existing directories, use the following syntax:\n' +
|
||||
'To import Skylinks into new or existing directories, use the following syntax:\n' +
|
||||
' directory="/my/sub/dir",skylink="AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA"\n' +
|
||||
' directory="/my/sub/dir2",skylink="AACyjmDGoHqY7mTaxi-rkpnKUJGZK1B4UhrF74Nv6tY6Cg"\n' +
|
||||
'\n' +
|
||||
@@ -40,7 +57,7 @@ export default connect(null, mapDispatchToProps)(class extends Component {
|
||||
' directory="/my/sub/dir",skylink="AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA",token="My Password"\n' +
|
||||
' directory="/my/sub/dir",skylink="AACyjmDGoHqY7mTaxi-rkpnKUJGZK1B4UhrF74Nv6tY6Cg",token="My Password"\n' +
|
||||
'\n' +
|
||||
'To import JSON Skylinks, use the following syntax:\n' +
|
||||
'To import Skylinks using JSON syntax:\n' +
|
||||
' [\n' +
|
||||
' {\n' +
|
||||
' "directory": "/",\n' +
|
||||
@@ -56,56 +73,87 @@ export default connect(null, mapDispatchToProps)(class extends Component {
|
||||
this.props.notifyInfo(msg)
|
||||
}
|
||||
|
||||
onImportSkylinksReply = (_, arg) => {
|
||||
this.props.notifyApplicationBusy(false);
|
||||
if (arg.data.Success) {
|
||||
console.log(arg.data.Result);
|
||||
this.setState({
|
||||
import_text: '',
|
||||
imports_array: [],
|
||||
second_stage: false,
|
||||
});
|
||||
} else {
|
||||
this.props.notifyError(arg.data.Error);
|
||||
}
|
||||
};
|
||||
|
||||
processNext = () => {
|
||||
const items = this.state.import_list.split('\n');
|
||||
let importsArray = [];
|
||||
try {
|
||||
for (let item of items) {
|
||||
item = item.trim();
|
||||
if (item.startsWith('[')) {
|
||||
importsArray = JSON.parse(this.state.import_list.trim());
|
||||
break;
|
||||
} else if (item.indexOf('=') >= 0) {
|
||||
const parts = item.split(',')
|
||||
let importItem = {
|
||||
directory: '/',
|
||||
skylink: '',
|
||||
token: '',
|
||||
};
|
||||
for (let part of parts) {
|
||||
part = part.trim();
|
||||
const pair = part.split('=');
|
||||
if (pair.length !== 2) {
|
||||
if (this.state.second_stage) {
|
||||
try {
|
||||
this.props.notifyApplicationBusy(true);
|
||||
this.sendRequest(Constants.IPC_Import_Skylinks, {
|
||||
Version: this.props.version,
|
||||
JsonArray: this.state.imports_array,
|
||||
});
|
||||
} catch (e) {
|
||||
this.props.notifyApplicationBusy(false);
|
||||
this.props.notifyError(e);
|
||||
}
|
||||
} else {
|
||||
const items = this.state.import_text.split('\n');
|
||||
let importsArray = [];
|
||||
try {
|
||||
for (let item of items) {
|
||||
item = item.trim();
|
||||
if (item.startsWith('[')) {
|
||||
importsArray = JSON.parse(this.state.import_text.trim());
|
||||
break;
|
||||
} else if (item.indexOf('=') >= 0) {
|
||||
const parts = item.split(',')
|
||||
let importItem = {
|
||||
directory: '/',
|
||||
skylink: '',
|
||||
token: '',
|
||||
};
|
||||
for (let part of parts) {
|
||||
part = part.trim();
|
||||
const pair = part.split('=');
|
||||
if (pair.length !== 2) {
|
||||
throw new Error('Invalid syntax for import: directory="",skylink="",token=""');
|
||||
}
|
||||
importItem = {
|
||||
...importItem,
|
||||
[pair[0].trim()]: pair[1].trim().replace(/"/g, ''),
|
||||
};
|
||||
}
|
||||
if (!importItem.skylink) {
|
||||
throw new Error('Invalid syntax for import: directory="",skylink="",token=""');
|
||||
}
|
||||
importItem = {
|
||||
...importItem,
|
||||
[pair[0].trim()]: pair[1].trim().replace(/"/g, ''),
|
||||
};
|
||||
importsArray.push(importItem);
|
||||
} else if (item.length > 0) {
|
||||
importsArray.push({
|
||||
directory: '/',
|
||||
skylink: item,
|
||||
});
|
||||
}
|
||||
if (!importItem.skylink) {
|
||||
throw new Error('Invalid syntax for import: directory="",skylink="",token=""');
|
||||
}
|
||||
importsArray.push(importItem);
|
||||
} else if (item.length > 0) {
|
||||
importsArray.push({
|
||||
directory: '/',
|
||||
skylink: item,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (importsArray.length === 0) {
|
||||
throw new Error('Nothing to import');
|
||||
if (importsArray.length === 0) {
|
||||
throw new Error('Nothing to import');
|
||||
}
|
||||
this.setState({
|
||||
imports_array: importsArray,
|
||||
second_stage: true,
|
||||
});
|
||||
} catch (e) {
|
||||
this.props.notifyError(e);
|
||||
}
|
||||
this.props.importSkylinks(this.props.version, importsArray);
|
||||
} catch (e) {
|
||||
this.props.notifyError(e);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
//<ImportList data={this.state.imports_array}/>
|
||||
return this.props.AppBusy ? (<div/>) : (
|
||||
<Box dxDark dxStyle={{
|
||||
height: 'auto',
|
||||
padding: 'var(--default_spacing)',
|
||||
@@ -118,15 +166,19 @@ export default connect(null, mapDispatchToProps)(class extends Component {
|
||||
style={{cursor: 'pointer'}}>X</a>
|
||||
</div>
|
||||
<h1 className={'SkynetImportHeading'}>Import List</h1>
|
||||
<textarea autoFocus={true}
|
||||
className={'SkynetImportTextArea'}
|
||||
onChange={e => this.setState({
|
||||
...this.state,
|
||||
import_list: e.target.value,
|
||||
})}
|
||||
value={this.state.import_list}
|
||||
rows={10}/>
|
||||
|
||||
{
|
||||
this.state.second_stage ? (
|
||||
<div/>
|
||||
) : (
|
||||
<textarea autoFocus={true}
|
||||
className={'SkynetImportTextArea'}
|
||||
onChange={e => this.setState({
|
||||
import_text: e.target.value,
|
||||
})}
|
||||
value={this.state.import_text}
|
||||
rows={10}/>
|
||||
)
|
||||
}
|
||||
<div className={'SkynetImportButtons'}>
|
||||
<Button buttonStyles={{height: 'auto', marginTop: 'var(--default_spacing)', width: 'auto'}}
|
||||
clicked={this.displaySyntax}>Import Syntax...</Button>
|
||||
@@ -135,7 +187,7 @@ export default connect(null, mapDispatchToProps)(class extends Component {
|
||||
marginLeft: 'var(--default_spacing)',
|
||||
marginTop: 'var(--default_spacing)',
|
||||
width: 'auto'
|
||||
}} clicked={this.processNext}>Next</Button>
|
||||
}} clicked={this.processNext}>{this.state.second_stage ? 'Import' : 'Next'}</Button>
|
||||
</div>
|
||||
</Box>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user