Skip to content

Commit 2e18ae9

Browse files
authored
Merge pull request #61 from oslabs-beta/rydang/hooks
hooks and context
2 parents 5700fce + b6b2ff5 commit 2e18ae9

File tree

17 files changed

+587
-514
lines changed

17 files changed

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

src/app/components/Action.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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 {
6-
selected, handleChangeSnapshot, handleJumpSnapshot, index, sliderIndex,
8+
selected, index, sliderIndex, dispatch,
79
} = props;
810

911
return (
1012
<div
1113
className={selected ? 'action-component selected' : 'action-component'}
12-
onClick={() => handleChangeSnapshot(index)}
14+
onClick={() => dispatch(changeView(index))}
1315
role="presentation"
1416
style={index > sliderIndex ? { color: '#5f6369' } : {}}
1517
>
@@ -18,7 +20,7 @@ const Action = (props) => {
1820
className="jump-button"
1921
onClick={(e) => {
2022
e.stopPropagation();
21-
handleJumpSnapshot(index);
23+
dispatch(changeSlider(index));
2224
}}
2325
tabIndex={index}
2426
type="button"
@@ -30,10 +32,10 @@ const Action = (props) => {
3032
};
3133

3234
Action.propTypes = {
35+
sliderIndex: PropTypes.number.isRequired,
3336
selected: PropTypes.bool.isRequired,
3437
index: PropTypes.number.isRequired,
35-
handleChangeSnapshot: PropTypes.func.isRequired,
36-
handleJumpSnapshot: PropTypes.func.isRequired,
38+
dispatch: PropTypes.func.isRequired,
3739
};
3840

3941
export default Action;

src/app/components/App.jsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import React from 'react';
1+
import React, { useReducer } from 'react';
22
import MainContainer from '../containers/MainContainer';
3+
import { StoreContext } from '../store';
4+
import mainReducer from '../reducers/mainReducer';
35

4-
const App = () => <MainContainer />;
6+
const initialState = {
7+
port: null,
8+
sliderIndex: 0,
9+
viewIndex: -1,
10+
intervalId: null,
11+
playing: false,
12+
snapshots: [],
13+
mode: {
14+
locked: false,
15+
paused: false,
16+
persist: false,
17+
},
18+
};
19+
20+
function App() {
21+
return (
22+
<StoreContext.Provider value={useReducer(mainReducer, initialState)}>
23+
<MainContainer />
24+
</StoreContext.Provider>
25+
);
26+
}
527

628
export default App;

0 commit comments

Comments
 (0)