summaryrefslogtreecommitdiff
path: root/frontend/app/views/graph/components/audio.list.js
blob: b7bf19aa69139fdb9b2693d943e35f45653c208b (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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'

import { history } from 'app/store'
import actions from 'app/actions'

class AudioList extends Component {
  state = {
    playing: false,
    play_id: -1,
  }

  constructor(props) {
    super(props)
    this.toggleAudio = this.toggleAudio.bind(this)
    this.upload = this.upload.bind(this)
    this.audioDidEnd = this.audioDidEnd.bind(this)
  }

  componentDidMount() {
    this.audioElement = document.createElement('audio')
    this.audioElement.addEventListener('ended', this.audioDidEnd)
  }

  componentWillUnmount() {
    this.audioElement.removeEventListener('ended', this.audioDidEnd)
    this.audioElement.pause()
    this.audioElement = null
  }

  audioDidEnd() {
    this.setState({ playing: false })
  }

  upload(e) {
    e.preventDefault()
    document.body.className = ''
    const files = e.dataTransfer ? e.dataTransfer.files : e.target.files
    let i
    if (!files.length) return
    Array.from(files).forEach(file => this.uploadTaggedFile(file, 'audio', file.filename))
  }

  uploadTaggedFile(file, tag, fn) {
    return new Promise((resolve, reject) => {
      this.setState({ status: "Uploading " + tag + "..." })
      const uploadData = {
        tag,
        file,
        __file_filename: fn,
        graph_id: this.props.graph.id,
        username: 'swimmer',
      }
      // console.log(uploadData)
      return actions.upload.upload(uploadData).then(data => {
        // console.log(data)
        resolve({
          ...data.res,
        })
      })
    })
  }

  destroyFile(upload) {
    return new Promise((resolve, reject) => {
      actions.upload.destroy(upload)
      .then(() => {
        console.log('Destroy successful')
        resolve()
      })
      .catch(() => {
        console.log('Error deleting the file')
        reject()
      })
    })
  }

  toggleAudio(upload) {
    console.log(upload)
    let playing = false
    if (this.state.play_id === upload.id && this.state.playing) {
      this.audioElement.pause()
    } else {
      this.audioElement.src = upload.url
      this.audioElement.currentTime = 0
      this.audioElement.play()
      playing = true
    }
    this.setState({
      playing,
      play_id: upload.id,
    })
  }

  render() {
    const { playing, play_id } = this.state
    const { graph } = this.props
    // console.log(graph.uploads)
    return (
      <div className='box audioList'>
        <div className="uploadButton">
          <button>
            <span>
              {"Upload an audio file"}
            </span>
          </button>
          <input
            type="file"
            accept="audio/mp3"
            onChange={this.upload}
            required={this.props.required}
          />
        </div>
        {graph.uploads.map(upload => (
          <div className='audioItem' key={upload.id} onClick={() => this.toggleAudio(upload)} >
            <img
              className='playButton'
              src={
                (playing && play_id === upload.id)
                ? "/static/img/icons_pause_white.svg"
                : "/static/img/icons_play_white.svg"
              }
            />
            <div className='title'>
              <div>{unslugify(upload.fn)}</div>
            </div>
          </div>
        ))}
      </div>
    )
  }
}

const unslugify = fn => fn.replace(/-/g, ' ').replace(/_/g, ' ').replace('.mp3', '')

const mapStateToProps = state => ({
  graph: state.graph.show.res,
})

const mapDispatchToProps = dispatch => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(AudioList)


/*
 - upload new audio file
 */