Skip to content

Commit 1e74e4c

Browse files
committed
TravelContainer complete
1 parent 34a53b3 commit 1e74e4c

File tree

2 files changed

+127
-12
lines changed

2 files changed

+127
-12
lines changed

src/app/RTKslices.tsx

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export const mainSlice = createSlice({
192192
console.log('SET CURRENT TAB IN APP');
193193
state.currentTabInApp = action.payload;
194194
},
195-
pause: (state, action) => {
195+
pause: (state) => {
196196
console.log('pause: ', current(state));
197197

198198
const {tabs, currentTab} = state
@@ -202,7 +202,108 @@ export const mainSlice = createSlice({
202202
tabs[currentTab].playing = false;
203203
tabs[currentTab].intervalId = null;
204204
},
205-
205+
playForward: (state, action) => {
206+
const {port, tabs, currentTab} = state
207+
const { hierarchy, snapshots, sliderIndex, intervalId } = tabs[currentTab] || {};
208+
209+
if (sliderIndex < snapshots.length - 1) {
210+
const newIndex = sliderIndex + 1;
211+
// eslint-disable-next-line max-len
212+
// finds the name by the newIndex parsing through the hierarchy to send to background.js the current name in the jump action
213+
const nameFromIndex = findName(newIndex, hierarchy);
214+
215+
port.postMessage({
216+
action: 'jumpToSnap',
217+
payload: snapshots[newIndex],
218+
index: newIndex,
219+
name: nameFromIndex,
220+
tabId: currentTab,
221+
});
222+
223+
tabs[currentTab].sliderIndex = newIndex;
224+
225+
// message is coming from the user
226+
if (!action.payload) {
227+
clearInterval(intervalId);
228+
tabs[currentTab].playing = false;
229+
}
230+
}
231+
},
232+
startPlaying : (state, action) => {
233+
const {tabs, currentTab} = state
234+
235+
tabs[currentTab].playing = true;
236+
tabs[currentTab].intervalId = action.payload;
237+
},
238+
moveForward: (state, action) => {
239+
const {port, tabs, currentTab} = state
240+
const { hierarchy, snapshots, sliderIndex, intervalId } = tabs[currentTab] || {};
241+
242+
if (sliderIndex < snapshots.length - 1) {
243+
const newIndex = sliderIndex + 1;
244+
// eslint-disable-next-line max-len
245+
// finds the name by the newIndex parsing through the hierarchy to send to background.js the current name in the jump action
246+
const nameFromIndex = findName(newIndex, hierarchy);
247+
248+
port.postMessage({
249+
action: 'jumpToSnap',
250+
payload: snapshots[newIndex],
251+
index: newIndex,
252+
name: nameFromIndex,
253+
tabId: currentTab,
254+
});
255+
256+
tabs[currentTab].sliderIndex = newIndex;
257+
258+
// message is coming from the user
259+
if (!action.payload) {
260+
clearInterval(intervalId);
261+
tabs[currentTab].playing = false;
262+
}
263+
}
264+
},
265+
moveBackward : (state, action) => {
266+
const {port, tabs, currentTab} = state
267+
const { hierarchy, snapshots, sliderIndex, intervalId } = tabs[currentTab] || {};
268+
269+
if (sliderIndex > 0) {
270+
const newIndex = sliderIndex - 1;
271+
// eslint-disable-next-line max-len
272+
// finds the name by the newIndex parsing through the hierarchy to send to background.js the current name in the jump action
273+
const nameFromIndex = findName(newIndex, hierarchy);
274+
275+
port.postMessage({
276+
action: 'jumpToSnap',
277+
payload: snapshots[newIndex],
278+
index: newIndex,
279+
name: nameFromIndex,
280+
tabId: currentTab,
281+
newProp: 'newPropFromReducer',
282+
});
283+
clearInterval(intervalId);
284+
285+
tabs[currentTab].sliderIndex = newIndex;
286+
tabs[currentTab].playing = false;
287+
}
288+
},
289+
290+
resetSlider: (state) => {
291+
const {port, tabs, currentTab} = state
292+
const { snapshots, sliderIndex} = tabs[currentTab] || {};
293+
294+
// eslint-disable-next-line max-len
295+
// resets name to 0 to send to background.js the current name in the jump action
296+
port.postMessage({
297+
action: 'jumpToSnap',
298+
index: 0,
299+
name: 0,
300+
payload: snapshots[0],
301+
tabId: currentTab,
302+
});
303+
tabs[currentTab].sliderIndex = 0;
304+
}
305+
306+
206307
},
207308
})
208309

@@ -219,6 +320,11 @@ export const {
219320
changeSlider,
220321
setCurrentTabInApp,
221322
pause,
323+
playForward,
324+
startPlaying,
325+
moveForward,
326+
moveBackward,
327+
resetSlider
222328
} = mainSlice.actions
223329

224330

src/app/containers/TravelContainer.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import {
99
moveForward,
1010
moveBackward,
1111
resetSlider,
12-
} from '../actions/actions';
13-
import { useStoreContext } from '../store';
12+
} from '../RTKslices';
13+
import { useDispatch, useSelector } from 'react-redux';
14+
// import { useStoreContext } from '../store';
1415
import { TravelContainerProps } from '../FrontendTypes';
1516
import { Button } from '@mui/material';
1617
import FastRewindIcon from '@mui/icons-material/FastRewind';
1718
import FastForwardIcon from '@mui/icons-material/FastForward';
1819

