aboutsummaryrefslogtreecommitdiff
path: root/src/components/media_upload/media_upload.js
blob: 746970aad864c06db03b17d15e4b28fa2551f24b (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
48
49
50
/* eslint-env browser */
import statusPosterService from '../../services/status_poster/status_poster.service.js'

const mediaUpload = {
  mounted () {
    const input = this.$el.querySelector('input')

    input.addEventListener('change', ({target}) => {
      const file = target.files[0]
      this.uploadFile(file)
    })
  },
  data () {
    return {
      uploading: false
    }
  },
  methods: {
    uploadFile (file) {
      const self = this
      const store = this.$store
      const formData = new FormData()
      formData.append('media', file)

      self.$emit('uploading')
      self.uploading = true

      statusPosterService.uploadMedia({ store, formData })
        .then((fileData) => {
          self.$emit('uploaded', fileData)
          self.uploading = false
        }, (error) => {
          self.$emit('upload-failed')
          self.uploading = false
        })
    }
  },
  props: [
    'dropFiles'
  ],
  watch: {
    'dropFiles': function (fileInfos) {
      if (!this.uploading) {
        this.uploadFile(fileInfos[0])
      }
    }
  }
}

export default mediaUpload