Skip to content

Commit d827321

Browse files
committed
lint useTimer
1 parent cf7e9ec commit d827321

File tree

1 file changed

+87
-82
lines changed

1 file changed

+87
-82
lines changed

src/useTimer.js

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,133 @@
11
import { useState, useEffect, useRef } from 'react';
22

3-
/* ---------------------- useTimer --------------------- */
3+
4+
// Validate expiryTimestamp
5+
function isValidExpiryTimestamp(expiryTimestamp) {
6+
const isValid = (new Date(expiryTimestamp)).getTime() > 0;
7+
if (!isValid) {
8+
console.warn('react-timer-hook: { useTimer } Invalid expiryTimestamp settings', expiryTimestamp);
9+
}
10+
return isValid;
11+
}
12+
13+
// Validate onExpire
14+
function isValidOnExpire(onExpire) {
15+
const isValid = onExpire && typeof onExpire === 'function';
16+
if (onExpire && !isValid) {
17+
console.warn('react-timer-hook: { useTimer } Invalid onExpire settings function', onExpire);
18+
}
19+
return isValid;
20+
}
421

522
export default function useTimer(settings) {
623
const { expiryTimestamp: expiry, onExpire } = settings || {};
724
const [expiryTimestamp, setExpiryTimestamp] = useState(expiry);
825

926
const [seconds, setSeconds] = useState(0);
10-
function subtractSecond() {
11-
setSeconds(prevSeconds => {
12-
if(prevSeconds === 0) {
13-
subtractMinute();
14-
return 59;
15-
} else if(prevSeconds > 0) {
16-
return prevSeconds - 1;
27+
const [minutes, setMinutes] = useState(0);
28+
const [hours, setHours] = useState(0);
29+
const [days, setDays] = useState(0);
30+
const intervalRef = useRef();
31+
32+
function reset() {
33+
if (intervalRef.current) {
34+
clearInterval(intervalRef.current);
35+
intervalRef.current = undefined;
36+
}
37+
setSeconds(0);
38+
setMinutes(0);
39+
setHours(0);
40+
setDays(0);
41+
}
42+
43+
function subtractDay() {
44+
setDays((prevDays) => {
45+
if (prevDays > 0) {
46+
return prevDays - 1;
1747
}
48+
reset();
49+
isValidOnExpire(onExpire) && onExpire();
1850
return 0;
1951
});
2052
}
2153

54+
function subtractHour() {
55+
setHours((prevHours) => {
56+
if (prevHours === 0) {
57+
subtractDay();
58+
return 23;
59+
}
60+
61+
if (prevHours > 0) {
62+
return prevHours - 1;
63+
}
64+
return 0;
65+
});
66+
}
2267

23-
const [minutes, setMinutes] = useState(0);
2468
function subtractMinute() {
25-
setMinutes(prevMinutes => {
69+
setMinutes((prevMinutes) => {
2670
if (prevMinutes === 0) {
2771
subtractHour();
2872
return 59;
29-
} else if(prevMinutes > 0) {
73+
}
74+
75+
if (prevMinutes > 0) {
3076
return prevMinutes - 1;
3177
}
3278
return 0;
3379
});
3480
}
3581

36-
const [hours, setHours] = useState(0);
37-
function subtractHour() {
38-
setHours(prevHours => {
39-
if (prevHours === 0) {
40-
subtractDay();
41-
return 23;
42-
} else if(prevHours > 0) {
43-
return prevHours - 1;
82+
function subtractSecond() {
83+
setSeconds((prevSeconds) => {
84+
if (prevSeconds === 0) {
85+
subtractMinute();
86+
return 59;
87+
}
88+
89+
if (prevSeconds > 0) {
90+
return prevSeconds - 1;
4491
}
4592
return 0;
4693
});
4794
}
4895

49-
const [days, setDays] = useState(0);
50-
function subtractDay() {
51-
setDays(prevDays => {
52-
if(prevDays > 0) {
53-
return prevDays - 1;
54-
}
96+
// Timer expiry date calculation
97+
function calculateExpiryDate() {
98+
const now = new Date().getTime();
99+
const distance = expiryTimestamp - now;
100+
const daysValue = Math.floor(distance / (1000 * 60 * 60 * 24));
101+
const hoursValue = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
102+
const minutesValue = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
103+
const secondsValue = Math.floor((distance % (1000 * 60)) / 1000);
104+
if (seconds < 0) {
55105
reset();
56106
isValidOnExpire(onExpire) && onExpire();
57-
return 0;
58-
});
107+
} else {
108+
setSeconds(secondsValue);
109+
setMinutes(minutesValue);
110+
setHours(hoursValue);
111+
setDays(daysValue);
112+
}
59113
}
60114

61-
const intervalRef = useRef();
62-
63115
function start() {
64-
if(isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) {
116+
if (isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) {
65117
calculateExpiryDate();
66118
intervalRef.current = setInterval(() => calculateExpiryDate(), 1000);
67119
}
68120
}
69121

70122
function pause() {
71-
if(intervalRef.current) {
72-
clearInterval(intervalRef.current);
73-
intervalRef.current = undefined;
74-
}
75-
}
76-
77-
function reset() {
78123
if (intervalRef.current) {
79124
clearInterval(intervalRef.current);
80125
intervalRef.current = undefined;
81126
}
82-
setSeconds(0);
83-
setMinutes(0);
84-
setHours(0);
85-
setDays(0);
86127
}
87128

88129
function resume() {
89-
if(isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) {
130+
if (isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) {
90131
intervalRef.current = setInterval(() => subtractSecond(), 1000);
91132
}
92133
}
@@ -96,50 +137,14 @@ export default function useTimer(settings) {
96137
setExpiryTimestamp(newExpiryTimestamp);
97138
}
98139

99-
100-
// Timer expiry date calculation
101-
function calculateExpiryDate() {
102-
var now = new Date().getTime();
103-
var distance = expiryTimestamp - now;
104-
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
105-
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
106-
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
107-
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
108-
if(seconds < 0) {
109-
reset();
110-
isValidOnExpire(onExpire) && onExpire();
111-
} else {
112-
setSeconds(seconds);
113-
setMinutes(minutes);
114-
setHours(hours);
115-
setDays(days);
116-
}
117-
}
118-
119140
// didMount effect
120141
useEffect(() => {
121142
start();
122143
return reset;
123-
},[expiryTimestamp]);
124-
144+
}, [expiryTimestamp]);
125145

126-
// Validate expiryTimestamp
127-
function isValidExpiryTimestamp(expiryTimestamp) {
128-
const isValid = (new Date(expiryTimestamp)).getTime() > 0;
129-
if(!isValid) {
130-
console.warn('react-timer-hook: { useTimer } Invalid expiryTimestamp settings', expiryTimestamp);
131-
}
132-
return isValid;
133-
}
134-
135-
// Validate onExpire
136-
function isValidOnExpire(onExpire) {
137-
const isValid = onExpire && typeof onExpire === 'function';
138-
if(onExpire && !isValid) {
139-
console.warn('react-timer-hook: { useTimer } Invalid onExpire settings function', onExpire);
140-
}
141-
return isValid;
142-
}
143146

144-
return { seconds, minutes, hours, days, start, pause, resume, restart };
147+
return {
148+
seconds, minutes, hours, days, start, pause, resume, restart,
149+
};
145150
}

0 commit comments

Comments
 (0)