-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.js
118 lines (103 loc) · 3.31 KB
/
movie.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
const url = new URL(location.href);
const movieId = url.searchParams.get("id")
const movieTitle = url.searchParams.get("title")
const APILINK = 'https://review-backend.anonlegionoke.repl.co/api/v1/reviews/';
const main = document.getElementById("section");
const title = document.getElementById("title");
title.innerText = movieTitle;
const div_new = document.createElement('div');
div_new.innerHTML = `
<div class="row">
<div class="column">
<div class="card">
New Review
<p><strong>Review: </strong>
<input type="text" id="new_review" value="">
</p>
<p><strong>User: </strong>
<input type="text" id="new_user" value="">
</p>
<p><a href="#" onclick="saveReview('new_review', 'new_user')">💾</a>
</p>
</div>
</div>
</div>
`
main.appendChild(div_new)
returnReviews(APILINK);
function returnReviews(url){
fetch(url + "movie/" + movieId).then(res => res.json())
.then(function(data){
console.log(data);
data.forEach(review => {
const div_card = document.createElement('div');
div_card.innerHTML = `
<div class="row">
<div class="column">
<div class="card" id="${review._id}">
<p><strong>Review: </strong>${review.review}</p>
<p><strong>User: </strong>${review.user}</p>
<p><a href="#"onclick="editReview('${review._id}','${review.review}', '${review.user}')">✏️</a> <a href="#" onclick="deleteReview('${review._id}')">🗑</a></p>
</div>
</div>
</div>
`
main.appendChild(div_card);
});
});
}
function editReview(id, review, user) {
const element = document.getElementById(id);
const reviewInputId = "review" + id
const userInputId = "user" + id
element.innerHTML = `
<p><strong>Review: </strong>
<input type="text" id="${reviewInputId}" value="${review}">
</p>
<p><strong>User: </strong>
<input type="text" id="${userInputId}" value="${user}">
</p>
<p><a href="#" onclick="saveReview('${reviewInputId}', '${userInputId}', '${id}',)">💾</a>
</p>
`
}
function saveReview(reviewInputId, userInputId, id="") {
const review = document.getElementById(reviewInputId).value;
const user = document.getElementById(userInputId).value;
if (id) {
fetch(APILINK + id, {
method: 'PUT',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({"user": user, "review": review})
}).then(res => res.json())
.then(res => {
console.log(res)
location.reload();
});
} else {
fetch(APILINK + "new", {
method: 'POST',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({"user": user, "review": review, "movieId": movieId})
}).then(res => res.json())
.then(res => {
console.log(res)
location.reload();
});
}
}
function deleteReview(id) {
fetch(APILINK + id, {
method: 'DELETE'
}).then(res => res.json())
.then(res => {
console.log(res)
location.reload();
});
}