summaryrefslogtreecommitdiff
path: root/app/client/common/audioPlayer/audioPlayer.reducer.js
blob: e2493255d38f206f13b6301184ee9d8b9e7405e3 (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
import types from '../../types'

const audioPlayerInitialState = {
  loading: false,
  error: null,
  status: '',
  current: null,
  index: -1,
  playlist: [],
}

const audio = document.createElement('audio')

const audioPlayerReducer = (state = audioPlayerInitialState, action) => {
  switch(action.type) {
    case types.audioPlayer.play:
      if (! action.file.url) {
        return state
      }
      audio.src = action.file.url
      audio.play()
      return {
        ...state,
        playing: true,
        current: action.file,
      }
    case types.audioPlayer.pause:
      audio.pause()
      return {
        ...state,
        playing: false,
      }
    case types.audioPlayer.resume:
      audio.play()
      return {
        ...state,
        playing: true,
      }
    default:
      return state
  }
}

export default audioPlayerReducer