Skip to content

Commit 7fffbf9

Browse files
committed
converted codebase into hooks
1 parent ffaab8e commit 7fffbf9

File tree

12 files changed

+282
-213
lines changed

12 files changed

+282
-213
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"build": "webpack --mode production",
77
"dev": "webpack --mode development --watch",
8-
"test": "jest --verbose --coverage --watchAll"
8+
"test": "jest --verbose --coverage --watchAll",
9+
"lint": "eslint --ext .js --ext .jsx src"
910
},
1011
"keywords": [
1112
"react",

src/app/actions/actions.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as types from '../constants/actionTypes';
2+
3+
export const togglePause = () => ({
4+
type: types.TOGGLE_MODE,
5+
payload: 'setPause',
6+
});
7+
8+
export const toggleLock = () => ({
9+
type: types.TOGGLE_MODE,
10+
payload: 'setLock',
11+
});
12+
13+
export const togglePersist = () => ({
14+
type: types.TOGGLE_MODE,
15+
payload: 'setPersist',
16+
});
17+
18+
export const addNewSnapshots = snapshots => ({
19+
type: types.NEW_SNAPSHOTS,
20+
payload: {
21+
snapshots,
22+
sliderIndex: snapshots.length - 1,
23+
},
24+
});
25+
26+
export const initialConnect = (snapshots, mode) => ({
27+
type: types.INITIAL_CONNECT,
28+
payload: {
29+
snapshots,
30+
mode,
31+
sliderIndex: 0,
32+
viewIndex: -1,
33+
},
34+
});
35+
36+
export const setPort = port => ({
37+
type: types.SET_PORT,
38+
payload: port,
39+
});
40+
41+
export const emptySnapshots = () => ({
42+
type: types.EMPTY,
43+
});
44+
45+
export const changeView = index => ({
46+
type: types.CHANGE_VIEW,
47+
payload: index,
48+
});
49+
50+
export const changeSlider = index => ({
51+
type: types.CHANGE_SLIDER,
52+
payload: index,
53+
});
54+
55+
export const moveBackward = () => ({
56+
type: types.MOVE_BACKWARD,
57+
payload: false,
58+
});
59+
60+
export const moveForward = () => ({
61+
type: types.MOVE_FORWARD,
62+
payload: false,
63+
});
64+
65+
export const playForward = () => ({
66+
type: types.MOVE_FORWARD,
67+
payload: true,
68+
});
69+
70+
export const pause = () => ({
71+
type: types.PAUSE,
72+
});
73+
74+
export const startPlaying = intervalId => ({
75+
type: types.PLAY,
76+
payload: intervalId,
77+
});
78+
79+
export const importSnapshots = newSnaps => ({
80+
type: types.IMPORT,
81+
payload: newSnaps,
82+
});

src/app/components/Action.jsx

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

4+
import { changeView, changeSlider } from '../actions/actions';
5+
46
const Action = (props) => {
57
const {
68
selected, index, sliderIndex, dispatch,
@@ -9,10 +11,7 @@ const Action = (props) => {
911
return (
1012
<div
1113
className={selected ? 'action-component selected' : 'action-component'}
12-
onClick={() => dispatch({
13-
type: 'changeView',
14-
payload: index,
15-
})}
14+
onClick={() => dispatch(changeView(index))}
1615
role="presentation"
1716
style={index > sliderIndex ? { color: '#5f6369' } : {}}
1817
>
@@ -21,10 +20,7 @@ const Action = (props) => {
2120
className="jump-button"
2221
onClick={(e) => {
2322
e.stopPropagation();
24-
dispatch({
25-
type: 'changeSlider',
26-
payload: index,
27-
});
23+
dispatch(changeSlider(index));
2824
}}
2925
tabIndex={index}
3026
type="button"

src/app/components/Chart.jsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23
import '../styles/components/_d3Tree.scss';
34
import * as d3 from 'd3';
45

@@ -8,12 +9,14 @@ let duration = 750;
89

910
class Chart extends Component {
1011
componentDidMount() {
11-
root = JSON.parse(JSON.stringify(this.props.snapshot));
12+
const { snapshot } = this.props;
13+
root = JSON.parse(JSON.stringify(snapshot));
1214
this.maked3Tree();
1315
}
1416

1517
componentDidUpdate() {
16-
root = JSON.parse(JSON.stringify(this.props.snapshot));
18+
const { snapshot } = this.props;
19+
root = JSON.parse(JSON.stringify(snapshot));
1720
this.maked3Tree();
1821
}
1922

@@ -28,8 +31,10 @@ class Chart extends Component {
2831
this.removed3Tree();
2932
duration = 0;
3033

31-
const margin = { top: 20, right: 120, bottom: 20, left: 120 };
32-
const width = 600 - margin.right - margin.left;
34+
const margin = {
35+
top: 20, right: 120, bottom: 20, left: 120,
36+
};
37+
// const width = 600 - margin.right - margin.left;
3338
const height = 600 - margin.top - margin.bottom;
3439

3540
let i = 0;
@@ -219,4 +224,10 @@ class Chart extends Component {
219224
}
220225
}
221226

227+
Chart.propTypes = {
228+
snapshot: PropTypes.arrayOf(
229+
PropTypes.object,
230+
).isRequired,
231+
};
232+
222233
export default Chart;

src/app/components/MainSlider.jsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
/* eslint-disable react/prop-types */
2+
13
import React from 'react';
24
import Slider from 'rc-slider';
35
import Tooltip from 'rc-tooltip';
46
import PropTypes from 'prop-types';
57

8+
import { changeSlider, pause } from '../actions/actions';
9+
610
const { Handle } = Slider;
711

812
const handle = (props) => {
@@ -23,24 +27,21 @@ const handle = (props) => {
2327
);
2428
};
2529

26-
const MainSlider = ({
27-
snapshotsLength,
28-
sliderIndex,
29-
dispatch,
30-
}) =>
31-
(
30+
function MainSlider({ snapshotsLength, sliderIndex, dispatch }) {
31+
return (
3232
<Slider
3333
min={0}
3434
max={snapshotsLength - 1}
3535
value={sliderIndex}
3636
onChange={(index) => {
3737
const newIndex = index === -1 ? 0 : index;
38-
dispatch({ type: 'changeSlider', payload: newIndex });
39-
dispatch({ type: 'pause' });
38+
dispatch(changeSlider(newIndex));
39+
dispatch(pause());
4040
}}
4141
handle={handle}
4242
/>
4343
);
44+
}
4445

4546
MainSlider.propTypes = {
4647
snapshotsLength: PropTypes.number.isRequired,

src/app/constants/actionTypes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const TOGGLE_MODE = 'TOGGLE_MODE';
2+
export const MOVE_BACKWARD = 'MOVE_BACKWARD';
3+
export const MOVE_FORWARD = 'MOVEFORWARD';
4+
export const IMPORT = 'IMPORT';
5+
export const EMPTY = 'EMPTY';
6+
export const CHANGE_VIEW = 'CHANGE_VIEW';
7+
export const CHANGE_SLIDER = 'CHANGE_SLIDER';
8+
export const SET_PORT = 'SET_PORT';
9+
export const PAUSE = 'PAUSE';
10+
export const PLAY = 'PLAY';
11+
export const INITIAL_CONNECT = 'INITIAL_CONNECT';
12+
export const NEW_SNAPSHOTS = 'NEW_SNAPSHOTS';

src/app/containers/ActionContainer.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import Action from '../components/Action';
44

5+
import { emptySnapshots } from '../actions/actions';
6+
57
const ActionContainer = ({
68
snapshots,
79
sliderIndex,
@@ -26,7 +28,7 @@ const ActionContainer = ({
2628
return (
2729
<div className="action-container">
2830
<div className="action-component exclude">
29-
<button className="empty-button" onClick={() => dispatch({ type: 'empty' })} type="button">
31+
<button className="empty-button" onClick={() => dispatch(emptySnapshots())} type="button">
3032
emptySnapshot
3133
</button>
3234
</div>

src/app/containers/ButtonsContainer.jsx

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

4+
import {
5+
importSnapshots, toggleLock, togglePause, togglePersist,
6+
} from '../actions/actions';
47

5-
function exportSnapshots(snapshots) {
8+
function exportHandler(snapshots) {
69
// create invisible download anchor link
710
const fileDownload = document.createElement('a');
811

@@ -19,15 +22,13 @@ function exportSnapshots(snapshots) {
1922
URL.revokeObjectURL(fileDownload.href);
2023
}
2124

22-
function importSnapshots(dispatch) {
25+
function importHandler(dispatch) {
2326
const fileUpload = document.createElement('input');
2427
fileUpload.setAttribute('type', 'file');
2528

2629
fileUpload.onchange = (event) => {
2730
const reader = new FileReader();
28-
reader.onload = () => {
29-
dispatch({ type: 'import', payload: JSON.parse(reader.result) });
30-
};
31+
reader.onload = () => dispatch(importSnapshots(JSON.parse(reader.result)));
3132
reader.readAsText(event.target.files[0]);
3233
};
3334

@@ -41,19 +42,19 @@ const ButtonsContainer = ({
4142
}) =>
4243
(
4344
<div className="buttons-container">
44-
<button className="pause-button" type="button" onClick={() => dispatch({ type: 'toggleMode', payload: 'paused' })}>
45+
<button className="pause-button" type="button" onClick={() => dispatch(togglePause())}>
4546
{paused ? 'Resume' : 'Pause'}
4647
</button>
47-
<button className="lock-button" type="button" onClick={() => dispatch({ type: 'toggleMode', payload: 'locked' })}>
48+
<button className="lock-button" type="button" onClick={() => dispatch(toggleLock())}>
4849
{locked ? 'Unlock' : 'Lock'}
4950
</button>
50-
<button className="persist-button" type="button" onClick={() => dispatch({ type: 'toggleMode', payload: 'persist' })}>
51+
<button className="persist-button" type="button" onClick={() => dispatch(togglePersist())}>
5152
{persist ? 'Unpersist' : 'Persist'}
5253
</button>
53-
<button className="export-button" type="button" onClick={() => exportSnapshots(snapshots)}>
54+
<button className="export-button" type="button" onClick={() => exportHandler(snapshots)}>
5455
Export
5556
</button>
56-
<button className="import-button" type="button" onClick={() => importSnapshots(dispatch)}>
57+
<button className="import-button" type="button" onClick={() => importHandler(dispatch)}>
5758
Import
5859
</button>
5960
</div>

0 commit comments

Comments
 (0)