-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
98 lines (95 loc) · 3.24 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const form = document.querySelector('.input');
const addInput = document.querySelector('.add-input');
const list = document.querySelector('.list');
const count = document.querySelector('#task-count');
// We need an array to hold our state
let items = [];
function handleSubmit(e) {
e.preventDefault();
// console.log('added!!');
const title = addInput.value.trim();
if (!title) return;
const item = {
title,
id: Date.now(),
// id: moment(new Date()).format("DD.MM.YYYY - hh:mm"),
complete: false,
};
items.push(item);
e.target.reset();
// fire off a custom event that will tell anyone else who cares that the items have been updated!
list.dispatchEvent(new CustomEvent('itemsUpdated'));
}
function displayItems() {
// console.log(items);
const html = items
.map(
item => `<li class="card">
<div class="card-left">
<input name="resolve" class="resolve"
value="${item.id}"
type="checkbox"
${item.complete && 'checked'}>
<div class="card-info">
<h4 class="task">${item.title}</h4>
</div>
</div>
<button aria-label="Remove ${item.title}"
value="${item.id}" name="delete-btn" class="fas fa-times btn delete-btn"></button>
</li>`
)
.join('');
list.innerHTML = html;
}
function mirrorToLocalStorage() {
// console.info('Saving todos to localstorage');s
localStorage.setItem('items', JSON.stringify(items));
count.innerHTML = `${items.length} Tasks`;
date.innerHTML = moment(new Date()).format("ddd, DD MMM YYYY");
}
function restoreFromLocalStorage() {
// console.info('Restoring from LS');x
const lsItems = JSON.parse(localStorage.getItem('items'));
if (lsItems.length === 0) {
const manual =
{
id: 1,
complete: false,
title: 'Hello',
};
items.push(manual);
localStorage.setItem('items', JSON.stringify(items));
}
// pull the items from LS
else if (lsItems.length) {
items.push(...lsItems);
list.dispatchEvent(new CustomEvent('itemsUpdated'));
}
}
function deleteItem(id) {
// console.log('DELETIENG ITEM', id);
// update our items array without this one
items = items.filter(item => item.id !== id);
// console.log(items);
list.dispatchEvent(new CustomEvent('itemsUpdated'));
}
function markAsComplete(id) {
// console.log('Marking as complete', id);
const itemRef = items.find(item => item.id === id);
itemRef.complete = !itemRef.complete;
list.dispatchEvent(new CustomEvent('itemsUpdated'));
}
form.addEventListener('submit', handleSubmit);
list.addEventListener('itemsUpdated', displayItems);
list.addEventListener('itemsUpdated', mirrorToLocalStorage);
// Event Delegation: We listen or the click on the list <ul> but then delegate the click over to the button if that is what was clicked
list.addEventListener('click', function(e) {
const id = parseInt(e.target.value);
if (e.target.matches('button')) {
deleteItem(id);
}
if (e.target.matches('input[type="checkbox"]')) {
markAsComplete(id);
}
});
restoreFromLocalStorage();