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

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

    input.addEventListener('change', ({target}) => {
      const file = target.files[0]
      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
        })
    })
  },
  data () {
    return {
      uploading: false
    }
  }
}

export default mediaUpload