๐Ÿ“’ [ TIL ] 2022.08.11_81์ผ์ฐจ # ์ตœ์ข… ํ”„๋กœ์ ํŠธ ๊ฐœ์„ (5)

๋ฌธ๋ช…์ฃผยท2022๋…„ 8์›” 12์ผ
0
post-thumbnail

[ 2022-08-11 (๋ชฉ) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

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

[ Today Learn ]

  • ํ”ผ๋“œ๋ฐฑ ๋ฐ›์€๋ถ€๋ถ„ ๊ฐœ์„ ํ•˜๊ธฐ
    1) ํšŒ์›๊ฐ€์ž…์‹œ ๋ฉ”์ธํŽ˜์ด์ง€ ์ด๋™
    2) ๊ฒ€์ƒ‰๊ฒฐ๊ณผ ์—†์„์‹œ ์•ˆ๋‚ด๋ฌธ๊ตฌ
    3) ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •์‹œ ๊ธฐ์กด ๋‚ด์šฉ ๋„์šฐ๊ธฐ

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

* article_detail.js

const url = window.location.search.split('=')
const obj_id = url[1]

// #๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

// ๊ฒŒ์‹œ๋ฌผ ์ƒ์„ธ ํŽ˜์ด์ง€ ๋ถ€๋ฅด๊ธฐ
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',

            },
        })

    const openButton = document.getElementById("modalclick");
    const modal = document.querySelector(".side_modal");
    const overlay = modal.querySelector(".side_modal__overlay");
    const closeBtn = modal.querySelector(".button");
    const openModal = () => {
        modal.classList.remove("hidden");
    }
    const closeModal = () => {
        modal.classList.add("hidden");
    }
    overlay.addEventListener("click", closeModal);
    closeBtn.addEventListener("click", closeModal);
    openButton.addEventListener("click", openModal);

        
        // ๋กœ๊ทธ์ธ ๋กœ๊ทธ์•„์›ƒ ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ ์ˆจ๊ธฐ๊ธฐ
        if (!localStorage.getItem("access")) {
            let my_buttons = document.getElementById("my-buttons")
            my_buttons.style.display = "none"

        }
        else {
            let login_button = document.getElementById("login-button")
            let signup_button = document.getElementById("signup-button")
            login_button.style.display = "none"
            signup_button.style.display = "none"
        }
        
        response_json = await response.json()
        return response_json.articles

    }
    // ๊ฒŒ์‹œ๋ฌผ ์ƒ์„ธ ๋‚ด์šฉ
    articleDetail().then((data) => {
        detail = response_json
        if (localStorage.getItem("access")) {
            const payload = localStorage.getItem("payload")
            const words = payload.split(':');
            id = words[5].split(',')

            let login_id = id[0]
            let article_author = detail['article_author']
            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 && login_id != article_author) {
                let temp_html =
                    `<div class="titlediv">
                    <div class="writeinfo">
                        <div><a>${nickname} - ${date}</a></div>
                    </div>
                    <div id="title" class="title"><h2>${title}</h2></div>
                </div>
            </div>
            <div class="contentdiv">
                <h5 id="content" class="content">${contents}</h5>
            </div>
            <div>
            <div class="botediv">
                <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’”</button><p class="btext3">์˜คํ•ด์ž…๋‹ˆ๋‹ค (${count_miss})</p></div>
            </div>
            </div>`
                $('#article-detail-box').prepend(temp_html)
            } else if (image == null && login_id == article_author){
                let temp_html =
                    `
                    <!-- ๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ • ๋ชจ๋‹ฌ -->
                    <div class="modal fade" id="staticBackdrop1" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
                        aria-labelledby="staticBackdropLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="staticBackdropLabel">๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ •</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    ์ œ๋ชฉ<br>
                                    <textarea type="text" class="modal-textinput" id="title-update">${title}</textarea>
                                    ๋‚ด์šฉ์ž…๋ ฅ<br>
                                    <textarea class="modal-textarea" id="contents-update">${contents}</textarea>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">์ทจ์†Œ</button>
                                    <button type="button" class="btn btn-primary">์ˆ˜์ •ํ•˜๊ธฐ</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="articlebuttons" id="article-buttons">
                        <p class="comment-modify"  data-bs-toggle="modal" data-bs-target="#staticBackdrop1">๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •</p>
                        <p class="comment-delete">์‚ญ์ œ</p>
                    </div>
                    <div class="titlediv">
                    <div class="writeinfo">
                    <div><a>${nickname} - ${date}</a></div>
                </div>
                <div class="title"><h2>${title}</h2></div>
                </div>
            </div>
            <div class="contentdiv">
                <h5 class="content">${contents}</h5>
            </div>
            <div>
            <div class="botediv">
                <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’”</button><p class="btext3">์˜คํ•ด์ž…๋‹ˆ๋‹ค (${count_miss})</p></div>
            </div>
            </div>`
                $('#article-detail-box').prepend(temp_html)
            } else if (image != null && login_id != article_author){
                let temp_html =
                    `<div class="titlediv">
                    <div class="writeinfo">
                    <div><a>${nickname} - ${date}</a></div>
                </div>
                <div class="title"><h2>${title}</h2></div>
                </div>
            </div>
            <div class="contentdiv">
                <img src="${backend_base_url}${image}" alt="" />
                <h5 class="content">${contents}</h5>
            </div>
            <div>
            <div class="botediv">
                <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’”</button><p class="btext3">์˜คํ•ด์ž…๋‹ˆ๋‹ค (${count_miss})</p></div>
            </div>
            </div>`
                $('#article-detail-box').prepend(temp_html)
            } else if (image != null && login_id == article_author){
                let temp_html =
                    `
                    <!-- ๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ • ๋ชจ๋‹ฌ -->
                    <div class="modal fade" id="staticBackdrop1" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
                        aria-labelledby="staticBackdropLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="staticBackdropLabel">๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ •</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    ์ œ๋ชฉ<br>
                                    <textarea type="text" class="modal-textinput" id="title-update">${title}</textarea>
                                    ๋‚ด์šฉ์ž…๋ ฅ<br>
                                    <textarea class="modal-textarea" id="contents-update">${contents}</textarea>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">์ทจ์†Œ</button>
                                    <button type="button" class="btn btn-primary">์ˆ˜์ •ํ•˜๊ธฐ</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="articlebuttons" id="article-buttons">
                        <p class="comment-modify" data-bs-toggle="modal" data-bs-target="#staticBackdrop1">๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •</p>
                        <p class="comment-delete">์‚ญ์ œ</p>
                    </div>
                <div class="titlediv">
                    <div class="writeinfo">
                    <div><a>${nickname} - ${date}</a></div>
                </div>
                <div class="title"><h2>${title}</h2></div>
                </div>
            </div>
            <div class="contentdiv">
                <img src="${backend_base_url}${image}" alt="" />
                <h5 class="content">${contents}</h5>
            </div>
            <div>
            <div class="botediv">
                <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’”</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 login_id = id[0]
                let comment_author = detail['comment_set'][i]['comment_author']

                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']
                if (comment_like_count == 0 && login_id != comment_author) {
                    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>
                        </div>
                        <div class="commentdetail">
                            <div class="comment"><h6>${comments}</h6></div>
                            <div style="display:flex; flex-direction:row; align-items:center">
                            <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="heart"token interpolation">${comment_id})"height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M16.5 3c-1.74 0-3.41.81-4.5 2.09C10.91 3.81 9.24 3 7.5 3 4.42 3 2 5.42 2 8.5c0 3.78 3.4 6.86 8.55 11.54L12 21.35l1.45-1.32C18.6 15.36 22 12.28 22 8.5 22 5.42 19.58 3 16.5 3zm-4.4 15.55l-.1.1-.1-.1C7.14 14.24 4 11.39 4 8.5 4 6.5 5.5 5 7.5 5c1.54 0 3.04.99 3.57 2.36h1.87C13.46 5.99 14.96 5 16.5 5c2 0 3.5 1.5 3.5 3.5 0 2.89-3.14 5.74-7.9 10.05z"/></svg>
                            <h5 style="margin:0; display:block">${comment_like_count}</h5>
                            </div>
                        </div>
                    </div>`
                $('#comments-box').prepend(temp_html)
            } else if (comment_like_count == 0 && login_id == comment_author){
                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-delete"token interpolation">${comment_id})"id="#">์‚ญ์ œ</p>
                        </div>
                        <div class="commentdetail">
                            <div class="comment"><h6>${comments}</h6></div>
                            <div style="display:flex; flex-direction:row; align-items:center">
                            <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="heart"token interpolation">${comment_id})"height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M16.5 3c-1.74 0-3.41.81-4.5 2.09C10.91 3.81 9.24 3 7.5 3 4.42 3 2 5.42 2 8.5c0 3.78 3.4 6.86 8.55 11.54L12 21.35l1.45-1.32C18.6 15.36 22 12.28 22 8.5 22 5.42 19.58 3 16.5 3zm-4.4 15.55l-.1.1-.1-.1C7.14 14.24 4 11.39 4 8.5 4 6.5 5.5 5 7.5 5c1.54 0 3.04.99 3.57 2.36h1.87C13.46 5.99 14.96 5 16.5 5c2 0 3.5 1.5 3.5 3.5 0 2.89-3.14 5.74-7.9 10.05z"/></svg>
                            <h5 style="margin:0; display:block">${comment_like_count}</h5>
                            </div>
                        </div>
                    </div>`
                $('#comments-box').prepend(temp_html)
            } else if (comment_like_count != 0 && login_id == comment_author){
                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-delete"token interpolation">${comment_id})"id="#">์‚ญ์ œ</p>
                        </div>
                        <div class="commentdetail">
                            <div class="comment"><h6>${comments}</h6></div>
                            <div style="display:flex; flex-direction:row; align-items:center">
                            <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="hearts"token interpolation">${comment_id})" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/></svg>
                            <h5 style="margin:0; display:block">${comment_like_count}</h5>
                            </div>
                        </div>
                    </div>`
                $('#comments-box').prepend(temp_html)

            } else if (comment_like_count != 0 && login_id != comment_author){
                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>
                        </div>
                        <div class="commentdetail">
                            <div class="comment"><h6>${comments}</h6></div>
                            <div style="display:flex; flex-direction:row; align-items:center">
                            <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="hearts"token interpolation">${comment_id})" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/></svg>
                            <h5 style="margin:0; display:block">${comment_like_count}</h5>
                            </div>
                        </div>
                    </div>`
                $('#comments-box').prepend(temp_html)
                


                $(document).on("click", ".staticBackdropLabel", function () {
                    var comment = $(this).data('id');
                    $(`.modal-footer #comment-update${comment_id}`).val( comment );
            });

            }
        
    }}  else if (!localStorage.getItem("access")){
            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>
                    <div class="title"><h2>${title}</h2></div>
                </div>
            </div>
            <div class="contentdiv">
                <h5 class="content">${contents}</h5>
            </div>
            <div>
            <div class="botediv">
                <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
                <div class="boteb"><button type="button" class="bote">๐Ÿ’”</button><p class="btext3">์˜คํ•ด์ž…๋‹ˆ๋‹ค (${count_miss})</p></div>
            </div>
            </div>`
            $('#article-detail-box').prepend(temp_html)
    } else if (image != null) {
        let temp_html =
        `
    <div class="titlediv">
        <div class="writeinfo">
        <div><a>${nickname} - ${date}</a></div>
    </div>
    <div class="title"><h2>${title}</h2></div>
    </div>
</div>
<div class="contentdiv">
    <img src="${backend_base_url}${image}" alt="" />
    <h5 class="content">${contents}</h5>
</div>
<div>
<div class="botediv">
    <div class="boteb"><button type="button" class="bote">๐ŸฆŠ</button><p class="btext1">ํญ์Šค์ž…๋‹ˆ๋‹ค (${count_fox})</p></div>
    <div class="boteb"><button type="button" class="bote">๐Ÿ’š</button><p class="btext2">๊ทธ๋ฆฐ๋ผ์ดํŠธ (${count_green})</p></div>
    <div class="boteb"><button type="button" class="bote">๐Ÿ’”</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']
        if (comment_like_count == 0) {
            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>
                </div>
                <div class="commentdetail">
                    <div class="comment"><h6>${comments}</h6></div>
                    <div style="display:flex; flex-direction:row; align-items:center">
                    <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="heart"token interpolation">${comment_id})"height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M16.5 3c-1.74 0-3.41.81-4.5 2.09C10.91 3.81 9.24 3 7.5 3 4.42 3 2 5.42 2 8.5c0 3.78 3.4 6.86 8.55 11.54L12 21.35l1.45-1.32C18.6 15.36 22 12.28 22 8.5 22 5.42 19.58 3 16.5 3zm-4.4 15.55l-.1.1-.1-.1C7.14 14.24 4 11.39 4 8.5 4 6.5 5.5 5 7.5 5c1.54 0 3.04.99 3.57 2.36h1.87C13.46 5.99 14.96 5 16.5 5c2 0 3.5 1.5 3.5 3.5 0 2.89-3.14 5.74-7.9 10.05z"/></svg>
                    <h5 style="margin:0; display:block">${comment_like_count}</h5>
                    </div>
                </div>
            </div>`
        $('#comments-box').prepend(temp_html)
} else if(comment_like_count != 0){
    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>
        </div>
        <div class="commentdetail">
            <div class="comment"><h6>${comments}</h6></div>
            <div style="display:flex; flex-direction:row; align-items:center">
            <svg xmlns="http://www.w3.org/2000/svg" id="hearts-button" class="hearts"token interpolation">${comment_id})" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/></svg>
            <h5 style="margin:0; display:block">${comment_like_count}</h5>
            </div>
        </div>
    </div>`
$('#comments-box').prepend(temp_html)
}
} }} )
}


/// ๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ •
async function getUpdateData() {
    let title= document.getElementById("title-update").value
    let contents = document.getElementById("contents-update").value
    if (!localStorage.getItem("access")) {
        alert('๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”!')
    }
    updateArticle(title, 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 getUpdateComment(comment_id) {
    let comment= document.getElementById(`comment-update${comment_id}`).value
    if (!localStorage.getItem("access")) {
        alert('๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”!')
    }
    updateComment(comment, comment_id)
}

/// ๋Œ“๊ธ€ ์‚ญ์ œ 
async function getdeleteComment(comment_id) {
    if (!localStorage.getItem("access")) {
        alert('๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”!')
    }
    deleteComment(comment_id)
}
// ํญ์Šค ํˆฌํ‘œ
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()
    
}

function updateMode() {
  
    const title = response_json["article_title"]
    const content = response_json["article_contents"]
    
    const input_title = document.createElement("textarea")
    input_title.setAttribute("id", "input_title")
    input_title.innerText = title.innerHTML

    const input_content = document.createElement("textarea")
    input_content.setAttribute("id", "input_content")
    input_content.innerText = content.innerHTML
    
    const body = document.body
    body.insertBefore(input_title, title)
    body.insertBefore(input_content, content)
}

// #๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

* search.js


// ๊ฒ€์ƒ‰๊ธฐ๋Šฅ
async function search() {
    const searchData = {
        type: document.getElementById('searchOption').value,
        search: document.getElementById('searchKeywords').value,
    }

    const response = await fetch(`${backend_base_url}/article/search/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            "Authorization": "Bearer " + localStorage.getItem("access"),
        },
        body: JSON.stringify(searchData)
    })
    if (searchData['search'] == '') {
        alert('๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!')
    } else {
        // ์„ธ์…˜ ์ €์žฅ
        response_json = await response.json()
        result = JSON.stringify(response_json)
        num = response_json.length
        sessionStorage.setItem("result", result)
        sessionStorage.setItem("keyword", searchData.search)
        sessionStorage.setItem("type", searchData.type)
        sessionStorage.setItem("num", num)
        if (result.length == 2) {
            alert('๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค!')
        }else{
            window.location.href = `${frontend_base_url}/search_result.html`
            return result
        }
    }

    response_json = await response.json()
    return response_json.articles
}

const PagingConf = {
    totalCount: 100,
    numbersPerPage: 5,
    navPageNumber: Math.ceil(sessionStorage.getItem("num")/10),
};
sessionStorage.removeItem("num")
const feedNumber = {
    num : 0
}

$(document).ready(function () {
    setPageInfo(PagingConf.numbersPerPage, PagingConf.navPageNumber, 1);
    // setFeedNumber( $(val) );
    makePageNav();
    getPosts();
});


function makePageNav() {
    const pageInfo = getPageInfo();
    renderPaging($('.paging'), PagingConf.totalCount, pageInfo.numbersPerPage, pageInfo.navPageNumber, pageInfo.currentPage, true);
}

function renderPaging(appendElement, totalCount, numbersPerPage, navPageNumber, currentPage) {

    let totalPage = Math.ceil(totalCount / numbersPerPage); // ์ „์ฒด ํŽ˜์ด์ง€์ˆ˜
    let pageGroup = Math.ceil(currentPage / navPageNumber); // 1~10 : 1๊ทธ๋ฃน 11~20 : 2๊ทธ๋ฃน

    let last = pageGroup * navPageNumber; // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์ˆ˜, ์ „์ฒด ํŽ˜์ด์ง€์ˆ˜ ๋„˜์ง€์•Š๊ฒŒ ์ฒ˜๋ฆฌ
    if (last > totalPage) {
        last = totalPage;
    }

    let first = last - (navPageNumber - 1);
    let front = 1;
    let prev = first - 1;
    let next = last + 1;
    let rear = totalPage;

    let innerHtml = ''; // ์ด๋™ ํŽ˜์ด์ง€ ์ˆ˜์™€ ํ™”์‚ดํ‘œ html

    // ์™ผ์ชฝ < <<
    if (prev > 0) {
        innerHtml += '<a href=#token operator">+ front + ')"> << </a>';
        innerHtml += '<a href=#token operator">+ prev + ')"> < </a> ';
    }
    // ์ค‘๊ฐ„ ์ด๋™ ํƒœ๊ทธ
    for (let i = first; i <= last; i++) {
        if (currentPage === i) {
            innerHtml += '<a href=# class="on"token operator">+ i + ')">' + i + '</a> ';
        } else {
            innerHtml += '<a href=#token operator">+ i + ')">' + i + '</a> ';
        }
    }
    // ์˜ค๋ฅธ์ชฝ > >>
    if (last < totalPage) {
        innerHtml += '<a href=#token operator">+ next + ')"> > </a>';
        innerHtml += '<a href=#token operator">+ rear + ')"> >> </a>';
    }

    appendElement.empty();
    appendElement.append(innerHtml);
}

function movePage(currentPage) {
    const pageInfo = getPageInfo();
    setPageInfo(pageInfo.numbersPerPage, pageInfo.navPageNumber, currentPage);
    makePageNav();
    getPosts();
}

function setPageInfo(numbersPerPage, navPageNumber, currentPage) {
    const pageInfo = {
        numbersPerPage: numbersPerPage,
        navPageNumber: navPageNumber,
        currentPage: currentPage
    };
    sessionStorage.setItem('pageInfo', JSON.stringify(pageInfo));
}

function getPageInfo() {
    return JSON.parse(sessionStorage.getItem('pageInfo'));
}

function getFeedInfo() {
    return JSON.parse(sessionStorage.getItem('pageInfo'));
}
function postClick() {
    if (!localStorage.getItem("access")) {
        alert('๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”!')
    } else {
        window.location.href='posting.html'
    }
}

function enterkey() {
	if (window.event.keyCode == 13) {
    	// ์—”ํ„ฐํ‚ค๊ฐ€ ๋ˆŒ๋ ธ์„ ๋•Œ
        search()

    }
}

function getPosts() {

    $('#search-post').empty()

    const pageInfo = getPageInfo();

    let page = pageInfo.currentPage;
    let offset = pageInfo.numbersPerPage;
    let params = '?page=' + page;
    params += '&offset=' + offset;
    $.ajax({
        type: 'POST',
        url: `${backend_base_url}/article/search/`,
        data: {            
            type: sessionStorage.getItem("type"),
            search: sessionStorage.getItem("keyword")
        },
        async: false,
        success: function (response) {        
            if (response.length != 0) {
                let posts = response;
                let board_html = `<b id="board-name">'${sessionStorage.getItem("keyword")}' ๊ฒ€์ƒ‰๊ฒฐ๊ณผ</b>`
                $('#board-name').empty()
                $('#board-name').append(board_html)
                for (let i = 0; i < posts.length; i++) {
                    let cnt = i + 1
                    const id = posts[i]['id']
                    const title = posts[i]['article_title']
                    const author = posts[i]['author']
                    const date = posts[i]['article_post_date']

                    let temp_html = `<div class="post-line row"token interpolation">${frontend_base_url}/detail.html?id=${id}'">
                                        <div class="th number col-1" scope="col">${cnt}.</div>
                                        <div class="th title col-6" scope="col"><b>${title}</b></div>
                                        <div class="th author col col-md-col1" scope="col">${author}</div>
                                        <div class="th date col" scope="col">${date}</div>
                                    </div>`
                    $('#search-post').append(temp_html)
                }
            }
        },
        fail: function (response) {
            // window.location.reload()
        }
    });
}

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

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

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