summaryrefslogtreecommitdiff
path: root/app/client/common/fileUpload.component.js
blob: d5ccd79b3404955922d1e416077e66b14737ec67 (plain)
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
import { h, Component } from 'preact'

class FileUpload extends Component {
  constructor(props){
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }
  handleChange(e){
    this.props.onChange && this.props.onChange()
    e.stopPropagation()
    e.preventDefault()
    this.setState({ thumbnails: [], images: [] })
    const files = e.dataTransfer ? e.dataTransfer.files : e.target.files
    const mimeFiles = []
    let i, f
    for (i = 0, f; i < files.length; i++) {
      f = files[i]
      if (!f) continue
      mimeFiles.push(f)
      // if (!f.type.match(this.props.mime)) continue
    }
    const promises = mimeFiles.map(f => (
      (resolve, reject) => {
        this.props.onUpload(f).then(resolve).catch(reject)
      }
    ))
    promises.reduce((p, fn) => p.then(fn), Promise.resolve())
  }
  render() {
    return (
      <div className='fileUpload param'>
        <label>
          <span>{this.props.title}</span>
          <input
            type='file'
            accept={this.props.accept || '*/*'}
            multiple={this.props.multiple}
            onChange={this.handleChange}
          />
          <button>{this.props.label || 'Choose file...'}</button>
        </label>
      </div>
    )
  }
}

export default FileUpload