-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
107 lines (78 loc) · 2.35 KB
/
App.js
File metadata and controls
107 lines (78 loc) · 2.35 KB
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
99
100
101
102
103
104
105
106
107
const app = document.querySelector('#app');
import {addTask} from "./src/services/taskApi";
import {tasks, update } from "./src/services/store";
import {deleteThemAll, displayTask, renderTask, rootView, welcomeMessage } from "./src/services/views";
app.innerHTML = rootView;
// prevent default form action
const form = document.querySelector('form');
form.addEventListener('submit',(e)=>{
e.preventDefault();
})
/**
* check for todos, if empty it knows what do display to the dom
*/
welcomeMessage();
function check(){
const getTask = tasks();
getTask.forEach((gottenTask)=>{
const renderedTask = renderTask(gottenTask);
displayTask(renderedTask);
});
}
check(); //render all available tasks from the beginning of the file
const addBtn = document.querySelector('.addBtn');
addBtn.addEventListener('click', ()=>{
const taskValue = taskInput.value.trim();
if(taskValue.length == 0){
alert('Todo is empty, add something jhoor')
} else{
addTask(taskValue);
taskInput.value= '';
}
});
/**
* addBtn and taskInput basically do the same thing, add a new todo
* most times I'm using task instead of todo, pardon the english
*
*/
const taskInput = document.querySelector('.taskInput');
taskInput.addEventListener('keypress', ()=>{
switch (e.key) {
case "Enter":
const taskValue = taskInput.value.trim();
if(taskValue.length == 0){
alert('Todo is empty, add something jhoor');
} else{
addTask(taskValue);
taskInput.value= '';
}
break;
}
});
/**
* Clear all tasks toggled as completed
*/
const clearBtn = document.querySelector('.clearBtn');
clearBtn.addEventListener('click',()=>{
/**
* Kenbot gave me this idea, guess I was tryna implement it in the wrong way
* by calling delete on each item
*/
let fetchTasks = tasks();
let tracker = [];
let unCompleted = fetchTasks.filter((fetchedTask) => fetchedTask.completed != true);
tracker.push(...unCompleted);
if(tracker.length > 0 ){
update(tracker);
deleteThemAll(tracker);
tracker = [];
}
else{
tracker = [];
update(tracker);
fetchTasks = tasks();
const taskHolder = document.querySelector('.taskHolder');
taskHolder.innerHTML = '';
welcomeMessage();
}
});