Skip to content

Commit bae4e24

Browse files
committed
simplified to only track time remaining rather than complex date calculations
1 parent d19acd5 commit bae4e24

File tree

7 files changed

+56
-52
lines changed

7 files changed

+56
-52
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@types/react": "^17.0.3",
1717
"@types/react-dom": "^17.0.0",
1818
"concurrently": "^6.0.0",
19+
"dayjs": "^1.10.4",
1920
"electron-is-dev": "^2.0.0",
2021
"mathjs": "^9.3.0",
2122
"moment": "^2.29.1",

src/Components/TimeInput.jsx

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ const defaultTimerDuration = "15";
55

66
const TimeInput = () => {
77
const [value, setValue] = useState(defaultTimerDuration)
8-
9-
const handleChange = (e) => {
10-
setValue(e.target.value)
11-
}
128

13-
const handleClick = (e) => {
14-
this.setState({ value: ""})
9+
function getDurationInMs(){
10+
if(value.endsWith("m")){
11+
return value.replace("m", "") * 60000;
12+
}
13+
else{
14+
return value * 1000;
15+
}
1516
}
17+
1618
return (
1719
<div>
1820
<form>
1921
<input type="text" value={value}
2022
onChange={(e) => { setValue(e.target.value) }}
2123
onClick={(e) => { setValue(e.target.value) }}/>
2224
</form>
23-
<Timer duration={value}></Timer>
25+
<Timer duration={getDurationInMs()}></Timer>
2426
</div>
2527
)
2628

src/Components/Timer.jsx

+29-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from "react";
22
import './styles.css';
3-
import Moment from 'react-moment';
4-
import moment from 'moment';
3+
import { msToTimeFormatted } from '../Helper';
54
import { evaluate } from 'mathjs';
5+
import dayjs from 'dayjs';
66

77
export default class Timer extends React.Component {
88
state = {
@@ -13,36 +13,47 @@ export default class Timer extends React.Component {
1313
soundPlaying: false
1414
}
1515

16+
tickRate = 100;
1617
audio = new Audio('bensound-creativeminds.mp3')
1718

1819
startTimer = () => {
1920
this.audio.pause();
20-
var start = new Date();
2121
this.setState({
2222
running: true,
23-
start: start,
24-
end: moment(start).add(evaluate(this.props.duration), 's').toDate()
23+
remainingTime: this.props.duration
2524
})
2625
}
2726

2827
stopTimer = () => {
29-
//this.audio.play();
3028
this.setState({
3129
running: false,
30+
remainingTime: 0
3231
})
3332
}
3433

35-
tick = () => {
36-
var now = moment(new Date())
37-
this.setState({
38-
remainingTime: moment(this.state.end).diff(now)
34+
pauseTimer = () => {
35+
this.setState({
36+
running: false,
3937
})
40-
if (this.state.running && this.state.remainingTime <= 0) {
41-
this.stopTimer();
42-
window.confirm("Timer is finished", this.audio.play());
43-
}
4438
}
4539

40+
tick = () => {
41+
if(this.state.running){
42+
this.setState({
43+
remainingTime: this.state.remainingTime - this.tickRate
44+
});
45+
if (this.state.remainingTime <= 0) {
46+
this.stopTimer();
47+
if(window.confirm("Timer is finished", this.audio.play()))
48+
{
49+
this.audio.pause();
50+
}
51+
}
52+
}
53+
}
54+
55+
56+
4657
pauseAudio = () => {
4758
this.audio.pause();
4859
}
@@ -65,10 +76,10 @@ export default class Timer extends React.Component {
6576
}
6677
return (
6778
<div>
68-
6979
<button onClick={this.startTimer} className="button" >Start</button> <button onClick={this.stopTimer} className="button">Stop</button>
70-
<p>{this.state.running ? <Moment durationFromNow filter={toPositiveFilter}>{this.state.end}</Moment> : null}</p>
80+
<p onClick={this.pauseTimer}>{ msToTimeFormatted(this.state.remainingTime) }</p>
7181
</div>
7282
)
73-
}
74-
}
83+
}
84+
}
85+

src/Helper.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function msToTimeFormatted(duration){
2+
var milliseconds = Math.floor((duration % 1000) / 100),
3+
seconds = Math.floor((duration / 1000) % 60),
4+
minutes = Math.floor((duration / (1000 * 60)) % 60),
5+
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
6+
7+
hours = (hours < 10) ? "0" + hours : hours;
8+
minutes = (minutes < 10) ? "0" + minutes : minutes;
9+
seconds = (seconds < 10) ? "0" + seconds : seconds;
10+
11+
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
12+
}

src/react-app-env.d.ts

-1
This file was deleted.

tsconfig.json

-26
This file was deleted.

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -4244,6 +4244,11 @@ date-fns@^2.16.1:
42444244
resolved "https://registry.npmjs.org/date-fns/-/date-fns-2.19.0.tgz"
42454245
integrity sha512-X3bf2iTPgCAQp9wvjOQytnf5vO5rESYRXlPIVcgSbtT5OTScPcsf9eZU+B/YIkKAtYr5WeCii58BgATrNitlWg==
42464246

4247+
dayjs@^1.10.4:
4248+
version "1.10.4"
4249+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.10.4.tgz#8e544a9b8683f61783f570980a8a80eaf54ab1e2"
4250+
integrity sha512-RI/Hh4kqRc1UKLOAf/T5zdMMX5DQIlDxwUe3wSyMMnEbGunnpENCdbUgM+dW7kXidZqCttBrmw7BhN4TMddkCw==
4251+
42474252
[email protected], debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9:
42484253
version "2.6.9"
42494254
resolved "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz"

0 commit comments

Comments
 (0)