<commit_before>﻿'use strict';

let addButtons = document.querySelectorAll('.addProduct');

addButtons.forEach(function (button) {
    button.addEventListener('click', function(event) {
        event.preventDefault();
        let productId = button.parentElement.querySelector('.productId').value;
        let qty = button.parentElement.querySelector('.addQty').value;
        postToDatabase(productId, qty);
    })
})

function postToDatabase(productId, qty) {

    let postData = new FormData();
    postData.append("productId", productId);
    postData.append("orderQty", qty);
    fetch('cart/AddToCart', 
    {
        method: "POST",
        body: postData,
        credentials: "same-origin",
    })
    .then(function (response) { return response.json();})
    .then(function (result) {
        console.log(result);

    })
}<commit_msg>update add to cart with ajax<commit_after>﻿'use strict';

let addButtons = document.querySelectorAll('.addProduct');

addButtons.forEach(function (button) {
    button.addEventListener('click', function(event) {
        event.preventDefault();
        let productId = button.parentElement.querySelector('.productId').value;
        let qty = button.parentElement.querySelector('.addQty').value;
        postToDatabase(productId, qty);
    })
})

function postToDatabase(productId, qty) {

    let postData = new FormData();
    postData.append("productId", productId);
    postData.append("orderQty", qty);
    fetch('/cart/AddToCart', 
    {
        method: "POST",
        body: postData,
        credentials: "same-origin",
    })
    .then(function (response) { return response.json();})
    .then(function (result) {
        let message = document.querySelector('.message');
        if (result.success) {
            message.classList.add('ok');
            setTimeout(function () {
                message.classList.remove('ok');
            }, 5000);
        } else {
            message.classList.add('error');
            setTimeout(function () {
                message.classList.remove('error');
            }, 5000);
        }
        document.querySelector('.message p').innerHTML = result.message;
    })
}
{'fraction_non_alphanumeric': 0.14510166358595195, 'fraction_numerical': 0.0036968576709796672, 'mean_word_length': 2.811619718309859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4859649', 'n_tokens_mistral': 603, 'n_tokens_neox': 607, 'n_words': 107}