Skip to content

Commit 75c5c0b

Browse files
committed
play / pause button implementation
1 parent 812f0d9 commit 75c5c0b

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/app/containers/MainContainer.jsx

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import ButtonsContainer from './ButtonsContainer';
77

88
const autoBind = require('auto-bind');
99

10+
// global variable for play function
11+
let globalPlaying = false;
12+
let intervalId = null;
13+
1014
class MainContainer extends Component {
1115
constructor(props) {
1216
super(props);
@@ -15,6 +19,7 @@ class MainContainer extends Component {
1519
snapshotIndex: 0,
1620
currentIndex: null,
1721
port: null,
22+
playing: false,
1823
};
1924

2025
autoBind(this);
@@ -46,30 +51,39 @@ class MainContainer extends Component {
4651
const { snapshots, snapshotIndex } = this.state;
4752
if (snapshots.length > 0 && snapshotIndex > 0) {
4853
const newIndex = snapshotIndex - 1;
49-
this.handleJumpSnapshot(newIndex);
50-
this.setState({ snapshotIndex: newIndex });
54+
// second callback parameter of setState to invoke handleJumpSnapshot
55+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
5156
}
5257
}
5358

5459
moveForward() {
5560
const { snapshots, snapshotIndex } = this.state;
5661
if (snapshotIndex < snapshots.length - 1) {
5762
const newIndex = snapshotIndex + 1;
58-
this.handleJumpSnapshot(newIndex);
59-
this.setState({ snapshotIndex: newIndex });
63+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
6064
}
6165
}
6266

63-
playForward() {
64-
const play = setInterval(() => {
65-
const { snapshots, snapshotIndex } = this.state;
66-
if (snapshotIndex < snapshots.length - 1) {
67-
const newIndex = snapshotIndex + 1;
68-
this.handleJumpSnapshot(newIndex);
69-
this.setState({ snapshotIndex: newIndex });
70-
} else clearInterval(play);
71-
}, 1000);
72-
play();
67+
play() {
68+
globalPlaying = !globalPlaying
69+
this.setState({playing: globalPlaying}, () => {
70+
if(this.state.playing){
71+
intervalId = setInterval(() => {
72+
const { snapshots, snapshotIndex } = this.state;
73+
if (snapshotIndex < snapshots.length - 1) {
74+
const newIndex = snapshotIndex + 1;
75+
this.setState({ snapshotIndex: newIndex}, this.handleJumpSnapshot(newIndex) );
76+
} else {
77+
// clear interval when play reaches the end
78+
globalVariable = false;
79+
clearInterval(intervalId);
80+
this.setState({ playing: false })
81+
}
82+
}, 1000);
83+
} else {
84+
clearInterval(intervalId);
85+
}
86+
})
7387
}
7488

7589
emptySnapshot() {
@@ -93,7 +107,7 @@ class MainContainer extends Component {
93107
}
94108

95109
render() {
96-
const { snapshots, snapshotIndex, port } = this.state;
110+
const { snapshots, snapshotIndex, port, playing } = this.state;
97111
return (
98112
<div className="main-container">
99113
<HeadContainer />
@@ -107,15 +121,14 @@ class MainContainer extends Component {
107121
/>
108122
<StateContainer snapshot={snapshots[snapshotIndex]} />
109123
<TravelContainer
110-
playing = {playing}
111124
snapshotsLength={snapshots.length}
125+
snapshotIndex={snapshotIndex}
112126
handleChangeSnapshot={this.handleChangeSnapshot}
113127
handleJumpSnapshot={this.handleJumpSnapshot}
114-
snapshotIndex={snapshotIndex}
115128
moveBackward={this.moveBackward}
116129
moveForward={this.moveForward}
117-
playForward={this.playForward}
118-
playing={playing}
130+
play={this.play}
131+
playing = {playing}
119132
/>
120133
<ButtonsContainer port={port} />
121134
</div>

src/app/containers/TravelContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import React from 'react';
22
import MainSlider from '../components/MainSlider';
33

44
const TravelContainer = ({
5-
playForward,
65
moveBackward,
76
moveForward,
87
snapshotsLength,
98
handleChangeSnapshot,
109
handleJumpSnapshot,
1110
snapshotIndex,
11+
play,
1212
playing
1313
}) => (
1414
<div className="travel-container">
15-
<div className="play-button" onClick={playForward}>
15+
<div className="play-button" onClick={play}>
1616
{ playing ? 'Pause': 'Play' }
1717
</div>
1818
<MainSlider

0 commit comments

Comments
 (0)