-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadModal.js
More file actions
84 lines (84 loc) · 2.77 KB
/
FileUploadModal.js
File metadata and controls
84 lines (84 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from 'react';
import { Modal, Button } from 'react-bootstrap';
import * as fileUploadService from '../services/fileUploadService'
// NOTE FOR USE:
// When using this FileUploadModal component in a new parent component/module, you will need to create a callback function in the PARENT file. example below:
//
// onFileUpload(uploadedImage) {
// this.setState({
// imageUrl: : uploadedImage
// })
// }
//
// line 57 in this file will lift up the image url to the parent component.
class FileUploadModal extends React.Component {
constructor(props) {
super(props)
this.state = {
show: false
, imgData: ''
};
this.onOpen = this.onOpen.bind(this);
this.onClose = this.onClose.bind(this);
this.fileSelectedHandler = this.fileSelectedHandler.bind(this);
this.onImgUpload = this.onImgUpload.bind(this);
}
onOpen() {
this.setState({
show: true
})
}
onClose() {
this.setState({
show: false
})
}
fileSelectedHandler = event => {
console.log(event.target.files[0])
this.setState({
imgData: event.target.files[0]
})
}
onImgUpload = event => {
const img = this.state.imgData;
console.log(img);
fileUploadService.getSignedUrl(img)
.then(response => {
const promise = fileUploadService.upload(response.url, this.state.imgData)
this.props.fileUpload(response.url)
return promise;
})
.then(this.onClose)
.catch(console.error);
}
render() {
const fileUploadClick = (
<div>
<button className='btn btn-light btn--icon-text' type='button' onClick={this.onOpen}>
<i className='zmdi zmdi-upload'></i> File Upload</button>
</div>
)
const modal = (
<Modal show={this.state.show} onHide={this.onClose}>
<Modal.Header closeButton>
<Modal.Title>File Upload</Modal.Title>
</Modal.Header>
<Modal.Body>
<div className='form-group col-sm 12'>
<input type='file' className='form-control-file' id='imageFile' name='imageFile' accept='.jpg, .jpeg, .png' onChange={this.fileSelectedHandler} />
</div>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.onImgUpload}>Upload</Button>
</Modal.Footer>
</Modal>
)
return (
<div>
{fileUploadClick}
{modal}
</div>
)
}
}
export default FileUploadModal;