diff options
Diffstat (limited to 'src/pages')
-rw-r--r-- | src/pages/Timer/Timer.tsx (renamed from src/pages/Timer/Timer.js) | 24 | ||||
-rw-r--r-- | src/pages/Timer/TimerButton.tsx (renamed from src/pages/Timer/TimerButton/TimerButton.js) | 20 |
2 files changed, 31 insertions, 13 deletions
diff --git a/src/pages/Timer/Timer.js b/src/pages/Timer/Timer.tsx index 6020c1b..3ceb674 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 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..86bbf76 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(); @@ -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); @@ -99,7 +107,7 @@ const TimerButton = ({ registerResult }) => { ); }; -const convertTimeToString = timeDelta => { +const convertTimeToString = (timeDelta: number): string => { let resultTime = ''; const minute = Math.floor(timeDelta / 60000); |