|
@@ -0,0 +1,62 @@
|
|
|
+import logo from './logo.svg';
|
|
|
+import React, { useState, useEffect, useRef } from 'react';
|
|
|
+import './App.scss';
|
|
|
+import { getTimeData, getTotalSeconds } from './utills';
|
|
|
+
|
|
|
+
|
|
|
+const Timer = ({ count, running }) => {
|
|
|
+
|
|
|
+ let [state, setState] = useState({ paused: !running, count, ...getTimeData(count) });
|
|
|
+ useEffect(() => {
|
|
|
+ let timer = undefined;
|
|
|
+ if (!state.paused) {
|
|
|
+ if (state.count > 0)
|
|
|
+ timer = setInterval(() => setStateInt({ count: state.count - 1 }), 1000);
|
|
|
+ else
|
|
|
+ pause();
|
|
|
+ }
|
|
|
+ return () => clearInterval(timer);
|
|
|
+ }, [state]);
|
|
|
+ const setStateInt = newState => setState(state = { ...state, ...newState });
|
|
|
+ const start = () => setStateInt({ paused: false });
|
|
|
+ const pause = () => setStateInt({ paused: true });
|
|
|
+ const reset = () => setStateInt({ count });
|
|
|
+ const stop = () => {
|
|
|
+ pause();
|
|
|
+ reset();
|
|
|
+ }
|
|
|
+
|
|
|
+ let time = getTimeData(state.count);
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <input value={state.hours}
|
|
|
+ onChange={e => setStateInt({ hours: e.target.value, count: getTotalSeconds(e.target.value, state.minutes, state.seconds) })}
|
|
|
+ type="number" />
|
|
|
+ <input value={state.minutes}
|
|
|
+ onChange={e => setStateInt({ minutes: e.target.value, count: getTotalSeconds(state.hours, e.target.value, state.seconds) })}
|
|
|
+ type="number" />
|
|
|
+ <input value={state.seconds}
|
|
|
+ onChange={e => setStateInt({ seconds: e.target.value, count: getTotalSeconds(state.hours, state.minutes, e.target.value) })}
|
|
|
+ type="number" />
|
|
|
+ {time.hours} : {time.minutes} : {time.seconds}
|
|
|
+ <br/>
|
|
|
+ <button onClick={() => start()}>Start</button>
|
|
|
+ <button onClick={() => pause()}>Pause</button>
|
|
|
+ <button onClick={() => reset()}>Reset</button>
|
|
|
+ <button onClick={() => stop()}>Stop</button>
|
|
|
+ </>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+function App() {
|
|
|
+
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <div className="App">
|
|
|
+ <Timer count={10} />
|
|
|
+ </div>
|
|
|
+ </>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+export default App;
|