Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - Add Timer #33

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ module.exports = {
'error',
2
],
'linebreak-style': [
'error',
'unix'
],
'linebreak-style': 0,
'quotes': [
'error',
'single',
Expand All @@ -45,6 +42,10 @@ module.exports = {
'error',
'always'
],
"prettier/prettier": ["error", {
"endOfLine":"auto"
}],
'no-console': 'off',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be able to get rid of this line. I'm not seeing any consoles that would require turning it off.

'react/prop-types': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/no-explicit-any': 0
Expand Down
1 change: 1 addition & 0 deletions env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=3001
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"workbox-webpack-plugin": "4.3.1"
},
"scripts": {
"start": "PORT=3001 node scripts/start.js",
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js",
"lint": "eslint --ext .ts,.tsx src/",
Expand Down
63 changes: 62 additions & 1 deletion src/components/Timer/Timer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
import React from 'react';
import { makeStyles } from '@material-ui/core';

const Timer: React.FC = () => <h1>Timer</h1>;
const useStyles = makeStyles(() => ({
timer: {
position: 'fixed',
left: 0,
width: '100vw',
height: '100vh',
margin: 0,
textAlign: 'center',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
},
}));

const Timer = () => {
const classes = useStyles();
const [minutes, setMintues] = React.useState(0);
const [seconds, setSeconds] = React.useState(-5);
Copy link
Owner

@ivnflpz ivnflpz Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like prefixing state variables with s to quickly identify them when reading through the code so these would become sMinutes and sSeconds.

const ref = React.useRef<any>();

const stopTimer = () => {
clearInterval(ref.current);
ref.current = null;
};

const startTimer = () => {
ref.current = setInterval(() => {
setSeconds((s) => {
if (s + 1 === 60) {
setMintues((m) => m + 1);
return 0;
}
return s + 1;
});
}, 1000);
};

const toggleTimer = () => {
if (ref.current != null) {
stopTimer();
} else {
startTimer();
}
};

return (
<div
className={classes.timer}
tabIndex={0}
role="button"
onClick={toggleTimer}
onKeyDown={toggleTimer}
>
<h1>
{minutes < 10 ? 0 : ''}
{minutes}:{seconds < 10 ? 0 : ''}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use something like padStart from lodash to handle adding these zeros for you.

{seconds < 0 ? Math.abs(seconds) : seconds}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since lookup time will always be at a maximum 15 seconds, could we only show seconds there. eg. when seconds state is negative we only show seconds and once its positive, it goes to minutes:seconds.

</h1>
</div>
);
};

export default Timer;