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 | |
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')
-rw-r--r-- | src/pages/Contribute/Contribute.tsx (renamed from src/pages/Contribute/Contribute.js) | 4 | ||||
-rw-r--r-- | src/pages/Profile/Profile.tsx (renamed from src/pages/Profile/Profile.js) | 19 | ||||
-rw-r--r-- | src/pages/Profile/Registration.tsx (renamed from src/pages/Profile/Registration/Registration.js) | 21 | ||||
-rw-r--r-- | src/pages/Scoreboard/Scoreboard.tsx (renamed from src/pages/Scoreboard/Scoreboard.js) | 12 | ||||
-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 |
6 files changed, 72 insertions, 40 deletions
diff --git a/src/pages/Contribute/Contribute.js b/src/pages/Contribute/Contribute.tsx index aa1c3f7..4c37fb9 100644 --- a/src/pages/Contribute/Contribute.js +++ b/src/pages/Contribute/Contribute.tsx @@ -17,6 +17,7 @@ import NewReleasesIcon from '@material-ui/icons/NewReleases'; import { Window, ContentSection } from 'react-benzin'; +import developers from '../../developers.json'; const useStyles = makeStyles(theme => ({ mono: { @@ -31,10 +32,9 @@ const useStyles = makeStyles(theme => ({ })); -const developers = require('../../developers.json'); -const Contribute = () => { +const Contribute: React.FC = () => { const classes = useStyles(); return ( diff --git a/src/pages/Profile/Profile.js b/src/pages/Profile/Profile.tsx index 65c3734..83acb30 100644 --- a/src/pages/Profile/Profile.js +++ b/src/pages/Profile/Profile.tsx @@ -5,12 +5,13 @@ import { makeStyles, } from '@material-ui/core'; -import Registration from './Registration/Registration'; +import Registration from './Registration'; import { Window, ContentSection, SmartList, } from 'react-benzin'; +import { User, Solution, RenderPropTypes } from '../../types'; import SolutionCard from '../../components/SolutionCard/SolutionCard'; @@ -27,12 +28,18 @@ const useStyles = makeStyles(theme => ({ })); -const Profile = ({ user, setUser }) => { +interface PropTypes { + user: User; + setUser: (user: User) => void; +} + + +const Profile: React.FC<PropTypes> = ({ user, setUser }) => { const classes = useStyles(); - const [profileSolutions, setProfileSolutions] = useState([]); + const [profileSolutions, setProfileSolutions] = useState<Solution[]>([]); - const handleLogout = () => { + const handleLogout = (): void => { setUser({ username: 'anonymous', id: null }); localStorage.clear(); }; @@ -43,11 +50,11 @@ const Profile = ({ user, setUser }) => { }); }, [user]); - const removeSolution = (id) => { + const removeSolution = (id: number): void => { setProfileSolutions(profileSolutions.filter((solution => solution.id !== id))); }; - const renderItem = ({ index, style }) => { + const renderItem: React.FC<RenderPropTypes> = ({ index, style }) => { return ( <div style={style} className={classes.cell}> <SolutionCard data={profileSolutions[index]} removeThisCard={removeSolution} /> diff --git a/src/pages/Profile/Registration/Registration.js b/src/pages/Profile/Registration.tsx index b2d5503..a5e0f3e 100644 --- a/src/pages/Profile/Registration/Registration.js +++ b/src/pages/Profile/Registration.tsx @@ -7,25 +7,30 @@ import { FormControlLabel, Grid, } from '@material-ui/core'; +import { User } from '../../types'; import { ContentSection } from 'react-benzin'; -import { get, post } from '../../../requests'; +import { get, post } from '../../requests'; -const Registration = ({ setUser }) => { +interface PropTypes { + setUser: (user: User) => void; +} - const [username, setUsername] = useState(''); - const [isRememberMe, setIsRememberMe] = useState(false); +const Registration: React.FC<PropTypes> = ({ setUser }) => { - const handleChange = (event) => { + const [username, setUsername] = useState<string>(''); + const [isRememberMe, setIsRememberMe] = useState<boolean>(false); + + const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => { setUsername(event.target.value); }; - const handleCheck = (event) => { + const handleCheck = (event: React.ChangeEvent<HTMLInputElement>): void => { setIsRememberMe(event.target.checked); }; - const handleSubmit = () => { + const handleSubmit = (): void => { if (username !== '') { post('users/', { username }) .then(response => { @@ -37,7 +42,7 @@ const Registration = ({ setUser }) => { }) .catch(err => { get('users/').then(response => { - const user = response.data.filter(user => user.username === username)[0]; + const user = response.data.filter((user: User) => user.username === username)[0]; setUser(user); if (isRememberMe) { localStorage.setItem('userId', user.id); diff --git a/src/pages/Scoreboard/Scoreboard.js b/src/pages/Scoreboard/Scoreboard.tsx index 47c0899..e4185bd 100644 --- a/src/pages/Scoreboard/Scoreboard.js +++ b/src/pages/Scoreboard/Scoreboard.tsx @@ -3,6 +3,7 @@ import React, { useEffect, useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Window, SmartList } from 'react-benzin'; +import { Solution, RenderPropTypes } from '../../types'; import SolutionCard from '../../components/SolutionCard/SolutionCard'; import Loading from '../../components/Loading/Loading'; @@ -22,17 +23,18 @@ const useStyles = makeStyles(theme => ({ } })); -const Scoreboard = () => { + +const Scoreboard: React.FC = () => { const classes = useStyles(); - const [solutions, setSolutions] = useState([]); + const [solutions, setSolutions] = useState<Solution[]>([]); - const updateSolutions = () => { + const updateSolutions = (): void => { get('scoreboard/').then(response => { setSolutions(response.data); }); }; - const removeSolution = id => { + const removeSolution = (id: number): void => { updateSolutions(); }; @@ -40,7 +42,7 @@ const Scoreboard = () => { setTimeout(updateSolutions, 300); }, []); - const renderItem = ({ index, style }) => { + const renderItem: React.FC<RenderPropTypes> = ({ index, style }) => { return ( <div style={style} className={classes.cell}> <SolutionCard data={solutions[index]} removeThisCard={removeSolution}/> 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); |