-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
210 lines (188 loc) · 6.29 KB
/
index.js
File metadata and controls
210 lines (188 loc) · 6.29 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// === Constants ===
const BASE = "https://fsa-puppy-bowl.herokuapp.com/api";
const COHORT = "/2601-Jason"; // Make sure to change this!
const API = BASE + COHORT;
// State
let puppies = [];
let SelectedPuppy;
//AsyncHandler - Replacing TryCatch with AsyncHandler for clarity and ease of use
function AsyncHandler(fn) {
return async function (...args) {
try {
return await fn(...args);
} catch (error) {
console.error("Error", error);
}
};
}
// Adds all the puppies to the puppies array under [STATE] (see above)
const getPuppies = AsyncHandler(async () => {
const response = await fetch(`${API}/players`);
const json = await response.json();
// changed const data to const json to clear things up - player data is at data.player, avoiding data.data.player
puppies = json.data.players;
});
const getPuppy = AsyncHandler(async (id) => {
const response = await fetch(`${API}/players/${id}`);
const json = await response.json();
SelectedPuppy = json.data.player;
render();
});
/*
= = = Recruit new puppies via API = = =
Instead of just calling getPuppies to refresh the full list of puppies each time we
recruit a new puppy, we can take the response, parse it with json, assign it to data and
use that data variable to push it onto the puppies array manually. This should reduce the amount
of API calls needed and make it more efficient (I think)
*/
const recruitPuppy = AsyncHandler(async (player) => {
const response = await fetch(`${API}/players`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(player),
});
const json = await response.json();
puppies.push(json.data.newPlayer);
render();
});
/*
= = = Banish a Puppy from the API-realm = = =
Again, isntead of just calling `await getPuppies();` at the end of our banishPuppy function to refresh
the puppy roster, we can specifically take the puppy we're banishing and remove him personally by
using .filter on the puppies array, and if the puppy.id >IS NOT< the puppy.id we're passing to
the banishPuppy function, then the puppy will be included in the filtered array. It will
make one less call to the API and, hopefully, be a bit more efficient.
*/
const banishPuppy = AsyncHandler(async (id) => {
await fetch(`${API}/players/${id}`, {
method: "DELETE",
});
puppies = puppies.filter((puppy) => puppy.id !== id);
render();
});
// Components
/*
= = = Show Puppy Stat Page and Mugshot on Click = = =
Clicking on the puppy will open a window that shows the relevant info from the related puppy object
*/
function PuppyRosterItem(puppy) {
const $li = document.createElement("li");
const $a = document.createElement("a");
$a.href = `#selected`;
$a.textContent = puppy.name;
$li.append($a);
$li.addEventListener("click", () => getPuppy(puppy.id));
return $li;
}
/*
= = = The Full Puppy Roster = = =
The list for our puppies, from which you can click with the above function
*/
function PuppyRoster() {
const $ul = document.createElement("ul");
$ul.className = "roster";
for (const puppy of puppies) {
const $li = PuppyRosterItem(puppy);
$ul.append($li);
}
return $ul;
}
/*
= = = Puppy Stats for the Selected Puppy = = =
First create a guard clause if no puppy is selected
Next create the innerHTML for the section
Last give the button an eventListener so we can banish puppies
*/
function PuppyStats() {
if (!SelectedPuppy) {
const $p = document.createElement("p");
$p.textContent = "Pick a Pup to View Stats";
return $p;
}
const $section = document.createElement("section");
$section.className = "puppy";
$section.innerHTML = `
<h3>${SelectedPuppy.name} #${SelectedPuppy.id}</h3>
<figure>
<img alt="${SelectedPuppy.name}" src="${SelectedPuppy.imageUrl}" />
</figure>
<p>Breed: ${SelectedPuppy.breed}</p>
<p>Status: ${SelectedPuppy.status}</p>
<button>Banish Puppy</button>
`;
const $delete = $section.querySelector("button");
$delete.addEventListener("click", () => banishPuppy(SelectedPuppy.id));
return $section;
}
/*
= = = Create a New Puppy Player = = =
First made the innerHTML for the name, breed, puppy mugshot (imageUrl).
Second addded an event listener for the "recruit puppy" button that >
Third creates a player object that includes the three inputs we gave it
Finally we call recruitPuppy function and reset the form (that we put off earlier with preventDefault)
*/
function NewPuppyForm() {
const $form = document.createElement("form");
$form.innerHTML = `
<label>
Name
<input name="name" required />
</label>
<label>
Breed
<input name="breed" required />
</label>
<label>
Puppy Mugshot URL
<input name="imageUrl" required />
</label>
<button>Recruit Puppy</button>
`;
// Need preventDefault to make sure that we don't refresh the page immediately
$form.addEventListener("submit", async (event) => {
event.preventDefault();
/*
Took a minute to figure this out using Lester's solution sheet for fullstack-gala-admin - It looks
like we're making a formData object based on the prototype of FormData($form) so that we can use
our new object to extact the values from the form fields. The API call happens when we pass 'player'
to our recruitPuppy function below. It then receieves our player data (puppy stats) and creates a
new puppy. This part is difficult to understand.
*/
const formData = new FormData($form);
const player = {
name: formData.get("name"),
breed: formData.get("breed"),
imageUrl: formData.get("imageUrl"),
};
await recruitPuppy(player);
$form.reset();
});
return $form;
}
// Render
function render() {
const $app = document.querySelector("#app");
$app.innerHTML = `
<h1>Puppy Bowl Bonanza<h1>
<main>
<section>
<h2>Puppy Roster<h2>
<PuppyRoster></PuppyRoster>
<h3>Invite Puppy to Roster</h3>
<NewPuppyForm></NewPuppyForm>
</section>
<section id="selected">
<h2>Puppy Stat Sheet</h2>
<PuppyStats></PuppyStats>
</section>
</main>
`;
$app.querySelector("PuppyRoster").replaceWith(PuppyRoster());
$app.querySelector("NewPuppyForm").replaceWith(NewPuppyForm());
$app.querySelector("PuppyStats").replaceWith(PuppyStats());
}
async function init() {
await getPuppies();
render();
}
init();