Skip to content

Commit f2a0ad6

Browse files
authored
Merge pull request #7 from amrlabib/webpack-config
Webpack config
2 parents 2f45955 + cf83b58 commit f2a0ad6

File tree

11 files changed

+221
-180
lines changed

11 files changed

+221
-180
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dev-dist
12
dist
23
node_modules
34
.DS_Store

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ src
44
build
55
!.eslintrc
66
yarn-error.log
7+
dev-dist
78

89
*.js
910
!dist/index.js

src/App.js renamed to app/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { useTimer } from './useTimer';
2+
import { useTimer } from '../src/index';
33

44
function MyTimer({ expiryTimestamp }) {
55
const {

app/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
ReactDOM.render(<App />, document.getElementById('app'));

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "react-timer-hook",
33
"version": "1.1.5",
4-
"description": "React timer hook is a custom react hook built to handle timers, stopwatch and timer logic and state in your react component.",
4+
"description": "React timer hook is a custom react hook built to handle timers, stopwatch and time logic/state in your react component.",
55
"main": "dist/index.js",
66
"scripts": {
7-
"build:dev": "webpack && webpack-dev-server",
8-
"build:prod": "rm -rf ./dist && webpack"
7+
"build:dev": "webpack --config webpack.dev.js && webpack-dev-server --open --config webpack.dev.js",
8+
"build:prod": "rm -rf ./dist && webpack --config webpack.prod.js"
99
},
1010
"repository": {
1111
"type": "git",

src/index.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import App from './App';
1+
import { useEffect } from 'react';
2+
import useTimer from './useTimer';
3+
import useStopwatch from './useStopwatch';
4+
import useTime from './useTime';
45

5-
ReactDOM.render(<App />, document.getElementById('app'));
6+
export {
7+
useTimer,
8+
useStopwatch,
9+
useTime,
10+
}
11+
12+
// This deprecated default export is just to avoid breaking old versions code before v1.1.0
13+
export default function deprecatedUseTimer(settings) {
14+
// didMount effect
15+
useEffect(() => {
16+
console.warn('react-timer-hook: default export useTimer is deprecated, use named exports { useTimer, useStopwatch, useTime } instead');
17+
},[]);
18+
19+
if(settings.expiryTimestamp) {
20+
const values = useTimer(settings);
21+
return { ...values, startTimer: values.start, stopTimer: values.pause, resetTimer: () => {} };
22+
} else {
23+
const values = useStopwatch(settings);
24+
return { ...values, startTimer: values.start, stopTimer: values.pause, resetTimer: values.reset };
25+
}
26+
}

src/useStopwatch.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { useState, useEffect, useRef } from 'react';
2+
3+
/* --------------------- useStopwatch ----------------------- */
4+
5+
export default function useStopwatch(settings) {
6+
const { autoStart } = settings || {};
7+
8+
// Seconds
9+
const [seconds, setSeconds] = useState(0);
10+
function addSecond() {
11+
setSeconds(prevSeconds => {
12+
if(prevSeconds === 59) {
13+
addMinute();
14+
return 0;
15+
}
16+
return prevSeconds + 1;
17+
});
18+
}
19+
20+
// Minutes
21+
const [minutes, setMinutes] = useState(0);
22+
function addMinute() {
23+
setMinutes(prevMinutes => {
24+
if (prevMinutes === 59) {
25+
addHour();
26+
return 0;
27+
}
28+
return prevMinutes + 1;
29+
});
30+
}
31+
32+
// Hours
33+
const [hours, setHours] = useState(0);
34+
function addHour() {
35+
setHours(prevHours => {
36+
if (prevHours === 23) {
37+
addDay();
38+
return 0;
39+
}
40+
return prevHours + 1;
41+
});
42+
}
43+
44+
// Days
45+
const [days, setDays] = useState(0);
46+
function addDay() {
47+
setDays(prevDays => {
48+
return prevDays + 1;
49+
});
50+
}
51+
52+
// Control functions
53+
const intervalRef = useRef();
54+
55+
function start() {
56+
if(!intervalRef.current) {
57+
intervalRef.current = setInterval(() => addSecond(), 1000);
58+
}
59+
}
60+
61+
function pause() {
62+
if(intervalRef.current) {
63+
clearInterval(intervalRef.current);
64+
intervalRef.current = undefined;
65+
}
66+
}
67+
68+
function reset() {
69+
if (intervalRef.current) {
70+
clearInterval(intervalRef.current);
71+
intervalRef.current = undefined;
72+
}
73+
setSeconds(0);
74+
setMinutes(0);
75+
setHours(0);
76+
setDays(0);
77+
}
78+
79+
// didMount effect
80+
useEffect(() => {
81+
if(autoStart) {
82+
start();
83+
}
84+
return reset;
85+
},[]);
86+
87+
return { seconds, minutes, hours, days, start, pause, reset };
88+
}

src/useTime.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { useState, useEffect, useRef } from 'react';
2+
3+
/* ---------------------- useTime --------------------- */
4+
5+
export default function useTime(settings) {
6+
const { format } = settings || {};
7+
const [seconds, setSeconds] = useState(0);
8+
const [minutes, setMinutes] = useState(0);
9+
const [hours, setHours] = useState(0);
10+
const [ampm, setAmPm] = useState('');
11+
12+
const intervalRef = useRef();
13+
function start() {
14+
if(!intervalRef.current) {
15+
setCurrentTime();
16+
intervalRef.current = setInterval(() => setCurrentTime(), 1000);
17+
}
18+
}
19+
20+
function reset() {
21+
if (intervalRef.current) {
22+
clearInterval(intervalRef.current);
23+
intervalRef.current = undefined;
24+
}
25+
setSeconds(0);
26+
setMinutes(0);
27+
setHours(0);
28+
setAmPm('');
29+
}
30+
31+
function formatHours(hours) {
32+
if (format === '12-hour') {
33+
const ampm = hours >= 12 ? 'pm' : 'am';
34+
var formattedHours = hours % 12;
35+
formattedHours = formattedHours || 12;
36+
return { hours: formattedHours, ampm };
37+
}
38+
return { hours, ampm: '' };
39+
}
40+
41+
42+
function setCurrentTime() {
43+
var now = new Date();
44+
const seconds = now.getSeconds();
45+
const minutes = now.getMinutes();
46+
const { hours, ampm } = formatHours(now.getHours());
47+
48+
49+
setSeconds(seconds);
50+
setMinutes(minutes);
51+
setHours(hours);
52+
setAmPm(ampm);
53+
}
54+
55+
// didMount effect
56+
useEffect(() => {
57+
start();
58+
return reset;
59+
},[]);
60+
61+
62+
return { seconds, minutes, hours, ampm };
63+
}

0 commit comments

Comments
 (0)