| 14 | }); |
| 15 | |
| 16 | class FileImport extends Component { |
| 17 | onDrop = (acceptedFiles) => { |
| 18 | if (!acceptedFiles || !acceptedFiles.length) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | const { intl } = this.props; |
| 23 | const file = acceptedFiles[0]; |
| 24 | const fileName = file.name; |
| 25 | const label = fileName.match(/^([^.]+)/)[0]; |
| 26 | const reader = new FileReader(); |
| 27 | |
| 28 | reader.onload = async (e) => { |
| 29 | const data = e.target.result; |
| 30 | this.props.onImport({ fileName, label, data }); |
| 31 | }; |
| 32 | reader.onerror = async () => { |
| 33 | showWarningToast(intl.formatMessage(messages.import_error)); |
| 34 | }; |
| 35 | reader.readAsText(file); |
| 36 | }; |
| 37 | |
| 38 | render() { |
| 39 | const { accept, importedFile, placeholder } = this.props; |
| 40 | |
| 41 | return ( |
| 42 | <Dropzone |
| 43 | accept={accept} |
| 44 | onDrop={this.onDrop} |
| 45 | noDragEventsBubbling |
| 46 | useFsAccessApi={false} |
| 47 | > |
| 48 | {({ getRootProps, getInputProps }) => ( |
| 49 | <div {...getRootProps()}> |
| 50 | <input {...getInputProps()} /> |
| 51 | {importedFile && ( |
| 52 | <Button |
| 53 | className="FileImport__file-name" |
| 54 | icon="document-open" |
| 55 | text={importedFile} |
| 56 | intent={Intent.PRIMARY} |
| 57 | minimal |
| 58 | /> |
| 59 | )} |
| 60 | {!importedFile && ( |
| 61 | <div className="FileImport__placeholder">{placeholder}</div> |
| 62 | )} |
| 63 | </div> |
| 64 | )} |
| 65 | </Dropzone> |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | export default injectIntl(FileImport); |
nothing calls this directly
no test coverage detected