-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
270 lines (173 loc) · 6.96 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
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
document.addEventListener('DOMContentLoaded', () => {
//url
const url= "http://localhost:3000/movies"
//first fetch const top 5 section
const top5Section= document.getElementById("top-5")
//second fetch const details section
const detailImg= document.querySelector("#movie-details-img")
const detailTitle= document.querySelector("#title")
const detailYear= document.querySelector("#release-year")
const detailRating= document.querySelector("#rating")
const detailSales= document.querySelector("#sales")
//user favorites const
const userFavesSection= document.querySelector("#user-favorites")
const newMovieForm= document.querySelector("#new-movie-form")
//form const
const userNameForm= document.querySelector("#user-name")
const movieImgForm= document.querySelector("#movie-image")
const movieTitleForm= document.querySelector("#movie-title")
const releaseYearForm= document.querySelector("#new-release-year")
const movieRatingForm= document.querySelector("#movie-rating")
const userName= document.querySelector("#movie-details h4")
//smash trash button const
const smashBtn= document.querySelector("#smash")
const trashBtn= document.querySelector("#trash")
let smashNumber= document.querySelector("#smash-span")
let trashNumber= document.querySelector("#trash-span")
//comment form const
const newCommentForm= document.querySelector("#new-comment-form")
const newCommentDetails= document.querySelector("#new-comment-details")
//fetching the entire db.json object
//fetch function
function getMovies() {
fetch(url)
.then((res) => res.json())
.then (movies => {
movies.map(movie => {
renderMovie(movie) // going through the conditional to determine next steps
//topFiveMovies(movie) //we no longer need this because renderMovie(movie) is going to do this
})
})
}
//invoke this fetchMovies function
getMovies()
//if else
function renderMovie(movie) {
if (movie.id < 6){
topFiveMovies(movie)
} else if (movie.id >= 6) {
userFaves(movie)
}
}
//populate top 5 movies on left
function topFiveMovies(movie){
const top5Img= document.createElement("img")
top5Img.src= movie.image
top5Section.appendChild(top5Img)
top5Img.addEventListener("click", (e) => {
renderDetails(movie)
})
}
//populate user favorites on the right
function userFaves(newMovie){
const favesImg= document.createElement("img")
favesImg.src= newMovie.image
userFavesSection.appendChild(favesImg)
favesImg.addEventListener("click", (e) => {
if (e.altKey){ //permits removal
//favesImg.remove()
deleteMovie(newMovie)
} else {
renderDetails(newMovie)
}
//renderDetails(newMovie)
})
}
//removal function
function deleteMovie(movie){
fetch(`${url}/${movie.id}`, {
method: "DELETE"
})
}
//function for add event listener
//w parameter of "placement" aka top5 being passed through as param, goes to wherever top 5 should go etc.
function renderDetails(movie){
detailImg.src= movie.image
detailTitle.textContent= movie.title
detailYear.textContent= movie.year
detailRating.textContent= `${movie.rating}/10`
detailSales.textContent= movie.sales //ok bc doesn't EXIST in the faves
userName.textContent= movie.name? `@${movie.name}` : "" //does movie.name exist? if true, this is the movie name, if false, nothing.
smashNumber.textContent= movie.smash
trashNumber.textContent= movie.trash
}
//fetch for single object parameter of large object
//function for fetch
function getOneMovie(){
return fetch(`${url}/5`)
.then((res) => res.json())
.then (movie => {
renderDetails(movie)
buttons(movie)
})
}
//invoke getOneMovie function
getOneMovie()
//user buttons
//do we want these here? this works on it's own
function buttons(){
smashBtn.addEventListener("dblclick", (e) => {
smashNumber.textContent= Number(smashNumber.textContent)+1
})
trashBtn.addEventListener("dblclick", (e) => {
trashNumber.textContent= Number(trashNumber.textContent)+1
})
}
//new movie form
newMovieForm.addEventListener("submit", (e) => {
e.preventDefault()
let newMovieObj= {
name: e.target["user-name"].value, //can use the name attribute to reference this area when there are no id's
image: movieImgForm.value,
title: movieTitleForm.value,
year: releaseYearForm.value,
rating: movieRatingForm.value,
smash: 0,
trash: 0,
comments: []
}
userFaves(newMovieObj)
e.target["user-name"].value= "" //can use the name attribute to reference this area when there are no id's
movieImgForm.value= ""
movieTitleForm.value= ""
releaseYearForm.value= ""
movieRatingForm.value= ""
//post invocation in here because we want to POST the contents of these instructions
post(newMovieObj)
})
newCommentForm.addEventListener("submit", (e) => {
e.preventDefault()
let newCommentLi = document.createElement("li")
newCommentLi.textContent= e.target["new-comment"].value
newCommentDetails.appendChild(newCommentLi)
e.target["new-comment"].value= ""
//handling patch
// newCommentLi.addEventListener("change", (e) => {
// handlePatch(e, newCommentLi)
// })
})
// function patch(url, body){
// return fetch(url, {
// method: "PATCH",
// headers: {"content-type": "application/json"},
// body: JSON.stringify(body)
// })
// .then(res => res.json())
// }
// function handlePatch(e, movies){
// e.preventDefault()
// patch(`${url}/${movies.id}`, {comments: e.target.value})
// .then(data => console.log(data))
// }
//persistence
//POST
//this ensures what the user submits, STAYS and is ADDED to the db.json object
function post(movie){
fetch(url, {
method: "POST",
headers: {"content-type": "application/json"},
body: JSON.stringify(movie)
})
}
//end of DOM Content Loaded event
})