-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (61 loc) · 2.46 KB
/
script.js
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
const loadPlayers = () => {
const url = `https://www.thesportsdb.com/api/v1/json/2/searchplayers.php?p=g`;
fetch(url)
.then(res => res.json())
.then(data => showPlayers(data.player));
}
const showPlayers = players => {
const playersContainer = document.getElementById('players-container');
playersContainer.innerHTML = '';
players.forEach(player => {
const div = document.createElement('div');
div.classList.add('col');
div.innerHTML = `
<div onclick="loadPlayerDetails(${player.idPlayer})" class="card h-100">
<img src="${player.strThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${player.strPlayer}</h5>
<p class="card-text">Team: ${player.strTeam}</p>
<p class="card-text">Jersey Number: ${player.strNumber ? player.strNumber : 'Not Given'}</p>
</div>
</div>
`;
playersContainer.appendChild(div);
})
}
loadPlayers();
// search players
const searchPlayers = () => {
const searchInputField = document.getElementById('search-input-field');
const searchValue = searchInputField.value;
loadPlayersByName(searchValue);
}
const loadPlayersByName = name => {
const url = `https://www.thesportsdb.com/api/v1/json/2/searchplayers.php?p=${name}`;
fetch(url)
.then(res => res.json())
.then(data => showPlayers(data.player));
}
// player details
const loadPlayerDetails = id => {
const url = `https://www.thesportsdb.com/api/v1/json/2/lookupplayer.php?id=${id}`
fetch(url)
.then(res => res.json())
.then(data => showPlayerDetails(data.players[0]));
}
const showPlayerDetails = details => {
const playerDetails = document.getElementById('player-details');
playerDetails.innerHTML = `
<div class="card-body">
<img src="${details.strThumb}" class="card-img-top" alt="">
<h5 class="card-title">${details.strPlayer}</h5>
<p class="card-text">Nationality: ${details.strNationality}</p>
<p class="card-text">Sports: ${details.strSport}</p>
<p class="card-text">Team: ${details.strTeam}</p>
<p class="card-text">Position: ${details.strPosition}</p>
<p class="card-text">Jersey Number: ${details.strNumber ? details.strNumber : 'Not Given'}</p>
<p class="card-text">Height: ${details.strHeight}</p>
<p class="card-text">Weight: ${details.strWeight}</p>
</div>
`;
}