-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (78 loc) · 2.88 KB
/
app.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
function arrayRemove (array, value) {
return array.filter(function(element){
return element != value;
});
}
const VUE = new Vue({
el: '#app',
data: {
todos : [],
archivedTodos : [],
doneTodos : [],
errors : [],
errorMessages : {
sameTodo : "You cant add same todo!",
blankTodo : "You need to write sth!"
}
},
methods : {
addTodo : function () {
var todosArray = this.todos
console.log(this.inputValue)
if (this.inputValue == undefined) {
var errorMessage = this.errorMessages.blankTodo
console.log(errorMessage)
this.errors.push(errorMessage)
} else if (this.inputValue.trim() == "") {
this.errors.push(this.errorMessages.blankTodo)
} else if (this.todos.includes(this.inputValue.trim())) {
var errorMessage = this.errorMessages.sameTodo
this.errors.push(errorMessage)
} else {
this.errors = []
todosArray.push(this.inputValue.trim())
this.inputValue = null;
localStorage.setItem('todos', JSON.stringify(todosArray))
}
},
archiveTodo : function (todo) {
var archiveTodosArray = this.archivedTodos
var newTodos = arrayRemove(this.todos, todo)
this.todos = arrayRemove(this.todos, todo)
archiveTodosArray.push(todo)
localStorage.setItem('archivedTodos', JSON.stringify(archiveTodosArray))
localStorage.setItem('todos', JSON.stringify(newTodos))
},
doneTodo : function (todo) {
var doneTodosArray = this.doneTodos
var newTodos = arrayRemove(this.todos, todo)
this.todos = arrayRemove(this.todos, todo)
doneTodosArray.push(todo)
localStorage.setItem('doneTodos', JSON.stringify(doneTodosArray))
localStorage.setItem('todos', JSON.stringify(newTodos))
},
deleteDoneTodo : function (doneTodo) {
var doneTodosArray = this.doneTodos
var newDoneTodos = arrayRemove(doneTodosArray, doneTodo)
this.doneTodos = arrayRemove(doneTodosArray, doneTodo)
localStorage.setItem('doneTodos', JSON.stringify(newDoneTodos))
},
deleteArchivedTodo : function (archivedTodo) {
var archivedTodosArray = this.archivedTodos
var newArchivedTodos = arrayRemove(archivedTodosArray, archivedTodo)
this.archivedTodos = arrayRemove(archivedTodosArray, archivedTodo)
localStorage.setItem('archivedTodos', JSON.stringify(newArchivedTodos))
},
}
})
document.addEventListener("DOMContentLoaded", () => {
JSON.parse(localStorage.getItem('todos')).forEach((item) => {
VUE.todos.push(item)
});
JSON.parse(localStorage.getItem('archivedTodos')).forEach((item) => {
VUE.archivedTodos.push(item)
});
JSON.parse(localStorage.getItem('doneTodos')).forEach((item) => {
VUE.doneTodos.push(item)
});
});