๐Ÿ“’ [ TIL ] 2022.07.25_68์ผ์ฐจ # ์ตœ์ข… ํ”„๋กœ์ ํŠธ (10)

๋ฌธ๋ช…์ฃผยท2022๋…„ 7์›” 26์ผ
1
post-thumbnail

[ 2022-07-25 (์›”) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

drf ๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ + ํ”„๋ก ํŠธ๊ฐœ๋ฐœ
: ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๋ฏธ๋กœ๊ทธ์ธ์‹œ ๊ณต๊ฐํˆฌํ‘œ/๊ฒŒ์‹œ๋ฌผ์ˆ˜์ •์‚ญ์ œ ๋กœ๊ทธ์ธํ•„์š” alert

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

  • ๋ฏธ๋กœ๊ทธ์ธ์‹œ ๊ณต๊ฐํˆฌํ‘œ/๊ฒŒ์‹œ๋ฌผ์ˆ˜์ •์‚ญ์ œ ๋กœ๊ทธ์ธํ•„์š” alert
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()
}
profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด