-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
139 lines (111 loc) · 3.85 KB
/
index.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
const pokeContainer = document.querySelector("#poke-container");
const pokeForm = document.querySelector("#poke-form");
const pokeFormContainer = document.querySelector("#poke-form-container");
// The following methods will fire off on page load:
// Attach an event listener to '#poke-form' when the page loads:
pokeForm.addEventListener("submit", function (event) {
event.preventDefault();
const name = document.querySelector("#name-input").value;
const img = document.querySelector("#img-input").value;
let newChar = {
id: 6, // NEEDS TO CHANGE,
name: name,
img: img,
likes: 0,
};
renderPokemon(newChar);
pokeForm.reset();
});
// Make a request to the server to retrieve and load all pokemon characters onto the DOM:
function getPokemon() {
fetch("http://localhost:3000/characters")
.then(function (resp) {
if (resp.ok) {
return resp.json();
} else {
throw new Error(`${resp.status}: ${resp.statusText}`);
}
})
.then(function (resp) {
resp.forEach(function (char) {
renderPokemon(char);
});
})
.catch(function (err) {
console.log(err);
});
}
getPokemon();
// Create a character card and append to "#poke-container":
function renderPokemon(pokemon) {
const pokeCard = document.createElement("div");
pokeCard.id = `poke-${pokemon.id}`;
pokeCard.className = "poke-card";
pokeCard.addEventListener("click", function () {
return showCharacter(pokemon);
});
const pokeImg = document.createElement("img");
pokeImg.src = pokemon.img;
pokeImg.alt = `${pokemon.name} image`;
const pokeName = document.createElement("h3");
pokeName.textContent = pokemon.name;
const pokeLikes = document.createElement("h3");
pokeLikes.textContent = "Likes: ";
const likeNum = document.createElement("h3");
likeNum.className = "likes-num";
likeNum.textContent = pokemon.likes;
const likesBttn = document.createElement("button");
likesBttn.className = "like-bttn";
likesBttn.textContent = "♥";
likesBttn.addEventListener("click", function () {
++pokemon.likes;
likeNum.textContent = pokemon.likes;
});
const deleteBttn = document.createElement("button");
deleteBttn.className = "delete-bttn";
deleteBttn.textContent = "delete";
deleteBttn.addEventListener("click", function () {
pokeCard.remove();
});
pokeCard.append(pokeImg, pokeName, pokeLikes, likeNum, likesBttn, deleteBttn);
pokeContainer.appendChild(pokeCard);
return pokeCard;
}
// The following methods will fire off when a character card gets clicked:
// Make a request to the server given a character id and return the character with associated comments. Replace the pokeContainer contents with only the clicked character card:
function showCharacter(character) {
fetch(`http://localhost:3000/characters/${character.id}`)
.then(function (resp) {
return resp.json();
})
.then(function (char) {
let pokeCard = renderPokemon(char);
pokeCard.id = "poke-show-card";
// pokeCard.dataset.id = character.id;
// loadComments(pokeCard, char);
pokeContainer.replaceChildren(pokeCard);
pokeFormContainer.replaceChildren(commentsForm());
});
}
function renderComment(comment) {
let li = document.createElement("li");
li.textContent = comment.content;
return li;
}
function commentsForm() {
let form = document.createElement("form");
form.id = "comment-form";
// attach an event listener to the #comment-form
let commentInput = document.createElement("input");
commentInput.type = "text";
commentInput.id = "comment-input";
let label = document.createElement("label");
label.className = "form-label";
label.textContent = "Leave a comment: ";
form.appendChild(label);
let submit = document.createElement("input");
submit.type = "submit";
submit.id = "submit";
form.append(commentInput, submit);
return form;
}