Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. let todos = [
  2. {id: 0, name: 'Buy milk', due: moment('2019-05-30'), isDone: true},
  3. {id: 1, name: 'Buy beer', due: '2019-06-03', isDone: false},
  4. {
  5. id: 2,
  6. name: 'Read "The Hitchhiker\'s Guide to the Galaxy" book',
  7. due: '2019-06-14',
  8. isDone: false
  9. }
  10. ];
  11.  
  12. const statics = {
  13. id: todos.length
  14. };
  15.  
  16. const populateTodos = (todos, container) => {
  17. const $div = $(container);
  18. $div.empty();
  19. todos.forEach((element) => {
  20. $div.append(`
  21. <div>
  22. <input data-check-id="${element.id}" type="checkbox" ${
  23. element.isDone ? 'checked' : ''
  24. }/>
  25. <span>${element.name} - ${moment(element.due).format(
  26. 'MMMM Do YYYY'
  27. )}</span>
  28. <button data-id="${element.id}">X</button>
  29. </div>
  30. `);
  31. });
  32. // Finish the function
  33. };
  34.  
  35. const todoView = (todo, container) => {
  36. const $div = $(container);
  37. $div.append(`
  38. <div class="todo-single">
  39. <input data-check-id="${todo.id}" type="checkbox" ${
  40. todo.isDone ? 'checked' : ''
  41. }/>
  42. <span>${todo.name} - ${moment(todo.due).format('MMMM Do YYYY')}</span>
  43. <button data-id="${todo.id}">X</button>
  44. </div>
  45. `);
  46. };
  47.  
  48. (() => {
  49. populateTodos(todos, '#todos');
  50. $(document).on('click', '#btn-add', (ev) => {
  51. const todotext = $('#todo-text').val();
  52. const tododate = $('#todo-date').val();
  53. console.log(moment(tododate));
  54. if (!moment(tododate).isValid() || moment(tododate).isSameOrBefore()) {
  55. alert('Invalid date');
  56. return;
  57. }
  58. const newToDo = {
  59. id: statics.id++,
  60. name: todotext,
  61. due: moment(tododate),
  62. isDone: false
  63. };
  64.  
  65. todos.push(newToDo);
  66. populateTodos(todos, '#todos');
  67. $('#todo-text').val('');
  68. $('#todo-date').val('');
  69. alert('Todo Added!');
  70. });
  71.  
  72. // Remove event
  73. $(document).on('click', '#todos > div > button', (ev) => {
  74. alert('Todo Deleted!');
  75. const todoId = $(ev.target).attr('data-id');
  76. todos = todos.filter((el) => el.id !== Number(todoId));
  77. populateTodos(todos, '#todos');
  78. });
  79.  
  80. // Check event
  81. $(document).on('click', '#todos > div > input', (ev) => {
  82. const todoId = $(ev.target).attr('data-check-id');
  83. alert('Todo Toggled!');
  84. todos = todos.map((el) => {
  85. if (el.id === todoId) {
  86. el.isDone = !el.isDone;
  87. }
  88. return el;
  89. });
  90. });
  91. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement