var handleTodoList = function(object) {
  var todos = object;

  if (!todos.length) {
    $('.todo-list-container').append('<p>There are no tasks for you today</p>');
  }

  $.each(todos, function(i, v) {
    var doneHtml = '<input id="' + v.id + '" type="checkbox"' + (v.done ? ' checked="checked"' : '') + '>';      
    var labelHtml = '<label for="' + v.id + '">' + doneHtml + ' ' + v.text + '</label>';
    var itemHtml = '<div class="todo-item">' + labelHtml + '</div>';
    
    $('.todo-list-container').append(itemHtml);
    $('#' + v.id).click(function(){
      console.log($(this).prop('checked'));
    });
  });
};

var loadTodos = function() {
  $.ajax({
    url: "/graphql?query={todoList{id,text,done}}"
  }).done(function(data) {
    console.log(data);
    var dataParsed = JSON.parse(data);
    handleTodoList(dataParsed.data.todoList);
  });
};

var addTodo = function(todoText) {
  if (!todoText || todoText === "") {
    alert('Please specify a task');
    return;
  }

  $.ajax({
    url: '/graphql?query=mutation+_{createTodo(text:"' + todoText + '"){id,text,done}}'
  }).done(function(data) {
    console.log(data);
    var dataParsed = JSON.parse(data);
    var todoList = [dataParsed.data.createTodo];
    handleTodoList(todoList);
  });
};

$(document).ready(function() {
  $('.todo-add-form').submit(function(e){
    e.preventDefault();
    addTodo($('.todo-add-form #task').val());
    $('.todo-add-form #task').val('');
  });

  loadTodos();
});

@@ -1,12 +1,504 @@
+var updateTodo = function(id, isDone)%7B%0A  $.ajax(%7B%0A    url: '/graphql?query=mutation+_%7BupdateTodo(id:%22' + id + '%22,done:' + isDone + ')%7Bid,text,done%7D%7D'%0A  %7D).done(function(data) %7B%0A    console.log(data);%0A    var dataParsed = JSON.parse(data);%0A    var updatedTodo = dataParsed.data.updateTodo;%0A    if (updatedTodo.done) %7B%0A      $('#' + updatedTodo.id).parent().parent().addClass('todo-done');%0A    %7D else %7B%0A      $('#' + updatedTodo.id).parent().parent().removeClass('todo-done');%0A    %7D %0A  %7D);%0A%7D;%0A%0A
 var handleTo
@@ -525,16 +525,16 @@
 ject) %7B%0A
-
   var to
@@ -1061,43 +1061,108 @@
     
-console.log($(this).prop('checked')
+var id = $(this).prop('id');%0A      var isDone = $(this).prop('checked');%0A      updateTodo(id, isDone
 );%0A 

Add update todo functionality