-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.js
More file actions
329 lines (313 loc) · 14 KB
/
read.js
File metadata and controls
329 lines (313 loc) · 14 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
const recommend_btn = document.getElementById('recommend_btn');
const recommend_count = document.getElementById('recommend_count');
const create_comment_btn = document.getElementById('create_comment_btn');
const comment_body = document.querySelector("[name='comment_body']");
const url_str = window.location.href;
const words = url_str.split('/');
const content_id = words[4].split('?')[0] || words[4];
window.onload = function checkAuthorization() { // Always execute this function when page loaded.
fetch(`/board/${content_id}/auth`, {
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
}
})
.then((res) => res.json())
.then((res) => {
if (res.code === 200 && res.message === 'authorized') {
$('div[name="method_div"]').append(owner);
} else if (res.code === 401 && res.message === 'unauthorized') {
$('div[name="method_div"]').append();
} else if (res.code === 419) { // Access Token has expired.
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
window.addEventListener('load', function() {
fetch(`/board/${content_id}/recommand`, {
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
}
})
.then((res) => res.json())
.then((res) => {
if(res.code == 200) {
if (res.message === "created") {
recommend_btn.className = "btn btn-danger"
recommend_btn.textContent = "추천취소"
} else if(res.message === "deleted") {
recommend_btn.className = "btn btn-success"
recommend_btn.textContent = "추천하기"
}
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
});
function delete_click() {
if (confirm("Are you sure want to delete this post?")) {
fetch(`/board/${content_id}`, {
method: "DELETE",
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
},
})
.then((res) => res.json())
.then((res) => {
if (res.code === 200) {
alert("Delete post success.");
location.href = "/board";
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
} else { alert("Delete post cancle."); }
}
function recommand_click() {
if(!localStorage.getItem('access_token')) {
alert('Please login first.');
location.href = "/user/login";
} else {
fetch(`/board/${content_id}/recommand`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
}
})
.then((res) => res.json())
.then((res) => {
if (res.code === 200 && res.message === "create") {
alert("Recommand success.");
location.herf = `/board/${content_id}<%= getPostQueryString() %>`
recommend_btn.className = "btn btn-danger"
recommend_btn.textContent = "추천취소"
recommend_count.textContent = res.data.recommand;
} else if (res.code === 200 && res.message === "delete") {
alert("Recommand cancle success.");
location.herf = `/board/${content_id}<%= getPostQueryString() %>`
recommend_btn.className = "btn btn-success"
recommend_btn.textContent = "추천하기"
recommend_count.textContent = res.data.recommand;
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
}
function create_comment_post() {
if(!localStorage.getItem('access_token')) {
alert('Please login first.');
location.href = "/user/login";
}
if(!comment_body.value) return alert("Please input comment.");
let new_comment = comment_body.value;
fetch(`/board/${content_id}/comment`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
},
body: JSON.stringify({ comment: new_comment })
})
.then((res) => res.json())
.then((res) => {
if (res.code === 201) {
alert("Create comment success.");
location.herf = `/board/${content_id}`
location.reload();
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
// 댓글 더보기. 댓글 개수를 가져와서 불러올 페이지를 계산하여 추가 댓글을 가져온다. 댓글은 5개 단위이다.
function more_comment_post() {
// class이름이 comment 인 요소들의 개수를 가져온다
let comment_count = document.getElementsByClassName("comment").length;
let comment_page = Math.ceil(comment_count / 5) + 1;
fetch(`/board/${content_id}/comment/${comment_page}`, {
method: "GET",
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
}
})
.then((res) => res.json())
.then((res) => {
if (res.code === 200) {
let comments = res.data;
// comments.more이 false 면 클래스가 more인 요소 숨김
if (!comments.more) {
// 클래스가 more인 요소를 가져온다
let more_btn = document.getElementsByClassName("more")[0];
more_btn.style.display = "none";
}
let comment_list = comments.rows;
// 가져온 comments를 순회하며 댓글을 추가한다.
comment_list.forEach((comment) => {
let comment_div = document.createElement("div");
let create_at = new Date(comment.created_at);
create_at.setHours(create_at.getHours() + 9);
comment_div.innerHTML = `
<div class="card comment">
<div>
<img src="${ comment.User.profile }" class="rounded-circle" alt="..." width=25 height=25>
<span class="name">${ comment.User.user_name }</span>
<button class="btn btn-secondary delete" type="button" onclick="delete_comment_post(${ comment.id })">✖</button>
<span class="date">
${ create_at.toISOString().replace('T', ' ').substring(0, 19) }
</span>
</div>
<div class="body">${comment.comment}</div>
</div>`
// 클래스가 comment 인 마지막 요소 뒤에 추가한다
let comment_list = document.getElementsByClassName("comment");
comment_list[comment_list.length - 1].after(comment_div);
});
} else if (res.code === 401) {
alert('If you want to see more comments, please login first.');
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
function delete_comment_post(comment_id) {
fetch(`/board/${content_id}/comment/${comment_id}`, {
method: "DELETE",
headers: {
'Content-Type': 'application/json',
'authorization': localStorage.getItem('access_token')
}
})
.then((res) => res.json())
.then((res) => {
if (res.code === 200) {
alert("Delete comment success.");
location.herf = `/board/${content_id}`
location.reload();
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
if(res.message === "unauthorized") {
alert("Only the author of the comment can delete it.");
} else {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/user/login";
}
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}