-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
279 lines (220 loc) · 12.4 KB
/
script.js
File metadata and controls
279 lines (220 loc) · 12.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//I wrote lots of notes on places where I couldn't quite get where I wanted.
//I would also appreciate any tips on how to clean this up and make it look more professional.
//I think this could be a template I could adjust for e.g. portfolios with some additional work.
//Additionally, is there a way to send things from the front end to the backend? For example, if I wanted to be able to add more Sadie pictures from the front end?
//variables
//buttons
const buttons = document.querySelectorAll(`button`)
const sadieButton = document.querySelector(`#sadie_button`)
const mannyButton = document.querySelector(`#manny_button`)
const cloudButton = document.querySelector(`#cloud_button`)
const gidgetButton = document.querySelector(`#gidget_button`)
//text on page
const title = document.querySelector(`#title`)
const animalInfo = document.querySelector(`#animal_info`)
const subhead = document.querySelector(`#subhead`)
const instructions = document.querySelector(`#instructions`)
const pettingText = document.querySelector(`#ooh`)
const goHomeText = document.querySelector(`#go_back`)
const sadieSlide = document.querySelector(`#sadie_img_slide`)
const cloudSlide = document.querySelector(`#cloud_img_slide`)
const mannySlide = document.querySelector(`#manny_img_slide`)
const gidgetSlide = document.querySelector(`#gidget_img_slide`)
//image
const animalImage = document.querySelector(`#animal_box`)
// event target and axios call on the same for-loop
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', async (event) => {
console.log(event.target.type)
let response = await axios.get(`http://localhost:3001/pets`)
console.log(response)
//if the user clicks on ANY button, hide a bunch of stuff, use pointer cursor, add text
//I can switch these to changing the display to block from none if that would be more proper
if (event.target.type == `submit`) {
sadieButton.classList.add(`hide_element`)
cloudButton.classList.add(`hide_element`)
mannyButton.classList.add(`hide_element`)
gidgetButton.classList.add(`hide_element`)
subhead.classList.add(`hide_element`)
goHomeText.innerHTML = `Or click here to choose a different furry fiend!`
goHomeText.style.cursor = `pointer`
animalImage.style.cursor = `pointer`
}
//if the user clicks on a pet, display that pet's info as called from the server
//note they all have almost the same copy so I may be able to write this in less code? I have the id params set up but not sure of a way to get
//them without still needing the independent event targets listed, so I'm not sure how much that helps. Would love to see that solution.
if (event.target.id == `sadie_button`) {
console.log(response.data[0])
animalInfo.innerHTML = `${response.data[0].name} is a ${response.data[0].age}-year-old ${response.data[0].breed} ${response.data[0].species}.
${response.data[0].pronoun} loves ${response.data[0].favoriteFood}. ${response.data[0].pronoun}'s a good ${response.data[0].sex}!`
animalImage.innerHTML = `<img src="images/sadie_starter.jpg">`
title.innerHTML = `${response.data[0].name}`
sadieSlide.innerHTML = `Click here to see more ${response.data[0].name}!`
sadieSlide.style.cursor = `pointer`
instructions.innerHTML = `Hover your mouse over ${response.data[0].name}'s picture to give ${response.data[0].obj_pronoun} a pet! Pssst, ${response.data[0].obj_pronoun} ${response.data[0].bestFeature} are ${response.data[0].obj_pronoun} best feature!`
}
if (event.target.id == `manny_button`) {
console.log(response.data[1])
animalInfo.innerHTML = `${response.data[1].name} is a ${response.data[1].age}-year-old ${response.data[1].breed} ${response.data[1].species}.
${response.data[1].pronoun} loves ${response.data[1].favoriteFood}. ${response.data[1].pronoun}'s a good ${response.data[1].sex}!`
animalImage.innerHTML = `<img src="images/manny_starter.jpeg">`
title.innerHTML = `${response.data[1].name}`
mannySlide.innerHTML = `Click here to see more ${response.data[1].name}!`
mannySlide.style.cursor = `pointer`
instructions.innerHTML = `Hover your mouse over ${response.data[1].name}'s picture to give him a pet! Pssst, ${response.data[1].obj_pronoun} ${response.data[1].bestFeature} is ${response.data[1].obj_pronoun} best feature!`
}
if (event.target.id == `gidget_button`) {
console.log(response.data[2])
animalInfo.innerHTML = `${response.data[2].name} is a ${response.data[2].age}-year-old ${response.data[2].breed} ${response.data[2].species}.
${response.data[2].pronoun} loves ${response.data[2].favoriteFood}. ${response.data[2].pronoun}'s a good ${response.data[2].sex}!`
animalImage.innerHTML = `<img src="images/gidget_starter.jpg">`
title.innerHTML = `${response.data[2].name}`
gidgetSlide.innerHTML = `Click here to see more ${response.data[2].name}!`
gidgetSlide.style.cursor = `pointer`
instructions.innerHTML = `Hover your mouse over ${response.data[2].name}'s picture to give ${response.data[2].obj_pronoun} a pet! Pssst, ${response.data[2].obj_pronoun} ${response.data[2].bestFeature} is ${response.data[2].obj_pronoun} best feature!`
}
if (event.target.id == `cloud_button`) {
console.log(response.data[3])
animalInfo.innerHTML = `${response.data[3].name} is a ${response.data[3].age}-year-old ${response.data[3].breed} ${response.data[3].species}.
${response.data[3].pronoun} loves ${response.data[3].favoriteFood}. ${response.data[3].pronoun}'s a good ${response.data[3].sex}!`
animalImage.innerHTML = `<img src="images/cloud_starter.jpg">`
title.innerHTML = `${response.data[3].name}`
cloudSlide.innerHTML = `Click here to see more ${response.data[3].name}!`
cloudSlide.style.cursor = `pointer`
instructions.innerHTML = `Hover your mouse over ${response.data[3].name}'s picture to give him a pet! Pssst, ${response.data[3].obj_pronoun} ${response.data[3].bestFeature} are ${response.data[3].obj_pronoun} best feature!`
}
}
)
}
// When you mouse over the pet picture, a heart animation plays. It stops when you mouse out.
//The image caption can change as well. I didn't care if the captions repeated twice in a row.
function petPet() {
console.log(`pet`)
let petCaptionArray = [`Ooooh, that's the spot!`, `It's been 100 years since my last tummy rub.`, `Bouncy bouncy bouncy!`, `Toy? Toy for me?`, `The rug looks better that way.`, `Cookie now?`, `💖 💖 💖`, `That poop isn't mine.`, `** happy wiggles **`, `Right there!`, `** happy thumps **`, `Scratch my ears!`, `Do I get a treat for this?`, `I didn't mean to eat your shoes.`]
const petCaption = petCaptionArray[Math.floor(Math.random() * petCaptionArray.length)]
document.getElementById("hearts_box").style.display = "block"
pettingText.innerHTML = petCaption
}
function stopPet() {
console.log(`stop petting`)
document.getElementById("hearts_box").style.display = "none"
pettingText.innerHTML = ``
// I would love to know how to target something that's inside something else with JS, like if I wanted to get an image that's inside a div and I only have an ID on the div
}
//get random images from the Sadie Image Array without repeating the same image twice. I think I was very close to getting this in an honest way.
//but I couldn't figure out how to get it to properly restart the function at else
//so I added an image that isn't in the array and reset localStorage to something that wouldn't match anything else
//which does what I want
//but I would like to have a better solution
//left some commented out stuff to show what I tried. I also tried putting an extra event listener on the text
//and calling the function on that
//which did not work
//I know I could just put the array in script.js and get this to work, but I really want to get it from the backend
// randomImage()
// function randomImage () {
sadieSlide.addEventListener(`click`, async () => {
console.log(`hi sadie`)
let response = await axios.get(`http://localhost:3001/sadie`)
const sadieImg = response.data[Math.floor(Math.random() * response.data.length)]
console.log(sadieImg)
animalImage.innerHTML = `<img src="${sadieImg}">`
//store and compare to local storage
if (sadieImg !== (localStorage.getItem(`index`))) {
animalImage.innerHTML = `<img src="${sadieImg}">`
localStorage.setItem(`index`, sadieImg)
console.log(`images don't match`)
console.log(localStorage)
}
else {
console.log(`images match`)
animalImage.innerHTML = `<img src="images/sadie_default.png">`
localStorage.setItem(`index`, 0)
}
})
// }
// get random images from the Manny Image Array
mannySlide.addEventListener(`click`, async () => {
console.log(`hi Manny`)
let response = await axios.get(`http://localhost:3001/manny`)
const mannyImg = response.data[Math.floor(Math.random() * response.data.length)]
console.log(mannyImg)
animalImage.innerHTML = `<img src="${mannyImg}">`
if (mannyImg !== (localStorage.getItem(`index`))) {
animalImage.innerHTML = `<img src="${mannyImg}">`
localStorage.setItem(`index`, mannyImg)
console.log(`images don't match`)
console.log(localStorage)
}
else {
console.log(`images match`)
animalImage.innerHTML = `<img src="images/manny_default.jpg">`
localStorage.setItem(`index`, 1)
}
})
// get random Images from the Cloud Array
cloudSlide.addEventListener(`click`, async () => {
console.log(`hi Cloud`)
let response = await axios.get(`http://localhost:3001/cloud`)
const cloudImg = response.data[Math.floor(Math.random() * response.data.length)]
console.log(cloudImg)
animalImage.innerHTML = `<img src="${cloudImg}">`
if (cloudImg !== (localStorage.getItem(`index`))) {
animalImage.innerHTML = `<img src="${cloudImg}">`
localStorage.setItem(`index`, cloudImg)
console.log(`images don't match`)
console.log(localStorage)
}
else {
console.log(`images match`)
animalImage.innerHTML = `<img src="images/cloud_default.png">`
localStorage.setItem(`index`, 2)
}
})
// get random Images from the Gidget Array
gidgetSlide.addEventListener(`click`, async () => {
console.log(`hi Gidget`)
let response = await axios.get(`http://localhost:3001/gidget`)
const gidgetImg = response.data[Math.floor(Math.random() * response.data.length)]
console.log(gidgetImg)
animalImage.innerHTML = `<img src="${gidgetImg}">`
if (gidgetImg !== (localStorage.getItem(`index`))) {
animalImage.innerHTML = `<img src="${gidgetImg}">`
localStorage.setItem(`index`, gidgetImg)
console.log(`images don't match`)
console.log(localStorage)
}
else {
console.log(`images match`)
animalImage.innerHTML = `<img src="images/gidget_default.jpg">`
localStorage.setItem(`index`, 3)
}
})
//when you click on the name of the animal, the page reverts to its former state. I am going to end up changing where you click for this to happen
function goHome() {
if (title.innerHTML !== `Furry Fiends`) {
animalImage.innerHTML = ``
animalInfo.innerHTML = ``
title.innerHTML = `Furry Fiends`
subhead.classList.remove(`hide_element`)
sadieButton.classList.remove(`hide_element`)
cloudButton.classList.remove(`hide_element`)
mannyButton.classList.remove(`hide_element`)
gidgetButton.classList.remove(`hide_element`)
sadieSlide.style.cursor = `default`
cloudSlide.style.cursor = `default`
gidgetSlide.style.cursor = `default`
mannySlide.style.cursor = `default`
animalImage.style.cursor = `default`
goHomeText.style.cursor = `default`
sadieSlide.innerHTML = ``
cloudSlide.innerHTML = ``
mannySlide.innerHTML = ``
gidgetSlide.innerHTML = ``
goHomeText.innerHTML = ``
instructions.innerHTML = ``
}
}
// event listeners
goHomeText.addEventListener(`click`, goHome)
animalImage.addEventListener(`mouseover`, petPet)
animalImage.addEventListener(`mouseout`, stopPet)