diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-03-21 17:59:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-21 17:59:48 +0300 |
commit | e89a7a79622d25561dc80a8489ae1f6022aebd73 (patch) | |
tree | e115938b26ee6019b4c349f52bd98fc34a4575e9 /src/pages/Timer | |
parent | fe750a9aadfb451b9537bb3cdd79581ef5120c07 (diff) | |
parent | 7f8ab6802c68c46f988ef012fdc88b09e43a9e54 (diff) | |
download | chrono-cube-ui-e89a7a79622d25561dc80a8489ae1f6022aebd73.tar.gz |
Merge pull request #48 from eug-vs/typescript
Migrate project to Typescript
Diffstat (limited to 'src/pages/Timer')
-rw-r--r-- | src/pages/Timer/Timer.tsx (renamed from src/pages/Timer/Timer.js) | 26 | ||||
-rw-r--r-- | src/pages/Timer/TimerButton.tsx (renamed from src/pages/Timer/TimerButton/TimerButton.js) | 30 |
2 files changed, 37 insertions, 19 deletions
diff --git a/src/pages/Timer/Timer.js b/src/pages/Timer/Timer.tsx index 6020c1b..a890815 100644 --- a/src/pages/Timer/Timer.js +++ b/src/pages/Timer/Timer.tsx @@ -7,8 +7,9 @@ import { ContentSection, SmartList, } from 'react-benzin'; +import { User, Solution, RenderPropTypes } from '../../types'; -import TimerButton from './TimerButton/TimerButton'; +import TimerButton from './TimerButton'; import SolutionCard from '../../components/SolutionCard/SolutionCard'; import { Button, makeStyles } from '@material-ui/core'; @@ -23,29 +24,38 @@ const useStyles = makeStyles(theme => ({ }, })); -const Timer = ({ user, recentSolutions, setRecentSolutions, setPage }) => { + +interface PropTypes { + user: User; + recentSolutions: Solution[]; + setRecentSolutions: (newRecentSolutions: Solution[]) => void; + setPage: (newPage: string) => void; +} + + +const Timer: React.FC<PropTypes> = ({ user, recentSolutions, setRecentSolutions, setPage }) => { const classes = useStyles(); - const registerResult = result => { - const solution = { author_id: user.id, result }; + const registerResult = (result: string): void => { + const solution = { 'author_id': user.id, result }; post('solutions/', solution).then(response => { setRecentSolutions([response.data].concat(recentSolutions)); }); }; - const handleLearnMore = () => { + const handleLearnMore = (): void => { setPage('contribute'); }; - const handleLogin = () => { + const handleLogin = (): void => { setPage('profile'); }; - const removeSolution = (id) => { + const removeSolution = (id: number): void => { setRecentSolutions(recentSolutions.filter((solution => solution.id !== id))); }; - const renderItem = ({ index, style }) => { + const renderItem: React.FC<RenderPropTypes> = ({ index, style }) => { const solution = recentSolutions[index]; return ( <div style={style} className={classes.cell}> diff --git a/src/pages/Timer/TimerButton/TimerButton.js b/src/pages/Timer/TimerButton.tsx index fdb6b7c..0a3bf38 100644 --- a/src/pages/Timer/TimerButton/TimerButton.js +++ b/src/pages/Timer/TimerButton.tsx @@ -12,13 +12,21 @@ const useStyles = makeStyles(theme => ({ }, })); -const TimerButton = ({ registerResult }) => { + +interface PropTypes { + registerResult: (result: string) => void; +} + +type Mode = 'idle' | 'countdown' | 'running' | 'over'; + + +const TimerButton: React.FC<PropTypes> = ({ registerResult }) => { const classes = useStyles(); const SPACE = 32; const maxCountdown = 15000; - const [time, setTime] = useState('00:00:00'); - const [mode, setMode] = useState('idle'); + const [time, setTime] = useState<string>('00:00:00'); + const [mode, setMode] = useState<Mode>('idle'); useEffect(()=> { const timestamp = Date.now(); @@ -29,14 +37,14 @@ const TimerButton = ({ registerResult }) => { if (timeDelta <= 0) setMode('over'); setTime(convertTimeToString(timeDelta)); }, 10); - return () => clearInterval(repeater); + return (): void => clearInterval(repeater); } if (mode === 'running') { const repeater = setInterval(() => { setTime(convertTimeToString(Date.now() - timestamp)); }, 10); - return () => clearInterval(repeater); + return (): void => clearInterval(repeater); } if (mode === 'over') { @@ -44,12 +52,12 @@ const TimerButton = ({ registerResult }) => { } }, [mode]); - const handleKeyPress = event => { + const handleKeyPress = (event: KeyboardEvent): void => { event.preventDefault(); if (event.keyCode === SPACE && mode === 'idle' ) setMode('countdown'); }; - const handleKeyUp = event => { + const handleKeyUp = (event: KeyboardEvent): void => { if (event.keyCode === SPACE) { if (mode === 'running') { registerResult(time); @@ -66,13 +74,13 @@ const TimerButton = ({ registerResult }) => { window.addEventListener('keyup', handleKeyUp); window.addEventListener('keypress', handleKeyPress); - return () => { + return (): void => { window.removeEventListener('keyup', handleKeyUp); window.removeEventListener('keypress', handleKeyPress); }; }); - const composeHelperText = () => { + const composeHelperText = (): string => { switch (mode) { case 'running': return 'Go fast!'; case 'countdown': return 'Release SPACE to begin'; @@ -81,7 +89,7 @@ const TimerButton = ({ registerResult }) => { } }; - const helperColor = () => { + const helperColor = (): 'primary' | 'secondary' | 'textSecondary' => { switch (mode) { case 'running': return 'primary'; case 'over': return 'secondary'; @@ -99,7 +107,7 @@ const TimerButton = ({ registerResult }) => { ); }; -const convertTimeToString = timeDelta => { +const convertTimeToString = (timeDelta: number): string => { let resultTime = ''; const minute = Math.floor(timeDelta / 60000); |