-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreBoardForm.tsx
115 lines (95 loc) · 3.05 KB
/
ScoreBoardForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { useState } from 'react'
import { v4 as uuid } from 'uuid'
import { MatchState } from './MatchList'
import BaseTitleWithIcon from './shared/BaseTitleWithIcon'
import { BaseButton } from './shared/BaseButton'
export const ScoreBoardForm = ({ onEndGame }: { onEndGame: (match: MatchState) => void }): JSX.Element => {
const DEFAULT_FORM = {
homeTeamName: '',
homeTeamScore: 0,
awayTeamName: '',
awayTeamScore: 0
}
const [form, setForm] = useState<MatchState>(DEFAULT_FORM)
const [errorHomeTeamName, setErrorHomeTeamName] = useState('')
const [errorAwayTeamName, setErrorAwayTeamName] = useState('')
const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
const { name, value } = event.target
setErrorHomeTeamName('')
setErrorAwayTeamName('')
setForm({ ...form, [name]: value })
}
const handleSubmit = (event: React.ChangeEvent<HTMLFormElement>): void => {
event.preventDefault()
if (form?.homeTeamName === '') {
setErrorHomeTeamName('Home team name is required')
}
if (form?.awayTeamName === '') {
setErrorAwayTeamName('Away team name is required')
}
if (form?.homeTeamName === '' || form?.awayTeamName === '') return
const id = uuid()
const newMatch: MatchState = {
...form,
id,
homeTeamScore: Number(form.homeTeamScore),
awayTeamScore: Number(form.awayTeamScore)
}
setForm(DEFAULT_FORM)
onEndGame(newMatch)
}
return (
<>
<BaseTitleWithIcon title='Start Game'>
<span>⚽</span>
</BaseTitleWithIcon>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor='homeTeamName'>Home Team Name</label>
<input
type='text'
name='homeTeamName'
placeholder='Home Team Name'
value={form.homeTeamName}
onChange={handleChange}
/>
{errorHomeTeamName !== '' && <div className='is-required'>{errorHomeTeamName}</div>}
</div>
<div>
<label htmlFor='homeTeamScore'>Home Team Score</label>
<input
type='number'
name='homeTeamScore'
placeholder='Home Team Score'
value={form.homeTeamScore}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor='awayTeamName'>Away Team Name</label>
<input
type='text'
name='awayTeamName'
placeholder='Away Team Name'
value={form.awayTeamName}
onChange={handleChange}
/>
{errorAwayTeamName !== '' && <div className='is-required'>{errorAwayTeamName}</div>}
</div>
<div>
<label htmlFor='awayTeamScore'>Away Team Score</label>
<input
type='number'
name='awayTeamScore'
placeholder='Away Team Score'
value={form.awayTeamScore}
onChange={handleChange}
/>
</div>
<BaseButton>
<span>Finish Game</span>
</BaseButton>
</form>
</>
)
}