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