Skip to content

Commit d89359b

Browse files
authored
Merge pull request #50 from oslabs-beta/rydang/snapshot-and-webpack
state viewer and slider index separated. webpack updated
2 parents 13aa466 + 2d84d3b commit d89359b

18 files changed

+155
-132
lines changed

.gitignore

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
node_modules
22
.DS_Store
3-
dist/
4-
extension/bundle.js
5-
package/react-time-travel-1.0.1.tgz
3+
src/extension/build/bundles
4+
package/react-time-travel-*.tgz
65
tictactoe
7-
tictactoe/build/bundle.js
8-
tictactoe/package-lock.json
96
parents
107
coverage

src/app/components/Action.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ const Action = (props) => {
1212
role="presentation"
1313
>
1414
<div className="action-component-text">{index}</div>
15-
<div
15+
<button
1616
className="jump-button"
17-
onClick={() => handleJumpSnapshot(index)}
17+
onClick={(e) => {
18+
e.stopPropagation();
19+
handleJumpSnapshot(index);
20+
}}
1821
tabIndex={index}
19-
role="button"
22+
type="button"
2023
>
2124
Jump
22-
</div>
25+
</button>
2326
</div>
2427
);
2528
};
2629

27-
// Action.propTypes = {
28-
// selected: PropTypes.bool,
29-
// index: PropTypes.number,
30-
// };
30+
Action.propTypes = {
31+
selected: PropTypes.bool.isRequired,
32+
index: PropTypes.number.isRequired,
33+
handleChangeSnapshot: PropTypes.func.isRequired,
34+
handleJumpSnapshot: PropTypes.func.isRequired,
35+
};
3136

3237
export default Action;

src/app/components/MainSlider.jsx

Lines changed: 28 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,31 @@ const handle = (props) => {
2323
);
2424
};
2525

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

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-
}
46+
MainSlider.propTypes = {
47+
snapshotLength: PropTypes.number.isRequired,
48+
sliderIndex: PropTypes.number.isRequired,
49+
handleJumpSnapshot: PropTypes.func.isRequired,
50+
pause: PropTypes.func.isRequired,
51+
};
4852

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

src/app/components/Tree.jsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import React from 'react';
22
import JSONTree from 'react-json-tree';
33
import PropTypes from 'prop-types';
44

5-
const getItemString = (type, data, itemType, itemString) => (
6-
<span>
7-
{data.name}
8-
</span>
9-
);
5+
const getItemString = (type, data) => {
6+
// check to make sure that we are on the tree node, not anything else
7+
if (Object.keys(data).length === 3 && typeof data.state === 'object' && typeof data.name === 'string' && Array.isArray(data.children)) {
8+
return (
9+
<span>
10+
{data.name}
11+
</span>
12+
);
13+
}
14+
};
1015

1116
const Tree = (props) => {
1217
const { snapshot } = props;
@@ -19,7 +24,7 @@ const Tree = (props) => {
1924
shouldExpandNode={() => true}
2025
getItemString={getItemString}
2126
labelRenderer={(raw) => {
22-
if (typeof raw[0] !== 'number') return <span>{raw[0]}</span>;
27+
return (typeof raw[0] !== 'number') ? <span>{raw[0]}</span> : null;
2328
}}
2429
/>
2530
)}

src/app/containers/ActionContainer.jsx

