|
1 | 1 | import { useState, useEffect, useRef } from 'react'; |
2 | | - |
3 | | - |
4 | | -function isValidExpiryTimestamp(expiryTimestamp) { |
5 | | - const isValid = (new Date(expiryTimestamp)).getTime() > 0; |
6 | | - if (!isValid) { |
7 | | - console.warn('react-timer-hook: { useTimer } Invalid expiryTimestamp settings', expiryTimestamp); |
8 | | - } |
9 | | - return isValid; |
10 | | -} |
11 | | - |
12 | | -function isValidOnExpire(onExpire) { |
13 | | - const isValid = onExpire && typeof onExpire === 'function'; |
14 | | - if (onExpire && !isValid) { |
15 | | - console.warn('react-timer-hook: { useTimer } Invalid onExpire settings function', onExpire); |
16 | | - } |
17 | | - return isValid; |
18 | | -} |
| 2 | +import { Time, Validate } from './utils'; |
19 | 3 |
|
20 | 4 | export default function useTimer(settings) { |
21 | 5 | const { expiryTimestamp: expiry, onExpire } = settings || {}; |
22 | 6 | const [expiryTimestamp, setExpiryTimestamp] = useState(expiry); |
23 | | - |
24 | | - const [seconds, setSeconds] = useState(0); |
25 | | - const [minutes, setMinutes] = useState(0); |
26 | | - const [hours, setHours] = useState(0); |
27 | | - const [days, setDays] = useState(0); |
| 7 | + const [seconds, setSeconds] = useState(Time.getSecondsFromExpiry(expiryTimestamp)); |
28 | 8 | const intervalRef = useRef(); |
29 | 9 |
|
30 | | - function reset() { |
| 10 | + function clearIntervalRef() { |
31 | 11 | if (intervalRef.current) { |
32 | 12 | clearInterval(intervalRef.current); |
33 | 13 | intervalRef.current = undefined; |
34 | 14 | } |
35 | | - setSeconds(0); |
36 | | - setMinutes(0); |
37 | | - setHours(0); |
38 | | - setDays(0); |
39 | | - } |
40 | | - |
41 | | - function subtractDay() { |
42 | | - setDays((prevDays) => { |
43 | | - if (prevDays > 0) { |
44 | | - return prevDays - 1; |
45 | | - } |
46 | | - reset(); |
47 | | - isValidOnExpire(onExpire) && onExpire(); |
48 | | - return 0; |
49 | | - }); |
50 | | - } |
51 | | - |
52 | | - function subtractHour() { |
53 | | - setHours((prevHours) => { |
54 | | - if (prevHours === 0) { |
55 | | - subtractDay(); |
56 | | - return 23; |
57 | | - } |
58 | | - |
59 | | - if (prevHours > 0) { |
60 | | - return prevHours - 1; |
61 | | - } |
62 | | - return 0; |
63 | | - }); |
64 | | - } |
65 | | - |
66 | | - function subtractMinute() { |
67 | | - setMinutes((prevMinutes) => { |
68 | | - if (prevMinutes === 0) { |
69 | | - subtractHour(); |
70 | | - return 59; |
71 | | - } |
72 | | - |
73 | | - if (prevMinutes > 0) { |
74 | | - return prevMinutes - 1; |
75 | | - } |
76 | | - return 0; |
77 | | - }); |
78 | | - } |
79 | | - |
80 | | - function subtractSecond() { |
81 | | - setSeconds((prevSeconds) => { |
82 | | - if (prevSeconds === 0) { |
83 | | - subtractMinute(); |
84 | | - return 59; |
85 | | - } |
86 | | - |
87 | | - if (prevSeconds > 0) { |
88 | | - return prevSeconds - 1; |
89 | | - } |
90 | | - return 0; |
91 | | - }); |
92 | | - } |
93 | | - |
94 | | - // Timer expiry date calculation |
95 | | - function calculateExpiryDate() { |
96 | | - const now = new Date().getTime(); |
97 | | - const distance = expiryTimestamp - now; |
98 | | - const daysValue = Math.floor(distance / (1000 * 60 * 60 * 24)); |
99 | | - const hoursValue = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); |
100 | | - const minutesValue = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); |
101 | | - const secondsValue = Math.floor((distance % (1000 * 60)) / 1000); |
102 | | - if (secondsValue < 0) { |
103 | | - reset(); |
104 | | - isValidOnExpire(onExpire) && onExpire(); |
105 | | - } else { |
106 | | - setSeconds(secondsValue); |
107 | | - setMinutes(minutesValue); |
108 | | - setHours(hoursValue); |
109 | | - setDays(daysValue); |
110 | | - } |
111 | 15 | } |
112 | 16 |
|
113 | 17 | function start() { |
114 | | - if (isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) { |
115 | | - calculateExpiryDate(); |
116 | | - intervalRef.current = setInterval(() => calculateExpiryDate(), 1000); |
| 18 | + if (!intervalRef.current) { |
| 19 | + intervalRef.current = setInterval(() => { |
| 20 | + const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp); |
| 21 | + if (secondsValue <= 0) { |
| 22 | + clearIntervalRef(); |
| 23 | + Validate.onExpire(onExpire) && onExpire(); |
| 24 | + } |
| 25 | + setSeconds(secondsValue); |
| 26 | + }, 1000); |
117 | 27 | } |
118 | 28 | } |
119 | 29 |
|
120 | 30 | function pause() { |
121 | | - if (intervalRef.current) { |
122 | | - clearInterval(intervalRef.current); |
123 | | - intervalRef.current = undefined; |
124 | | - } |
| 31 | + clearIntervalRef(); |
125 | 32 | } |
126 | 33 |
|
127 | 34 | function resume() { |
128 | | - if (isValidExpiryTimestamp(expiryTimestamp) && !intervalRef.current) { |
129 | | - intervalRef.current = setInterval(() => subtractSecond(), 1000); |
| 35 | + if (!intervalRef.current) { |
| 36 | + intervalRef.current = setInterval(() => setSeconds((prevSeconds) => (prevSeconds - 1)), 1000); |
130 | 37 | } |
131 | 38 | } |
132 | 39 |
|
133 | 40 | function restart(newExpiryTimestamp) { |
134 | | - reset(); |
| 41 | + clearIntervalRef(); |
135 | 42 | setExpiryTimestamp(newExpiryTimestamp); |
136 | 43 | } |
137 | 44 |
|
138 | | - // didMount effect |
139 | 45 | useEffect(() => { |
140 | | - start(); |
141 | | - return reset; |
| 46 | + if (Validate.expiryTimestamp(expiryTimestamp)) { |
| 47 | + setSeconds(Time.getSecondsFromExpiry(expiryTimestamp)); |
| 48 | + start(); |
| 49 | + } |
| 50 | + return clearIntervalRef; |
142 | 51 | }, [expiryTimestamp]); |
143 | 52 |
|
144 | 53 |
|
145 | 54 | return { |
146 | | - seconds, minutes, hours, days, start, pause, resume, restart, |
| 55 | + ...Time.getTimeFromSeconds(seconds), start, pause, resume, restart, |
147 | 56 | }; |
148 | 57 | } |
0 commit comments