20+
//Pause, plsayForward, startPlaying, moveForward, moveBackward
21+
1922
/*
2023
This container renders the time-travel play button, seek bar, playback controls, and the playback speed dropdown, located towards the bottom of the application, above the locked, download, upload, and tutorial buttons
2124
*/
@@ -29,14 +32,15 @@ const speeds: {
2932
{ value: 1000, label: '1.0x' },
3033
{ value: 500, label: '2.0x' },
3134
];
32-
35+
//NOTE HERE REMOVED THE DISPATCH FUNCTION IN THE TYPE SCRIPT:
36+
//USING THE BUILT IN DISPATCH
3337
function play( // function that will start/pause slider movement
3438
speed: number,
3539
playing: boolean,
36-
dispatch: (a: any) => void,
3740
snapshotsLength: number,
3841
sliderIndex: number,
3942
): void {
43+
const dispatch = useDispatch();
4044
if (playing) { // if already playing, clicking the button will pause the slider
4145
dispatch(pause());
4246
} else {
@@ -47,7 +51,7 @@ function play( // function that will start/pause slider movement
4751
}
4852
const intervalId: NodeJS.Timeout = setInterval(() => { // sets interval period to wait before advancing 'currentIndex'/slider
4953
if (currentIndex < snapshotsLength - 1) { // as long as we're not the last snapshot, increment slider up through our dispatch and increment index
50-
dispatch(playForward());
54+
dispatch(playForward(true));
5155
currentIndex += 1;
5256
} else {
5357
dispatch(pause()); // pause the slider when we reach the end
@@ -60,8 +64,12 @@ function play( // function that will start/pause slider movement
6064
function TravelContainer(props: TravelContainerProps): JSX.Element {
6165
const { snapshotsLength } = props;
6266
const [selectedSpeed, setSpeed] = useState(speeds[1]); // create a new local state selectedSpeed and set it to the second element of the 'speeds' array (1.0x speed)
63-
const [{ tabs, currentTab }, dispatch] = useStoreContext(); // we destructure the returned context object from the invocation of the useStoreContext function. Properties not found on the initialState object (dispatch) are from the useReducer function invocation in the App component
64-
const { sliderIndex, playing } = tabs[currentTab]; // we destructure the currentTab object
67+
68+
// const [{ tabs, currentTab }, dispatch] = useStoreContext(); // we destructure the returned context object from the invocation of the useStoreContext function. Properties not found on the initialState object (dispatch) are from the useReducer function invocation in the App component
69+
// const { sliderIndex, playing } = tabs[currentTab]; // we destructure the currentTab object
70+
const dispatch = useDispatch();
71+
const { tabs, currentTab } = useSelector((state: any) => state.main);
72+
const { sliderIndex, playing } = tabs[currentTab];
6573

6674
return (
6775
<div className='travel-container'>
@@ -72,15 +80,16 @@ function TravelContainer(props: TravelContainerProps): JSX.Element {
7280
type='button'
7381
// data-testid, prop for testing in RTL
7482
data-testid='play-button-test'
75-
onClick={() => play(selectedSpeed.value, playing, dispatch, snapshotsLength, sliderIndex)}
83+
//REMOVED DISPATCH FROM PLAY
84+
onClick={() => play(selectedSpeed.value, playing, snapshotsLength, sliderIndex)}
7685
>
7786
{playing ? 'Pause' : 'Play'}
7887
</Button>
7988
<MainSlider snapshotsLength={snapshotsLength} />
80-
<Button variant="contained" className='backward-button' onClick={() => dispatch(moveBackward())} type='button' sx={{height: 25, minWidth: 30, p: 0, mr: 1}}>
89+
<Button variant="contained" className='backward-button' onClick={() => dispatch(moveBackward(false))} type='button' sx={{height: 25, minWidth: 30, p: 0, mr: 1}}>
8190
<FastRewindIcon sx={{color: '#000'}}/>
8291
</Button>
83-
<Button variant="contained" className='forward-button' onClick={() => dispatch(moveForward())} type='button' sx={{height: 25, minWidth: 30, p: 0}}>
92+
<Button variant="contained" className='forward-button' onClick={() => dispatch(moveForward(false))} type='button' sx={{height: 25, minWidth: 30, p: 0}}>
8493
<FastForwardIcon sx={{color: '#000'}}/>
8594
</Button>
8695
<Dropdown speeds={speeds} selectedSpeed={selectedSpeed} setSpeed={setSpeed} />

0 commit comments

Comments
 (0)