| Total Complexity | 1 |
| Complexity/F | 1 |
| Lines of Code | 59 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Coverage | 92.31% |
| Changes | 0 | ||
| 1 | import React from 'react'; |
||
| 2 | import { UncontrolledTooltip } from 'reactstrap'; |
||
| 3 | import PropTypes from 'prop-types'; |
||
| 4 | |||
| 5 | 3 | const ImportServersBtn = (serversImporter) => class ImportServersBtn extends React.Component { |
|
| 6 | 3 | static defaultProps = { |
|
| 7 | onImport: () => ({}), |
||
| 8 | }; |
||
| 9 | 3 | static propTypes = { |
|
| 10 | onImport: PropTypes.func, |
||
| 11 | createServers: PropTypes.func, |
||
| 12 | fileRef: PropTypes.oneOfType([ PropTypes.object, PropTypes.node ]), |
||
| 13 | }; |
||
| 14 | |||
| 15 | constructor(props) { |
||
| 16 | 3 | super(props); |
|
| 17 | 3 | this.fileRef = props.fileRef || React.createRef(); |
|
| 18 | } |
||
| 19 | |||
| 20 | render() { |
||
| 21 | 3 | const { importServersFromFile } = serversImporter; |
|
| 22 | 3 | const { onImport, createServers } = this.props; |
|
| 23 | 3 | const onChange = ({ target }) => |
|
| 24 | 1 | importServersFromFile(target.files[0]) |
|
| 25 | .then(createServers) |
||
| 26 | .then(onImport) |
||
| 27 | .then(() => { |
||
| 28 | // Reset input after processing file |
||
| 29 | 1 | target.value = null; |
|
| 30 | }); |
||
| 31 | |||
| 32 | 3 | return ( |
|
| 33 | <React.Fragment> |
||
| 34 | <button |
||
| 35 | type="button" |
||
| 36 | className="btn btn-outline-secondary mr-2" |
||
| 37 | id="importBtn" |
||
| 38 | 1 | onClick={() => this.fileRef.current.click()} |
|
| 39 | > |
||
| 40 | Import from file |
||
| 41 | </button> |
||
| 42 | <UncontrolledTooltip placement="top" target="importBtn"> |
||
| 43 | You can create servers by importing a CSV file with columns <b>name</b>, <b>apiKey</b> and <b>url</b> |
||
| 44 | </UncontrolledTooltip> |
||
| 45 | |||
| 46 | <input |
||
| 47 | type="file" |
||
| 48 | accept="text/csv" |
||
| 49 | className="create-server__csv-select" |
||
| 50 | ref={this.fileRef} |
||
| 51 | onChange={onChange} |
||
| 52 | /> |
||
| 53 | </React.Fragment> |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | }; |
||
| 57 | |||
| 58 | export default ImportServersBtn; |
||
| 59 |