-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchList.tsx
121 lines (107 loc) · 3.49 KB
/
MatchList.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
116
117
118
119
120
121
import { useState } from 'react'
import BaseTitleWithIcon from './shared/BaseTitleWithIcon'
import { YellowButton } from './shared/BaseButton'
export interface MatchState {
id?: string
homeTeamName?: string
homeTeamScore: number
awayTeamName?: string
awayTeamScore: number
}
export const MatchList = ({
matches,
onSaveMatch
}: {
matches: MatchState[]
onSaveMatch: (match: MatchState) => void
}): JSX.Element => {
const DEFAULT_INPUTS = {
homeTeamScore: 0,
awayTeamScore: 0
}
const [selectedMatch, setSelectedMatch] = useState<MatchState>()
const [inputValues, setInputValues] = useState<MatchState>(DEFAULT_INPUTS)
const handleUpdateScores = (match: MatchState): void => {
setSelectedMatch(match)
setInputValues({
homeTeamScore: match.homeTeamScore,
awayTeamScore: match.awayTeamScore
})
}
const handleSaveScores = (match: MatchState): void => {
setSelectedMatch(undefined)
const updatedMatch: MatchState = {
...match,
homeTeamScore: Number(inputValues.homeTeamScore),
awayTeamScore: Number(inputValues.awayTeamScore)
}
onSaveMatch(updatedMatch)
}
const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
const { name, value } = event.target
setInputValues({ ...inputValues, [name]: value })
}
return (
<>
<BaseTitleWithIcon title='Matches'>
<span>⚽</span>
</BaseTitleWithIcon>
<table>
<thead>
<tr>
<th>#</th>
<th>Home Team</th>
<th>Score</th>
<th>Away Team</th>
<th>Score</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{matches.length === 0 && <tr><td className='text-center' colSpan={6}>No registered matches</td></tr>}
{matches.length > 0 &&
matches.map((match, index) => {
return (
<tr key={match.id}>
<td>{index + 1}</td>
<td>
{match.homeTeamName}
</td>
<td>
{selectedMatch?.id !== match.id
? <span>{match.homeTeamScore}</span>
: <input
type='number'
name='homeTeamScore'
placeholder='HT Score'
value={inputValues.homeTeamScore}
onChange={handleChange}
/>}
</td>
<td>
{match.awayTeamName}
</td>
<td>
{selectedMatch?.id !== match.id
? <span>{match.awayTeamScore}</span>
: <input
type='number'
name='awayTeamScore'
placeholder='AT Score'
value={inputValues.awayTeamScore}
onChange={handleChange}
/>}
</td>
<td>
{selectedMatch?.id !== match.id
? <YellowButton size='small' onClick={() => handleUpdateScores(match)}><span>Update Scores</span></YellowButton>
: <YellowButton size='small' onClick={() => handleSaveScores(match)}><span>Save Scores</span></YellowButton>}
</td>
</tr>
)
})}
</tbody>
</table>
</>
)
}