Skip to content

Commit 9832211

Browse files
committed
started context and fixed bug in buttons not reflectin state
1 parent 7fffbf9 commit 9832211

File tree

5 files changed

+56
-50
lines changed

5 files changed

+56
-50
lines changed

src/app/actions/actions.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import * as types from '../constants/actionTypes';
22

3-
export const togglePause = () => ({
3+
export const toggleMode = mode => ({
44
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',
5+
payload: mode,
166
});
177

188
export const addNewSnapshots = snapshots => ({

src/app/containers/ButtonsContainer.jsx

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

44
import {
5-
importSnapshots, toggleLock, togglePause, togglePersist,
5+
importSnapshots, toggleMode,
66
} from '../actions/actions';
7+
import StoreContext from '../store';
78

89
function exportHandler(snapshots) {
910
// create invisible download anchor link
@@ -35,20 +36,18 @@ function importHandler(dispatch) {
3536
fileUpload.click();
3637
}
3738

38-
const ButtonsContainer = ({
39-
mode: { paused, locked, persist },
40-
dispatch,
41-
snapshots,
42-
}) =>
43-
(
39+
function ButtonsContainer(props) {
40+
const { snapshots, mode: { paused, locked, persist }, dispatch } = props;
41+
// const [{ snapshots, mode: { paused, locked, persist } }, dispatch] = useContext(StoreContext);
42+
return (
4443
<div className="buttons-container">
45-
<button className="pause-button" type="button" onClick={() => dispatch(togglePause())}>
44+
<button className="pause-button" type="button" onClick={() => dispatch(toggleMode('paused'))}>
4645
{paused ? 'Resume' : 'Pause'}
4746
</button>
48-
<button className="lock-button" type="button" onClick={() => dispatch(toggleLock())}>
47+
<button className="lock-button" type="button" onClick={() => dispatch(toggleMode('locked'))}>
4948
{locked ? 'Unlock' : 'Lock'}
5049
</button>
51-
<button className="persist-button" type="button" onClick={() => dispatch(togglePersist())}>
50+
<button className="persist-button" type="button" onClick={() => dispatch(toggleMode('persist'))}>
5251
{persist ? 'Unpersist' : 'Persist'}
5352
</button>
5453
<button className="export-button" type="button" onClick={() => exportHandler(snapshots)}>
@@ -59,6 +58,7 @@ const ButtonsContainer = ({
5958
</button>
6059
</div>
6160
);
61+
}
6262

6363
ButtonsContainer.propTypes = {
6464
dispatch: PropTypes.func.isRequired,

src/app/containers/MainContainer.jsx

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

88
import mainReducer from '../reducers/mainReducer';
99
import { addNewSnapshots, initialConnect, setPort } from '../actions/actions';
10+
import StoreContext from '../store';
1011

1112
const initialState = {
1213
port: null,
@@ -59,7 +60,6 @@ function MainContainer() {
5960
});
6061

6162
const {
62-
port,
6363
snapshots,
6464
sliderIndex,
6565
viewIndex,
@@ -69,33 +69,33 @@ function MainContainer() {
6969

7070
// if viewIndex is -1, then use the sliderIndex instead
7171
const snapshotView = (viewIndex === -1) ? snapshots[sliderIndex] : snapshots[viewIndex];
72-
7372
return (
74-
(port) ? (
75-
<div className="main-container">
76-
<HeadContainer />
77-
<div className="body-container">
78-
<ActionContainer
79-
snapshots={snapshots}
80-
sliderIndex={sliderIndex}
81-
viewIndex={viewIndex}
82-
dispatch={dispatch}
83-
/>
84-
{(snapshots.length) ? <StateContainer snapshot={snapshotView} /> : null}
85-
<TravelContainer
86-
snapshotsLength={snapshots.length}
87-
sliderIndex={sliderIndex}
88-
playing={playing}
89-
dispatch={dispatch}
90-
/>
91-
<ButtonsContainer
92-
mode={mode}
93-
dispatch={dispatch}
94-
snapshots={mainState.snapshots}
95-
/>
96-
</div>
73+
// <StoreContext.Provider value={[mainState, dispatch]}>
74+
<div className="main-container">
75+
<HeadContainer />
76+
<div className="body-container">
77+
<ActionContainer
78+
snapshots={snapshots}
79+
sliderIndex={sliderIndex}
80+
viewIndex={viewIndex}
81+
dispatch={dispatch}
82+
/>
83+
{(snapshots.length) ? <StateContainer snapshot={snapshotView} /> : null}
84+
<TravelContainer
85+
snapshotsLength={snapshots.length}
86+
sliderIndex={sliderIndex}
87+
playing={playing}
88+
dispatch={dispatch}
89+
/>
90+
<ButtonsContainer
91+
mode={mode}
92+
dispatch={dispatch}
93+
snapshots={mainState.snapshots}
94+
/>
9795
</div>
98-
) : <div>no existing react-time-travel page</div>);
96+
</div>
97+
// </StoreContext.Provider>
98+
);
9999
}
100100

101101
export default MainContainer;

src/app/reducers/mainReducer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ export default function mainReducer(state, action) {
7272
case types.TOGGLE_MODE: {
7373
mode[action.payload] = !mode[action.payload];
7474
const newMode = mode[action.payload];
75-
port.postMessage({ action: action.payload, payload: newMode });
75+
let actionText;
76+
switch (action.payload) {
77+
case 'paused':
78+
actionText = 'setPause';
79+
break;
80+
case 'locked':
81+
actionText = 'setLock';
82+
break;
83+
case 'persist':
84+
actionText = 'setPersist';
85+
break;
86+
default:
87+
}
88+
port.postMessage({ action: actionText, payload: newMode });
7689
return { ...state, mode };
7790
}
7891
case types.PAUSE: {

src/app/store.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
export default React.createContext();

0 commit comments

Comments
 (0)