Skip to content

Commit 7fd71a3

Browse files
committed
separated view and slider index
1 parent 0767a77 commit 7fd71a3

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed

src/app/components/MainSlider.jsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import Slider from 'rc-slider';
33
import Tooltip from 'rc-tooltip';
4-
4+
import PropTypes from 'prop-types';
55

66
const { Handle } = Slider;
77

@@ -23,27 +23,34 @@ const handle = (props) => {
2323
);
2424
};
2525

26-
class mainSlider extends Component {
27-
constructor(props) {
28-
super(props);
29-
}
26+
const MainSlider = ({
27+
snapshotLength,
28+
handleChangeSnapshot,
29+
sliderIndex,
30+
handleJumpSnapshot,
31+
pause,
32+
}) =>
33+
(
34+
<Slider
35+
min={0}
36+
max={snapshotLength - 1}
37+
value={sliderIndex}
38+
onChange={(index) => {
39+
const newIndex = index === -1 ? 0 : index;
40+
handleChangeSnapshot(newIndex);
41+
handleJumpSnapshot(newIndex);
42+
pause();
43+
}}
44+
handle={handle}
45+
/>
46+
);
3047

31-
render() {
32-
return (
33-
<Slider
34-
min={0}
35-
max={this.props.snapshotLength - 1}
36-
value={this.props.snapshotIndex}
37-
onChange={(index) => {
38-
index = index === -1 ? 0 : index;
39-
this.props.handleChangeSnapshot(index);
40-
this.props.handleJumpSnapshot(index);
41-
this.props.pause();
42-
}}
43-
handle={handle}
44-
/>
45-
);
46-
}
47-
}
48+
MainSlider.propTypes = {
49+
snapshotLength: PropTypes.number.isRequired,
50+
handleChangeSnapshot: PropTypes.func.isRequired,
51+
sliderIndex: PropTypes.number.isRequired,
52+
handleJumpSnapshot: PropTypes.func.isRequired,
53+
pause: PropTypes.func.isRequired,
54+
};
4855

49-
export default mainSlider;
56+
export default MainSlider;

src/app/containers/ActionContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Action from '../components/Action';
44