Lines changed: 5 additions & 5 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}`}
@@ -28,9 +28,9 @@ const ActionContainer = ({
2828
return (
2929
<div className="action-container">
3030
<div className="action-component exclude">
31-
<div className="empty-button" onClick={emptySnapshot}>
31+
<button className="empty-button" onClick={emptySnapshot} type="button">
3232
emptySnapshot
33-
</div>
33+
</button>
3434
</div>
3535
<div>{actionsArr}</div>
3636
</div>
@@ -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/ButtonsContainer.jsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
const ButtonsContainer = ({ mode: { paused, locked, persist }, toggleMode, importSnapshots, exportSnapshots }) => (
5-
<div className="buttons-container">
6-
<div className="pause-button" onClick={() => toggleMode('paused')}>{(paused) ? 'Resume' : 'Pause'}</div>
7-
<div className="lock-button" onClick={() => toggleMode('locked')}>{(locked) ? 'Unlock' : 'Lock'}</div>
8-
<div className="persist-button" onClick={() => toggleMode('persist')}>{(persist) ? 'Unpersist' : 'Persist'}</div>
9-
<div className="import-button" onClick={importSnapshots}>Import</div>
10-
<div className="export-button" onClick={exportSnapshots}>Export</div>
11-
</div>
12-
);
4+
const ButtonsContainer = ({
5+
mode: { paused, locked, persist },
6+
toggleMode,
7+
importSnapshots,
8+
exportSnapshots,
9+
}) =>
10+
(
11+
<div className="buttons-container">
12+
<button className="pause-button" type="button" onClick={() => toggleMode('paused')}>{(paused) ? 'Resume' : 'Pause'}</button>
13+
<button className="lock-button" type="button" onClick={() => toggleMode('locked')}>{(locked) ? 'Unlock' : 'Lock'}</button>
14+
<button className="persist-button" type="button" onClick={() => toggleMode('persist')}>{(persist) ? 'Unpersist' : 'Persist'}</button>
15+
<button className="import-button" type="button" onClick={importSnapshots}>Import</button>
16+
<button className="export-button" type="button" onClick={exportSnapshots}>Export</button>
17+
</div>
18+
);
1319

1420
ButtonsContainer.propTypes = {
1521
toggleMode: PropTypes.func.isRequired,

src/app/containers/MainContainer.jsx

Lines changed: 32 additions & 31 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,16 @@ class MainContainer extends Component {
3940
const { action, payload } = message;
4041
switch (action) {
4142
case 'sendSnapshots': {
42-
const snapshotIndex = payload.length - 1;
43+
const sliderIndex = payload.length - 1;
4344
// set state with the information received from the background script
44-
this.setState({ snapshots: payload, snapshotIndex });
45+
this.setState({ snapshots: payload, sliderIndex });
4546
break;
4647
}
4748
case 'initialConnectSnapshots': {
4849
const { snapshots, mode } = payload;
49-
const snapshotIndex = snapshots.length - 1;
50-
this.setState({ snapshots, snapshotIndex, mode });
50+
const viewIndex = 0;
51+
const sliderIndex = viewIndex;
52+
this.setState({ snapshots, mode, viewIndex, sliderIndex });
5153
break;
5254
}
5355
default:
@@ -64,21 +66,21 @@ class MainContainer extends Component {
6466
}
6567

6668
moveBackward() {
67-
const { snapshots, snapshotIndex } = this.state;
69+
const { snapshots, sliderIndex } = this.state;
6870
this.pause();
69-
if (snapshots.length > 0 && snapshotIndex > 0) {
70-
const newIndex = snapshotIndex - 1;
71+
if (snapshots.length > 0 && sliderIndex > 0) {
72+
const newIndex = sliderIndex - 1;
7173
// second callback parameter of setState to invoke handleJumpSnapshot
72-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
74+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
7375
}
7476
}
7577

7678
moveForward() {
77-
const { snapshots, snapshotIndex } = this.state;
79+
const { snapshots, sliderIndex } = this.state;
7880
this.pause();
79-
if (snapshotIndex < snapshots.length - 1) {
80-
const newIndex = snapshotIndex + 1;
81-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
81+
if (sliderIndex < snapshots.length - 1) {
82+
const newIndex = sliderIndex + 1;
83+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
8284
}
8385
}
8486

@@ -88,10 +90,10 @@ class MainContainer extends Component {
8890
const { playing } = this.state;
8991
if (playing) {
9092
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));
93+
const { snapshots, sliderIndex } = this.state;
94+
if (sliderIndex < snapshots.length - 1) {
95+
const newIndex = sliderIndex + 1;
96+
this.setState({ sliderIndex: newIndex }, this.handleJumpSnapshot(newIndex));
9597
} else {
9698
// clear interval when play reaches the end
9799
globalPlaying = false;
@@ -112,22 +114,22 @@ class MainContainer extends Component {
112114

113115
emptySnapshot() {
114116
const { port, snapshots } = this.state;
115-
this.setState({ snapshots: [snapshots[0]], snapshotIndex: 0 });
117+
this.setState({ snapshots: [snapshots[0]], sliderIndex: 0, viewIndex: 0 });
116118
port.postMessage({ action: 'emptySnap' });
117119
}
118120

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

128-
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) {
129130
const { snapshots, port } = this.state;
130-
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
131+
port.postMessage({ action: 'jumpToSnap', payload: snapshots[sliderIndex] });
132+
this.setState({ sliderIndex });
131133
}
132134

133135
importSnapshots() {
@@ -190,24 +192,23 @@ class MainContainer extends Component {
190192

191193
render() {
192194
const {
193-
snapshots, snapshotIndex, mode, playing, playSpeed,
195+
snapshots, viewIndex, sliderIndex, mode, playing, playSpeed,
194196
} = this.state;
195197
return (
196198
<div className="main-container">
197199
<HeadContainer />
198200
<div className="body-container">
199201
<ActionContainer
200202
snapshots={snapshots}
201-
snapshotIndex={snapshotIndex}
203+
viewIndex={viewIndex}
202204
handleChangeSnapshot={this.handleChangeSnapshot}
203205
handleJumpSnapshot={this.handleJumpSnapshot}
204206
emptySnapshot={this.emptySnapshot}
205207
/>
206-
{(snapshots.length) ? <StateContainer snapshot={snapshots[snapshotIndex]} /> : null}
208+
{(snapshots.length) ? <StateContainer snapshot={snapshots[viewIndex]} /> : null}
207209
<TravelContainer
208210
snapshotsLength={snapshots.length}
209-
snapshotIndex={snapshotIndex}
210-
handleChangeSnapshot={this.handleChangeSnapshot}
211+
sliderIndex={sliderIndex}
211212
handleJumpSnapshot={this.handleJumpSnapshot}
212213
moveBackward={this.moveBackward}
213214
moveForward={this.moveForward}

0 commit comments

Comments
 (0)