/
players.js
62 lines (52 loc) · 1.9 KB
/
players.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
// hard coded data for purposes of illustration
const LEAGUE_NAME = "English Premier League";
const TEAM_NAME = "Arsenal";
const BASE_URL = "https://www.thesportsdb.com/api/v1/json/1/";
const ALL_LEAGUES_URL = BASE_URL + "all_leagues.php";
const ALL_TEAMS_URL = BASE_URL + "lookup_all_teams.php";
const ALL_PLAYERS_URL = BASE_URL + "lookup_all_players.php";
const PLAYER_HONORS_URL = BASE_URL + "lookuphonors.php";
function myFetch(url, params) {
if (params) {
url += "?" + encodeParams(params);
}
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(response.status);
}
return response.json()
}
);
}
function encodeParams(params) {
return Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
}
function findLeagueId(leagueData, leagueName) {
const league = leagueData.leagues.find(l => l.strLeague === leagueName);
return league ? league.idLeague : null;
}
function findTeamId(teamData, teamName) {
const team = teamData.teams.find(t => t.strTeam === teamName);
return team ? team.idTeam : null;
}
function printHonors(honorData) {
if (honorData.honors != null) {
var playerLI = document.createElement("li");
document.getElementById("honorsList").append(playerLI);
var playerName =
document.createTextNode(honorData.honors[0].strPlayer);
playerLI.appendChild(playerName);
var honorsUL= document.createElement("ul");
playerLI.appendChild(honorsUL);
honorData.honors.forEach(honor => {
var honorLI = document.createElement("li");
honorsUL.appendChild(honorLI);
var honorText = document.createTextNode(
`${honor.strHonour} - ${honor.strSeason}`);
honorLI.appendChild(honorText);
});
}
}