-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.jsx
More file actions
68 lines (66 loc) · 3.02 KB
/
Copy pathTimer.jsx
File metadata and controls
68 lines (66 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {React,useState,useEffect} from 'react'
import {Box,Typography,IconButton} from '@material-ui/core'
import PauseIcon from '@material-ui/icons/Pause';
import PlayArrowIcon from '@material-ui/icons/PlayArrow';
import {makeStyles} from '@material-ui/core/styles';
import { useSelector, useDispatch } from 'react-redux';
import {incrementTimer,disableDispatch} from '../redux/slicers/timer';//import REDUX THUNK FUNCTION
const useStyles=makeStyles((theme)=>({
timeBox:{
display:'flex',
flexDirection:'row',
justifyContent:'space-around',
borderStyle:'solid',
borderColor:theme.palette.primary.main,
borderWidth:2
},
iconBox:{
display:'flex',
flexDirection:'row',
justifyContent:'center'
}
}))
export default function Timer() {
const classes=useStyles();
const [paused,setPaused]=useState(true);
//WE INTRODUCE THIS STATE VARIABLE TO FIX THE BUG IN WHICH MULTIPLE CLICKS ON PLAY ICON IN <1s MAKE MULTIPLE DISPATCHES THUS INCREMENTS OF TIMER INSTEAD OF JUST ONE
const dispatchEnable=useSelector(state=>state.timer.dispatch);
const time=useSelector(state=>state.timer);
const dispatch = useDispatch();
useEffect(()=>{
if(!paused)
{
dispatch(incrementTimer());//ALWAYS INCREMENT TIMER WHEN PAUSE=false
}
},[time.ticks]);
useEffect(()=>{
if(!paused)
{
if(dispatchEnable)//IF TRUE-> TIMER CAN BE INCREMENTED,ELSE TIMER CANT BE INCREMENTED-> 1 SECOND HASNT GONE YET
{
dispatch(incrementTimer());//INCREMENT TIMER
}
}
},[paused]);
const handlePauseChange=(event)=>{
if(!paused)//IF PAUSED=FALSE BEFORE CLICK-> WE WANT TO STOP TIMER WITH CLICK-> dispatch=false-> DONT INCREMENT COUNTER-> DISABLE DISPATCHING
{
dispatch(disableDispatch());//SET IT TO FALSE ONLY HERE
}
setPaused(!paused);//ALWAYS DO THIS AFTER CLICK
}
return (
<Box>
<Box className={classes.timeBox}>
<Box style={{width:'40%',display:'flex',flexDirection:'row', justifyContent:'center'}}><Typography color='primary' variant='h3'>{time.minutes}</Typography></Box>
<Box style={{width:'10%',display:'flex',flexDirection:'row', justifyContent:'center'}}><Typography color='primary' variant='h3'>:</Typography></Box>
<Box style={{width:'40%',display:'flex',flexDirection:'row', justifyContent:'center'}}><Typography color='primary' variant='h3'>{time.seconds}</Typography></Box>
</Box>
<Box className={classes.iconBox}> { (paused) ?
<IconButton title='Start' onClick={handlePauseChange} disableRipple style={{height:'100%'}} color='secondary'><PlayArrowIcon style={{width:50,height:'auto'}}/></IconButton>
:
<IconButton title='Stop' onClick={handlePauseChange} disableRipple style={{height:'100%'}} color='secondary'><PauseIcon style={{width:50,height:'auto'}}/></IconButton>
} </Box>
</Box>
)
}