55
const ActionContainer = ({
66
snapshots,
7-
snapshotIndex,
7+
viewIndex,
88
handleChangeSnapshot,
99
handleJumpSnapshot,
1010
emptySnapshot,
1111
}) => {
1212
let actionsArr = [];
1313
if (snapshots.length > 0) {
1414
actionsArr = snapshots.map((snapshot, index) => {
15-
const selected = index === snapshotIndex;
15+
const selected = index === viewIndex;
1616
return (
1717
<Action
1818
key={`action${index}`}
@@ -41,7 +41,7 @@ ActionContainer.propTypes = {
4141
snapshots: PropTypes.arrayOf(
4242
PropTypes.object,
4343
).isRequired,
44-
snapshotIndex: PropTypes.number.isRequired,
44+
viewIndex: PropTypes.number.isRequired,
4545
handleChangeSnapshot: PropTypes.func.isRequired,
4646
handleJumpSnapshot: PropTypes.func.isRequired,
4747
emptySnapshot: PropTypes.func.isRequired,

src/app/containers/MainContainer.jsx

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class MainContainer extends Component {
1717

1818
this.state = {
1919
snapshots: [],
20-
snapshotIndex: 0,
20+
sliderIndex: 0,
21+
viewIndex: 0,
2122
port: null,
2223
playing: false,
2324
mode: {
@@ -39,15 +40,17 @@ class MainContainer extends Component {
3940
const { action, payload } = message;
4041
switch (action) {
4142
case 'sendSnapshots': {
42-
const snapshotIndex = payload.length - 1;
43+
const viewIndex = payload.length - 1;
44+
const sliderIndex = viewIndex;
4345
// set state with the information received from the background script
44-
this.setState({ snapshots: payload, snapshotIndex });
46+
this.setState({ snapshots: payload, viewIndex, sliderIndex });
4547
break;
4648
}
4749
case 'initialConnectSnapshots': {
4850
const { snapshots, mode } = payload;
49-
const snapshotIndex = snapshots.length - 1;
50-
this.setState({ snapshots, snapshotIndex, mode });
51+
const viewIndex = snapshots.length - 1;
52+
const sliderIndex = viewIndex;
53+
this.setState({ snapshots, mode, viewIndex, sliderIndex });
5154
break;
5255
}
5356
default:
@@ -64,21 +67,21 @@ class MainContainer extends Component {
6467
}
6568

6669
moveBackward() {
67-
const { snapshots, snapshotIndex } = this.state;
70+
const { snapshots, sliderIndex } = this.state;
6871
this.pause();
69-
if (snapshots.length > 0 && snapshotIndex > 0) {
70-
const newIndex = snapshotIndex - 1;
72+
if (snapshots.length > 0 && sliderIndex > 0) {
73+
const newIndex = sliderIndex - 1;
7174
// second callback parameter of setState to invoke handleJumpSnapshot
72-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
75+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
7376
}
7477
}
7578

7679
moveForward() {
77-
const { snapshots, snapshotIndex } = this.state;
80+
const { snapshots, sliderIndex } = this.state;
7881
this.pause();
79-
if (snapshotIndex < snapshots.length - 1) {
80-
const newIndex = snapshotIndex + 1;
81-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
82+
if (sliderIndex < snapshots.length - 1) {
83+
const newIndex = sliderIndex + 1;
84+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
8285
}
8386
}
8487

@@ -88,10 +91,10 @@ class MainContainer extends Component {
8891
const { playing } = this.state;
8992
if (playing) {
9093
intervalId = setInterval(() => {
91-
const { snapshots, snapshotIndex } = this.state;
92-
if (snapshotIndex < snapshots.length - 1) {
93-
const newIndex = snapshotIndex + 1;
94-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
94+
const { snapshots, sliderIndex } = this.state;
95+
if (sliderIndex < snapshots.length - 1) {
96+
const newIndex = sliderIndex + 1;
97+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
9598
} else {
9699
// clear interval when play reaches the end
97100
globalPlaying = false;
@@ -111,22 +114,22 @@ class MainContainer extends Component {
111114

112115
emptySnapshot() {
113116
const { port, snapshots } = this.state;
114-
this.setState({ snapshots: [snapshots[0]], snapshotIndex: 0 });
117+
this.setState({ snapshots: [snapshots[0]], sliderIndex: 0, viewIndex: 0 });
115118
port.postMessage({ action: 'emptySnap' });
116119
}
117120

118-
// change the snapshot index
121+
// change the view index
119122
// this will change the state shown in the state container but won't change the DOM
120-
handleChangeSnapshot(snapshotIndex) {
121-
// snapshotIndex
122-
// --> 1. affects the action that is highlighted
123-
// --> 2. moves the slider
124-
this.setState({ snapshotIndex });
123+
handleChangeSnapshot(viewIndex) {
124+
this.setState({ viewIndex });
125125
}
126126

127-
handleJumpSnapshot(snapshotIndex) {
127+
// changes the slider index
128+
// this will change the dom but not the state shown in the state container
129+
handleJumpSnapshot(sliderIndex) {
128130
const { snapshots, port } = this.state;
129-
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
131+
port.postMessage({ action: 'jumpToSnap', payload: snapshots[sliderIndex] });
132+
this.setState({ sliderIndex });
130133
}
131134

132135
importSnapshots() {
@@ -189,23 +192,23 @@ class MainContainer extends Component {
189192

190193
render() {
191194
const {
192-
snapshots, snapshotIndex, mode, playing, playSpeed,
195+
snapshots, viewIndex, sliderIndex, mode, playing, playSpeed,
193196
} = this.state;
194197
return (
195198
<div className="main-container">
196199
<HeadContainer />
197200
<div className="body-container">
198201
<ActionContainer
199202
snapshots={snapshots}
200-
snapshotIndex={snapshotIndex}
203+
viewIndex={viewIndex}
201204
handleChangeSnapshot={this.handleChangeSnapshot}
202205
handleJumpSnapshot={this.handleJumpSnapshot}
203206
emptySnapshot={this.emptySnapshot}
204207
/>
205-
{(snapshots.length) ? <StateContainer snapshot={snapshots[snapshotIndex]} /> : null}
208+
{(snapshots.length) ? <StateContainer snapshot={snapshots[viewIndex]} /> : null}
206209
<TravelContainer
207210
snapshotsLength={snapshots.length}
208-
snapshotIndex={snapshotIndex}
211+
sliderIndex={sliderIndex}
209212
handleChangeSnapshot={this.handleChangeSnapshot}
210213
handleJumpSnapshot={this.handleJumpSnapshot}
211214
moveBackward={this.moveBackward}

src/app/containers/TravelContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TravelContainer extends Component {
2929
snapshotsLength,
3030
handleChangeSnapshot,
3131
handleJumpSnapshot,
32-
snapshotIndex,
32+
sliderIndex,
3333
play,
3434
playing,
3535
pause,
@@ -43,7 +43,7 @@ class TravelContainer extends Component {
4343
<MainSlider
4444
snapshotLength={snapshotsLength}
4545
handleChangeSnapshot={handleChangeSnapshot}
46-
snapshotIndex={snapshotIndex}
46+
sliderIndex={sliderIndex}
4747
handleJumpSnapshot={handleJumpSnapshot}
4848
pause={pause}
4949
/>
@@ -71,7 +71,7 @@ TravelContainer.propTypes = {
7171
snapshotsLength: PropTypes.number.isRequired,
7272
handleChangeSnapshot: PropTypes.func.isRequired,
7373
handleJumpSnapshot: PropTypes.func.isRequired,
74-
snapshotIndex: PropTypes.number.isRequired,
74+
sliderIndex: PropTypes.number.isRequired,
7575
playing: PropTypes.bool.isRequired,
7676
};
7777
export default TravelContainer;

0 commit comments

Comments
 (0)