-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
157 lines (126 loc) · 4.1 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Use the API_URL variable to make fetch requests to the API.
// Replace the placeholder with your cohort name (ex: 2109-UNF-HY-WEB-PT)
const cohortName = "YOUR COHORT NAME HERE";
const API_URL = `https://fsa-puppy-bowl.herokuapp.com/api/${cohortName}`;
/**
* Fetches all players from the API.
* @returns {Object[]} the array of player objects
*/
const fetchAllPlayers = async () => {
try { const response = await fetch ('https://fsa-puppy-bowl.herokuapp.com/api/2307-FSA-ET-WEB-FT-SF/players');
const data = await response.json();
// console.log(data.data.players)
return data.data.players
// TODO
} catch (err) {
console.error("Uh oh, trouble fetching players!", err);
}
};
/**
* Fetches a single player from the API.
* @param {number} playerId
* @returns {Object} the player object
*/
const fetchSinglePlayer = async (playerId) => {
try { const response = await fetch (`https://fsa-puppy-bowl.herokuapp.com/api/2307-FSA-ET-WEB-FT-SF/players/${playerId}/`);
const data = await response.json();
return data.data.player;
// TODO
} catch (err) {
console.error(`Oh no, trouble fetching player #${playerId}!`, err);
}
};
// console.log(fetchSinglePlayer(640));
/**
* Updates `<main>` to display a list of all players.
*
* If there are no players, a corresponding message is displayed instead.
*
* Each player is displayed in a card with the following information:
* - name
* - id
* - image (with alt text of the player's name)
*
* Additionally, each card has two buttons:
* - "See details" button that, when clicked, calls `renderSinglePlayer` to
* display more information about the player
* - "Remove from roster" button that, when clicked, will call `removePlayer` to
* remove that specific player and then re-render all players
*
* Note: this function should replace the current contents of `<main>`, not append to it.
* @param {Object[]} playerList - an array of player objects
*/
// div for players info
function createElement(dt){
const element = document.createElement("div");
// names
const name = document.createElement("h1");
name.innerHTML = dt.name
// id
const id = document.createElement("h2");
id.innerHTML = dt.id
// img
const image = document.createElement("h3");
image.innerHTML = dt.imageUrl
// buttons
const detailsBut = document.createElement("button");
detailsBut.innerHTML= "See Details";
detailsBut.addEventListener("click",()=>{
renderSinglePlayer(dt.id)
})
const removeBut = document.createElement("button");
removeBut.innerHTML="Remove from roster";
removeBut.addEventListener("click",()=>{
element.remove()
})
element.appendChild(name);
element.appendChild(id);
element.appendChild(image);
element.appendChild(detailsBut);
element.appendChild(removeBut);
document.getElementsByTagName("main")[0].appendChild(element);
}
const renderAllPlayers = (playerList) => {
fetchAllPlayers().then(response=>{
response.forEach((i) =>
createElement(i))
})
};
/**
* Updates `<main>` to display a single player.
* The player is displayed in a card with the following information:
* - name
* - id
* - breed
* - image (with alt text of the player's name)
* - team name, if the player has one, or "Unassigned"
*
* The card also contains a "Back to all players" button that, when clicked,
* will call `renderAllPlayers` to re-render the full list of players.
* @param {Object} player an object representing a single player
*/
const renderSinglePlayer = (player) => {
fetchSinglePlayer(player).then(response=>{
createElement(response)
})
};
/**
* Initializes the app by fetching all players and rendering them to the DOM.
*/
const init = async () => {
const players = await fetchAllPlayers();
renderAllPlayers(players);
};
// This script will be run using Node when testing, so here we're doing a quick
// check to see if we're in Node or the browser, and exporting the functions
// we want to test if we're in Node.
if (typeof window === "undefined") {
module.exports = {
fetchAllPlayers,
fetchSinglePlayer,
renderAllPlayers,
renderSinglePlayer,
};
} else {
init();
}