-
Notifications
You must be signed in to change notification settings - Fork 0
/
PracticeScript1.js
148 lines (126 loc) · 5.8 KB
/
PracticeScript1.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
"use strict";
const serverUrl = "https://allison-oscar-movie-project-assigment.glitch.me/movies"
setTimeout(fade_out, 2000);
setTimeout(fade_in, 2000);
function fade_out() {
$("#load").fadeOut().empty();
}
function fade_in() {
$(fad).fadeIn().empty();
}
function fad () {
function AJAX(url, method = "GET", data) {
const options = {
method: method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
return fetch(url, options)
.then(res => res.json())
.then(responseData => responseData)
.catch(err => err)
}
AJAX(serverUrl)
.then(data => console.log(data))
let localMovies = [];
function movieData() {
$.get(`https://allison-oscar-movie-project-assigment.glitch.me/movies`, {
dataType: "json",
}).done(function (movies) {
localMovies = movies;
var html = "";
$("#display1").empty();
movies.forEach(function (movie) {
console.log("hello")
html = `<div id = "full"
<div class="card">
<p id = "movie-title-${movie.id}">Title: ${movie.title}</p>
<hr>
<img id = "movie-poster-${movie.id}" src= "${movie.poster}" alt="image" height="400px" width="300px">
<hr>
<p id = "movie-rating-${movie.id}">Rating: ${movie.rating}</p>
<hr>
<p id = "movie-desc-${movie.id}">Description: ${movie.plot}</p>
<hr>
<p id = "movie-year-${movie.id}">Year: ${movie.year}</p>
<hr>
<p id = "movie-director-${movie.id}">Director: ${movie.director}</p>
<hr>
<p id = "movie-genre-${movie.id}">Genre: ${movie.genre}</p>
<hr>
<input name="submit" type="submit" value = "edit" data-id=${movie.id} class= "editMovie">
<input name="submit" type="submit" value = "delete" data-id=${movie.id} class= "deleteMovie">
</div>`
$('#display1').append(html);
})
})
}
movieData(serverUrl, "GET")
$(function () {
$("#submit_movie").click(function (event) {
event.preventDefault();
$.ajax(serverUrl, {
type: 'POST',
data: {
title: $("#movieName").val(),
rating: $("#movieRating").val(),
plot: $("#movieDes").val()
}
}).done(function (data) {
$("#movieName").val("")
$("#movieRating").val("")
$("#movieDes").val("")
})
});
});
$(function clickEdit() {
$(document).on('click', '.editMovie', function (event) {
// $(".editMovie").click( function (event) {
event.preventDefault();
var movieId = $(this).attr("data-id")
const currentMovie = localMovies.filter(function (movie) {
return movie.id == movieId
})[0];
console.log(currentMovie)
let formToEdit = `
<form>
<label for="movieNameToEdit${movieId}"></label><input type="text" value = "${currentMovie.title}" name="movieNameToEdit${movieId}" id="movieNameToEdit${movieId}" placeholder="Name">
<label for="movieRatingToEdit${movieId}"></label><input type="text" value = "${currentMovie.rating}" name="movieRatingToEdit${movieId}" id="movieRatingToEdit${movieId}" placeholder="Rating">
<label for="movieDesToEdit${movieId}"></label><input type="text" value = "${currentMovie.plot}" name="movieDesToEdit${movieId}" id="movieDesToEdit${movieId}" placeholder="Description">
<label for="movieYearToEdit${movieId}"></label><input type="text" value = "${currentMovie.year}" name="movieDesToEdit${movieId}" id="movieYearToEdit${movieId}" placeholder="Year">
<label for="movieDirToEdit${movieId}"></label><input type="text" value = "${currentMovie.director}" name="movieDesToEdit${movieId}" id="movieDirToEdit${movieId}" placeholder="Director">
<label for="movieGenreToEdit${movieId}"></label><input type="text" value = "${currentMovie.genre}" name="movieGenreToEdit${movieId}" id="movieGenreToEdit${movieId}" placeholder="Genre">
<input name="saveChanges${movieId}" type="submit" value="Submit" id="submit_movie${movieId}">
</form>`
$(this).parent().html(formToEdit);
$(`#submit_movie${movieId}`).click(function (event) {
event.preventDefault()
const newMovieObject = {
title: $(`#movieNameToEdit${movieId}`).val(),
rating: $(`#movieRatingToEdit${movieId}`).val(),
plot: $(`#movieDesToEdit${movieId}`).val(),
year: $(`#movieYearToEdit${movieId}`).val(),
director: $(`#movieDirToEdit${movieId}`).val(),
genre: $(`#movieGenreToEdit${movieId}`).val()
}
$.ajax(serverUrl + "/" + movieId, {
type: 'PATCH',
data: newMovieObject
}).done(movieData)
})
});
$(document).on('click', '.deleteMovie', function (event) {
event.preventDefault();
var movieId = $(this).attr("data-id")
const currentMovie = localMovies.filter(function (movie) {
return movie.id == movieId
})[0];
$.ajax(serverUrl + "/" + movieId, {
type: 'DELETE',
data: currentMovie
}).done(movieData)
})
})
}