<commit_before>import { requester } from '../../utils/requster.js';
import { templates } from '../../utils/templates.js';
import { validator } from '../../utils/validator.js';
import { formHandler } from '../../utils/formHandler.js';

let dataFromAPI;
const newsUrl = "http://127.0.0.1:8000/api/news/";

export function DetailedNewsController(id) {

    return new Promise((resolve, reject) => {
        resolve(requester.getJSON(newsUrl + id + '/'));
    }).then((newData) => {
        dataFromAPI = newData;
        return new Promise((resolve, reject) => {
            resolve(templates.get('detailed-news'));
        });
    }).then((res) => {
        let hbTemplate = Handlebars.compile(res),
            template = hbTemplate(dataFromAPI);

        $('#content').html(template);

        formHandler();

        $('#add-comment-button').on('click', () => {
            let body = {
                content: ''
            },
                addCommentUrl = `http://127.0.0.1:8000/api/news/${id}/add_comment/`;

            if (validator.comment($('#comment-content').val())) {
                body.content = $('#comment-content').val();
                requester.postJSON(addCommentUrl, body)
                    .then(() => {
                        toastr.success('Comment added!');
                        $('#comment-content').val('');
                    }).catch((err) => {
                        toastr.error('Comments can\' be empty!')
                    })
            } 
            else {
                toastr.error('Comments shold be max 2048 characters long!');
            }
        });
    }).catch((err) => {
        console.log(err);
    });

}

export function loadComments(id) {
    let getData = requester.getJSON(newsUrl + id + '/'),
        getTemplate = templates.get('partials/comment');

    Promise.all([getData, getTemplate]).then((result) => {
        let newData = result[0],
            hbTemplate = Handlebars.compile(result[1]),
            commentsToLoad = [];

        commentsToLoad = newData.comment_set.filter(function (obj) {
            return !dataFromAPI.comment_set.some(function (obj2) {
                return obj.content === obj2.content && obj.posted_by.user === obj2.posted_by.user;
            });
        });

        dataFromAPI.comment_set = newData.comment_set;

        for (let i = 0; i < commentsToLoad.length; i += 1) {
            let template = hbTemplate(commentsToLoad[i]);
            $('#comments').append(template);
        }
    })
}<commit_msg>Changed comment add url<commit_after>import { requester } from '../../utils/requster.js';
import { templates } from '../../utils/templates.js';
import { validator } from '../../utils/validator.js';
import { formHandler } from '../../utils/formHandler.js';

let dataFromAPI;
const newsUrl = "http://127.0.0.1:8000/api/news/";

export function DetailedNewsController(id) {

    return new Promise((resolve, reject) => {
        resolve(requester.getJSON(newsUrl + id + '/'));
    }).then((newData) => {
        dataFromAPI = newData;
        return new Promise((resolve, reject) => {
            resolve(templates.get('detailed-news'));
        });
    }).then((res) => {
        let hbTemplate = Handlebars.compile(res),
            template = hbTemplate(dataFromAPI);

        $('#content').html(template);

        formHandler();

        $('#add-comment-button').on('click', () => {
            let body = {
                content: ''
            },
                addCommentUrl = `http://127.0.0.1:8000/api/news/${id}/comments/`;

            if (validator.comment($('#comment-content').val())) {
                body.content = $('#comment-content').val();
                requester.postJSON(addCommentUrl, body)
                    .then(() => {
                        toastr.success('Comment added!');
                        $('#comment-content').val('');
                    }).catch((err) => {
                        toastr.error('Comments can\' be empty!')
                    })
            }
            else {
                toastr.error('Comments shold be max 2048 characters long!');
            }
        });
    }).catch((err) => {
        console.log(err);
    });

}

export function loadComments(id) {
    let getData = requester.getJSON(newsUrl + id + '/'),
        getTemplate = templates.get('partials/comment');

    Promise.all([getData, getTemplate]).then((result) => {
        let newData = result[0],
            hbTemplate = Handlebars.compile(result[1]),
            commentsToLoad = [];

        commentsToLoad = newData.comment_set.filter(function (obj) {
            return !dataFromAPI.comment_set.some(function (obj2) {
                return obj.content === obj2.content && obj.posted_by.user === obj2.posted_by.user;
            });
        });

        dataFromAPI.comment_set = newData.comment_set;

        for (let i = 0; i < commentsToLoad.length; i += 1) {
            let template = hbTemplate(commentsToLoad[i]);
            $('#comments').append(template);
        }
    })
}

{'fraction_non_alphanumeric': 0.16769078295341922, 'fraction_numerical': 0.012289395441030724, 'mean_word_length': 1.8867276887871853, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4740094', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1389, 'n_words': 241}