drf ๋ฐฑ์๋๊ฐ๋ฐ + ํ๋ก ํธ๊ฐ๋ฐ
: ์ด๋ฒ ํ๋ก์ ํธ๋ ํํ๋ก์ ํธ๋ก ์์ํฉ๋๋ค.
const url = window.location.search.split('=')
const obj_id = url[1]
// ๊ฒ์๋ฌผ ์์ธ ํ์ด์ง ๋ถ๋ฅด๊ธฐ
window.onload = async function articleDetail() {
let articleDetail = async () => {
let response = await fetch(`${backend_base_url}/article/${obj_id}/detail/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
// ๋ก๊ทธ์ธ ๋ก๊ทธ์์ ํ์๊ฐ์
๋ฒํผ ์จ๊ธฐ๊ธฐ
if (!localStorage.getItem("access")) {
let logout_button = document.getElementById("logout-button")
let my_buttons = document.getElementById("my-buttons")
logout_button.style.visibility = "hidden"
my_buttons.style.visibility = "hidden"
}
else {
let login_button = document.getElementById("login-button")
let signup_button = document.getElementById("signup-button")
login_button.style.visibility = "hidden"
signup_button.style.visibility = "hidden"
}
if (!localStorage.getItem("access")) {
let my_buttons = document.getElementById("my-buttons")
my_buttons.style.visibility = "hidden"
}
response_json = await response.json()
return response_json.articles
}
// ๊ฒ์๋ฌผ ์์ธ ๋ด์ฉ
articleDetail().then((data) => {
detail = response_json
console.log(detail)
let nickname = detail['nickname']
let image = detail['article_image']
let title = detail['article_title']
let contents = detail['article_contents']
let date = detail['article_post_date']
let count_fox = detail['vote']['fox']
let count_green = detail['vote']['green']
let count_miss = detail['vote']['miss']
if (image == null ) {
let temp_html =
`<div class="titlediv">
<div class="writeinfo">
<div><a>${nickname} - ${date}</a></div>
<div class="articlebuttons">
<p class="comment-modify">์์ </p>
<p class="comment-delete" onclick="removeArticle()">์ญ์ </p>
</div>
</div>
<div class="title"><h2>${title}</h2></div>
</div>
</div>
<div class="contentdiv">
<h3 class="content">${contents}</h3>
</div>
<div>
<div class="botediv">
<div class="boteb"><button type="button" class="bote" onclick="vote1()">๐ฆ</button><p class="btext1">ํญ์ค์
๋๋ค (${count_fox})</p></div>
<div class="boteb"><button type="button" class="bote" onclick="vote2()">๐</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํธ (${count_green})</p></div>
<div class="boteb"><button type="button" class="bote" onclick="vote3()">๐</button><p class="btext3">์คํด์
๋๋ค (${count_miss})</p></div>
</div>
</div>`
$('#article-detail-box').prepend(temp_html)
} else{
let temp_html =
`<div class="titlediv">
<div class="writeinfo">
<div><a>${nickname} - ${date}</a></div>
<div class="articlebuttons">
<p class="comment-modify">์์ </p>
<p class="comment-delete" onclick="removeArticle()">์ญ์ </p>
</div>
</div>
<div class="title"><h2>${title}</h2></div>
</div>
</div>
<div class="contentdiv">
<img src="http://127.0.0.1:8000${image}" alt="" />
<h3 class="content">${contents}</h3>
</div>
<div>
<div class="botediv">
<div class="boteb"><button type="button" class="bote" onclick="vote1()">๐ฆ</button><p class="btext1">ํญ์ค์
๋๋ค (${count_fox})</p></div>
<div class="boteb"><button type="button" class="bote" onclick="vote2()">๐</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํธ (${count_green})</p></div>
<div class="boteb"><button type="button" class="bote" onclick="vote3()">๐</button><p class="btext3">์คํด์
๋๋ค (${count_miss})</p></div>
</div>
</div>`
$('#article-detail-box').prepend(temp_html)
}
for (let i = 0; i < detail['comment_set'].length; i++) {
let comments = detail['comment_set'][i]['comment_contents']
let comment_id = detail['comment_set'][i]['id']
let nickname = detail['comment_set'][i]['nickname']
let comment_created_at = detail['comment_set'][i]['comment_created_at']
let comment_like_count = detail['comment_set'][i]['count']
let temp_html =
`<div class="comments">
<div class="cowriteinfo">
<div><p class="cowriter">${nickname}</p></div>
<div><p class="cotime">${comment_created_at}</p></div>
<p class="comment-modify" id="#">์์ </p>
<p class="comment-delete" id="#">์ญ์ </p>
</div>
<div class="commentdetail">
<div class="comment"><h4>${comments}</h4></div>
<div style="display:flex; flex-direction:row">
<button type="button" id="like" class="like" onclick="likeButton(${comment_id})"><i class="fa-regular fa-heart fa-2x"></i></button>
<button type="button" id="reallike" class="reallike" onclick="likeButton(${comment_id})"><i class="fa-solid fa-heart fa-2x"></i></button>
<h5>${comment_like_count}</h5>
</div>
</div>
</div>`
$('#comments-box').prepend(temp_html)
}
}
)
}
/// ๊ฒ์๋ฌผ ์์
async function getUpdateData() {
let contents = document.getElementById("contents-update").value
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
console.log(contents)
updateArticle(contents)
}
// ๊ฒ์๋ฌผ ์ญ์
async function removeArticle() {
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
await deleteArticle(obj_id)
window.location.replace(`${fronted_base_url}/index.html`)
}
// ๋๊ธ ์์ฑ
async function commentCreate() {
let comment_contents = document.getElementById("wcomment").value
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
else if (comment_contents == ''){
alert('๋๊ธ์ ์์ฑํด์ฃผ์ธ์!')
}
else {
let comment_data = {
comment_contents: comment_contents,
}
let response = await fetch(`${backend_base_url}/article/${obj_id}/comment/`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
},
body: JSON.stringify(comment_data)
})
response_json = await response.json()
window.location.reload()
}
}
// ํญ์ค ํฌํ
async function vote1() {
let category = {
category: "ํญ์ค์
๋๋ค",
}
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
const response = await fetch(`${backend_base_url}/article/${obj_id}/article/vote/`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
},
body: JSON.stringify(category)
}
)
response_json = await response.json()
window.location.reload()
}
// ๊ทธ๋ฆฐ๋ผ์ดํธ ํฌํ
async function vote2() {
let category = {
category: "๊ทธ๋ฆฐ๋ผ์ดํธ",
}
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
const response = await fetch(`${backend_base_url}/article/${obj_id}/article/vote/`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
},
body: JSON.stringify(category)
}
)
response_json = await response.json()
window.location.reload()
}
// ์คํด ํฌํ
async function vote3() {
let category = {
category: "์คํด์
๋๋ค",
}
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
const response = await fetch(`${backend_base_url}/article/${obj_id}/article/vote/`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
},
body: JSON.stringify(category)
}
)
response_json = await response.json()
window.location.reload()
}
// ๋๊ธ ๊ณต๊ฐ ํฌํ
async function likeButton(comment_id) {
let data = {
category: "๊ณต๊ฐ",
}
if (!localStorage.getItem("access")) {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์!')
}
const response = await fetch(`${backend_base_url}/article/${comment_id}/comment/like/`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
},
body: JSON.stringify(data)
}
)
response_json = await response.json()
window.location.reload()
}