Minor refactoring on ImportServersBtn

This commit is contained in:
Alejandro Celaya 2018-08-23 15:33:05 +02:00
parent 3df5fd12d3
commit da1616542d
2 changed files with 16 additions and 12 deletions

View file

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { UncontrolledTooltip } from 'reactstrap'; import { UncontrolledTooltip } from 'reactstrap';
import serversImporter from '../services/ServersImporter'; import serversImporter, { serversImporterType } from '../services/ServersImporter';
import { createServers } from '../reducers/server'; import { createServers } from '../reducers/server';
import { assoc } from 'ramda'; import { assoc } from 'ramda';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
@ -11,7 +11,9 @@ const defaultProps = {
serversImporter, serversImporter,
}; };
const propTypes = { const propTypes = {
onChange: PropTypes.func, onImport: PropTypes.func,
serversImporter: serversImporterType,
createServers: PropTypes.func,
}; };
export class ImportServersBtn extends React.Component { export class ImportServersBtn extends React.Component {
@ -21,15 +23,12 @@ export class ImportServersBtn extends React.Component {
} }
render() { render() {
const { serversImporter, onImport } = this.props; const { serversImporter: { importServersFromFile }, onImport, createServers } = this.props;
const onChange = e => serversImporter.importServersFromFile(e.target.files[0]).then( const onChange = e =>
servers => { importServersFromFile(e.target.files[0])
const { createServers } = this.props; .then(servers => servers.map(server => assoc('id', uuid(), server)))
const serversWithIds = servers.map(server => assoc('id', uuid(), server)); .then(createServers)
createServers(serversWithIds); .then(onImport);
onImport(serversWithIds);
}
);
return ( return (
<React.Fragment> <React.Fragment>
@ -41,10 +40,10 @@ export class ImportServersBtn extends React.Component {
> >
Import from file Import from file
</button> </button>
<UncontrolledTooltip placement="top" target="importBtn"> <UncontrolledTooltip placement="top" target="importBtn">
You can create servers by importing a CSV file with columns "name", "apiKey" and "url" You can create servers by importing a CSV file with columns "name", "apiKey" and "url"
</UncontrolledTooltip> </UncontrolledTooltip>
<input <input
type="file" type="file"
onChange={onChange} onChange={onChange}

View file

@ -1,4 +1,9 @@
import csvjson from 'csvjson'; import csvjson from 'csvjson';
import PropTypes from 'prop-types';
export const serversImporterType = PropTypes.shape({
importServersFromFile: PropTypes.func,
});
export class ServersImporter { export class ServersImporter {
constructor(csvjson) { constructor(csvjson) {