-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (144 loc) · 4.29 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
function App() {
const [displayTime, setDisplayTime] = React.useState(25 * 60);
const [breakTime, setBreakTime] = React.useState(5 * 60);
const [sessionTime, setSessionTime] = React.useState(25 * 60);
const [timerOn, setTimerOn] = React.useState(false);
const [onBreak, setOnBreak] = React.useState(false);
const [breakAudio, setBreakAudio] = React.useState(
new Audio("./sound/beep-01a.mp3")
);
const playBreakSound = () => {
breakAudio.currentTime = 0;
breakAudio.play();
};
function formatTime(time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
return (
(minutes < 10 ? "0" + minutes : minutes) +
":" +
(seconds < 10 ? "0" + seconds : seconds)
);
}
const changeTime = (amount, type) => {
if (type == "break") {
if (breakTime < 60 && amount < 0) {
return;
}
setBreakTime((prev) => prev + amount);
} else {
if (sessionTime < 60 && amount < 0) {
return;
}
setSessionTime((prev) => prev + amount);
if (!timerOn) {
setDisplayTime(sessionTime + amount);
}
}
};
const controlTime = () => {
let second = 1000;
let date = new Date().getTime();
let nextDate = new Date().getTime() + second;
let onBreakVariable = onBreak;
if (!timerOn) {
let interval = setInterval(() => {
date = new Date().getTime();
if (date > nextDate) {
setDisplayTime((prev) => {
if (prev <= 0 && !onBreakVariable) {
playBreakSound();
onBreakVariable = true;
setOnBreak(true);
return breakTime;
} else if (prev <= 0 && onBreakVariable) {
playBreakSound();
onBreakVariable = false;
setOnBreak(false);
return sessionTime;
}
return prev - 1;
});
nextDate += second;
}
}, 30);
localStorage.clear();
localStorage.setItem("interval-id", interval);
}
if (timerOn) {
clearInterval(localStorage.getItem("interval-id"));
}
setTimerOn(!timerOn);
};
const resetTime = () => {
setDisplayTime(25 * 60);
setBreakTime(5 * 60);
setSessionTime(25 * 60);
};
return (
<div className="center-align displayTime">
<h1>25 + 5 Clock / Pomodoro Clock</h1>
<div className="dual-container">
<Length
title={"break length"}
changeTime={changeTime}
type={"break"}
time={breakTime}
formatTime={formatTime}
/>
<Length
title={"session length"}
changeTime={changeTime}
type={"session"}
time={sessionTime}
// the error fixed by removing comma arround the formatTime inside bracket.
formatTime={formatTime}
/>
</div>
<div className=" center-align">
<h3>{onBreak ? "Break" : "Session"} </h3>
<h1> {formatTime(displayTime)} </h1>
<button
className="btn-medium deep-purple lighten-2"
onClick={controlTime}
>
{timerOn ? (
<i className="material-icons">pause_circle_filled</i>
) : (
<i className="material-icons">play_circle_filled</i>
)}
</button>
<button
className="btn-medium deep-purple lighten-2"
onClick={resetTime}
>
<i className="material-icons">autorenew</i>
</button>
</div>
</div>
);
}
function Length({ title, changeTime, type, time, formatTime }) {
return (
<div>
<h3>{title}</h3>
<div className="time-sets">
<button
onClick={() => changeTime(-60, type)}
className="btn-small deep-purple lighten-2"
>
<i className="material-icons">arrow_downward</i>
</button>
{/* this shows an error "formatTime()" is not a funtion but infact that is already a function, so now I am working on that why it shows such error */}
<h3> {formatTime(time)} </h3>
<button
className="btn-small deep-purple lighten-2"
onClick={() => changeTime(60, type)}
>
<i className="material-icons">arrow_upward</i>
</button>
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));