-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.html
65 lines (55 loc) · 1.65 KB
/
template.html
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
<!DOCTYPE html>
<html>
<head>
<script>
let globalId = 1;
let todoState = [];
let oldTodoState = [];
function addTodo() {
// big function we wrote in the beginning
}
function removeTodo(todo) {
const element = document.getElementById(todo.id);
element.parent.removeChild(element);
}
function updateTodo(oldTodo, newTodo) {
const element = document.getElementById(oldTodo.id);
element.children[0].innerHTML = newTodo.title;
element.children[1].innerHTML = newTodo.description;
element.children[0].innerHTML = newTodo.completed ? "Mark as done" : "Done";
}
function updateState(newTodos) {
// calculate the diff b/w newTodos and oldTodos.
// More specifically, find out what todos are -
// 1. added
// 2. deleted
// 3. updated
const added = [];
const deleted = [];
const updated = [];
// calculate these 3 arrays
// call addTodo, removeTodo, updateTodo functions on each of the
// elements
oldTodoState = newTodos;
}
function addTodo() {
const title = document.getElementById("title").value;
const description = document.getElementById("description").value;
todoState.push({
title: title,
description: description,
id: globalId++,
})
updateState(todoState);
}
</script>
</head>
<body>
<input type="text" id="title" placeholder="Todo title"></input> <br /><br />
<input type="text" id="description" placeholder="Todo description"></input> <br /><br />
<button onclick="addTodo()">Add todo</button>
<br /> <br />
<div id="todos">
</div>
</body>
</html>