// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

document.addEventListener("turbolinks:load", function() {
  $('#new_task').submit(function(event) {
    event.preventDefault();
    let values = $(this).serialize();
    let posting = $.post(this.action, values);
    posting.success(function(data) {
      let task = data;
      let template = Handlebars.compile(document.getElementById("task-template").innerHTML);
      let result = template(task);
      if (task.description) {
        document.getElementById("edit-selected").innerHTML += result;
        document.getElementById("new_task").reset();
        // update incomplete tasks counter
        num = parseInt(document.getElementById("number-of-incomplete-tasks").innerHTML);
        document.getElementById("number-of-incomplete-tasks").innerHTML = num + 1;
      }
    });
  });
});

// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

document.addEventListener("turbolinks:load", function() {
  $('#new_task').submit(function(event) {
    event.preventDefault();
    let values = $(this).serialize();
    let posting = $.post(this.action, values);
    posting.success(function(data) {
      let task = data;
      let template = Handlebars.compile(document.getElementById("task-template").innerHTML);
      let result = template(task);
      if (task.description) {
        document.getElementById("edit-selected").innerHTML += result;
        document.getElementById("new_task").reset();
        // update incomplete tasks counters
        num = parseInt(document.getElementById("number-of-incomplete-tasks").innerHTML);
        document.getElementById("number-of-incomplete-tasks").innerHTML = num + 1;

        element = document.querySelector(".list-group-lists a.active span");
        if (element.innerHTML) {
          element.innerHTML = num + 1;
        } else {
          element.innerHTML = 1;
        }
      }
    });
  });
});

update navbar incomplete tasks counter with AJAX call