|
1 | | -import React, { Component }from 'react'; |
2 | | -import Sound from 'react-sound'; |
| 1 | +import React, { Component } from 'react' |
| 2 | +import Sound from 'react-sound' |
3 | 3 | import cookie from 'react-cookie' |
4 | | -import songs from '../sound'; |
| 4 | +import songs from '../sound' |
5 | 5 |
|
6 | 6 | export default class Backgroundtrack extends Component { |
7 | | - |
8 | | - constructor(props) { |
9 | | - super(props); |
| 7 | + constructor (props) { |
| 8 | + super(props) |
10 | 9 |
|
11 | 10 | this.state = { |
12 | | - theme : 0, |
| 11 | + theme: 0, |
13 | 12 | currentSong: songs[0], |
14 | 13 | position: 0, |
15 | 14 | volume: 25, |
16 | 15 | playStatus: Sound.status.STOPPED |
17 | | - }; |
| 16 | + } |
18 | 17 | } |
19 | 18 |
|
20 | | - getStatusText() { |
21 | | - switch(this.state.playStatus) { |
| 19 | + getStatusText () { |
| 20 | + switch (this.state.playStatus) { |
22 | 21 | case Sound.status.PLAYING: |
23 | | - return 'playing'; |
| 22 | + return 'playing' |
24 | 23 | case Sound.status.PAUSED: |
25 | | - return 'paused'; |
| 24 | + return 'paused' |
26 | 25 | case Sound.status.STOPPED: |
27 | | - return 'stopped'; |
| 26 | + return 'stopped' |
28 | 27 | default: |
29 | | - return '(unknown)'; |
| 28 | + return '(unknown)' |
30 | 29 | } |
31 | 30 | } |
32 | 31 |
|
33 | 32 | componentDidMount () { |
34 | 33 | const cookies = cookie.load('sound') |
35 | 34 | if (cookies === undefined) { |
36 | | - cookie.save('sound', 'ON'); |
37 | | - this.setState({playStatus: Sound.status.PLAYING}); |
38 | | - } |
39 | | - else if(cookies === 'ON'){ |
40 | | - this.setState({playStatus: Sound.status.PLAYING}); |
41 | | - } |
42 | | - else { |
43 | | - this.setState({playStatus: Sound.status.STOPPED}); |
| 35 | + cookie.save('sound', 'ON') |
| 36 | + this.setState({ playStatus: Sound.status.PLAYING }) |
| 37 | + } else if (cookies === 'ON') { |
| 38 | + this.setState({ playStatus: Sound.status.PLAYING }) |
| 39 | + } else { |
| 40 | + this.setState({ playStatus: Sound.status.STOPPED }) |
44 | 41 | } |
45 | 42 | } |
46 | 43 |
|
47 | | - componentDidUpdate(){ |
48 | | - var newTheme = 0 |
49 | | - if (this.props.currentLevel >=1 && this.props.currentLevel < 6 ) { |
50 | | - newTheme = 0; |
51 | | - } |
52 | | - else if (this.props.currentLevel >=6 && this.props.currentLevel < 11) { |
53 | | - newTheme = 1; |
54 | | - } |
55 | | - else if (this.props.currentLevel >=11 && this.props.currentLevel < 16) { |
56 | | - newTheme = 2; |
57 | | - } |
58 | | - else if (this.props.currentLevel >=16 && this.props.currentLevel < 21) { |
59 | | - newTheme = 3; |
60 | | - } |
61 | | - else { |
62 | | - newTheme = 4; |
| 44 | + componentDidUpdate () { |
| 45 | + let newTheme = 0 |
| 46 | + if (this.props.currentLevel >= 1 && this.props.currentLevel < 6) { |
| 47 | + newTheme = 0 |
| 48 | + } else if (this.props.currentLevel >= 6 && this.props.currentLevel < 11) { |
| 49 | + newTheme = 1 |
| 50 | + } else if (this.props.currentLevel >= 11 && this.props.currentLevel < 16) { |
| 51 | + newTheme = 2 |
| 52 | + } else if (this.props.currentLevel >= 16 && this.props.currentLevel < 21) { |
| 53 | + newTheme = 3 |
| 54 | + } else { |
| 55 | + newTheme = 4 |
63 | 56 | } |
64 | 57 |
|
65 | | - if(this.state.theme !== newTheme){ |
66 | | - this.setState({theme: newTheme, currentSong: songs[newTheme]}) |
| 58 | + if (this.state.theme !== newTheme) { |
| 59 | + this.setState({ theme: newTheme, currentSong: songs[newTheme] }) |
67 | 60 | } |
68 | 61 |
|
69 | 62 | const cookies = cookie.load('sound') |
70 | 63 | const isAlreadySet = this.state.playStatus === Sound.status.PLAYING |
71 | | - if(cookies === 'ON'){ |
| 64 | + if (cookies === 'ON') { |
72 | 65 | if (isAlreadySet) return |
73 | | - this.setState({playStatus: Sound.status.PLAYING}) |
74 | | - } |
75 | | - else { |
| 66 | + this.setState({ playStatus: Sound.status.PLAYING }) |
| 67 | + } else { |
76 | 68 | if (!isAlreadySet) return |
77 | | - this.setState({playStatus: Sound.status.STOPPED}) |
| 69 | + this.setState({ playStatus: Sound.status.STOPPED }) |
78 | 70 | } |
79 | 71 | } |
80 | 72 |
|
81 | | - render() { |
82 | | - return( |
83 | | - <div> |
| 73 | + render () { |
| 74 | + return ( |
| 75 | + <div> |
84 | 76 | <Sound |
85 | 77 | url={this.state.currentSong.url} |
86 | 78 | playStatus={this.state.playStatus} |
87 | 79 | playFromPosition={this.state.position} |
88 | 80 | volume={this.state.volume} |
89 | | - onFinishedPlaying={() => this.setState({playStatus: Sound.status.PLAYING})} /> |
90 | | - </div> |
| 81 | + onFinishedPlaying={() => this.setState({ playStatus: Sound.status.PLAYING })} |
| 82 | + /> |
| 83 | + </div> |
91 | 84 | ) |
92 | 85 | } |
93 | 86 | } |
0 commit comments