-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (122 loc) · 6.02 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
window.addEventListener('load', () => {
const form = document.getElementById('task-form')
const input = document.getElementById('task-input')
const listOfTasks = document.getElementById('task-container')
// localStorage.removeItem("taskList");
let taskObj = JSON.parse(localStorage.getItem('taskList'))
if (taskObj) { //checking to see if there is a task already
Object.entries(taskObj).forEach(el => { //obj.entries turns an object to an array of arrays! for you there spying my codes
const taskEl = document.createElement('div') //allows us create dom notes
taskEl.classList.add('task')
const taskContentEl = document.createElement('div')
taskContentEl.classList.add('task-content')
taskEl.appendChild(taskContentEl)
const taskInputEl = document.createElement('input')
taskInputEl.classList.add('text')
taskInputEl.type = 'text'
taskInputEl.value = el[1]
taskInputEl.setAttribute('readonly', 'readonly')
taskContentEl.appendChild(taskInputEl)
const taskActionsBtnsEl = document.createElement('div')
taskActionsBtnsEl.classList.add('task-actions')
const taskEditBtnEl = document.createElement('button')
taskEditBtnEl.setAttribute('id', el[0])
taskEditBtnEl.classList.add('edit')
taskEditBtnEl.innerHTML = 'Edit'
const taskDeleteBtnEl = document.createElement('button')
taskDeleteBtnEl.setAttribute('id', `D${(el)[0]}`)
taskDeleteBtnEl.classList.add('delete')
taskDeleteBtnEl.innerHTML = 'Delete'
taskActionsBtnsEl.appendChild(taskEditBtnEl)
taskActionsBtnsEl.appendChild(taskDeleteBtnEl)
taskEl.appendChild(taskActionsBtnsEl)
listOfTasks.appendChild(taskEl)
input.value = ''
taskEditBtnEl.addEventListener('click', (e) => {
if (taskEditBtnEl.innerText.toLowerCase() == 'edit') {
taskEditBtnEl.innerText = 'Save'
taskInputEl.removeAttribute('readonly')
taskInputEl.focus()
}
else {
taskInputEl.setAttribute('readonly', 'readonly')
taskEditBtnEl.innerText = 'Edit'
let id = e.target.id
let taskObj = JSON.parse(localStorage.getItem('taskList'))
taskObj[id] = taskInputEl.value
localStorage.setItem('taskList', JSON.stringify(taskObj))
}
})
taskDeleteBtnEl.addEventListener('click', (e) => {
listOfTasks.removeChild(taskEl)
let taskObj = JSON.parse(localStorage.getItem('taskList'))
let id = e.target.id.slice(1)
delete taskObj[id]
localStorage.setItem('taskList', JSON.stringify(taskObj))
})
});
}
//form
form.addEventListener('submit', (event) => {
event.preventDefault() //stops it from refreshing the page
const inputedtask = input.value
let randomId = Math.random().toString().slice(2,8)
if (!localStorage.getItem('taskList')) {
let taskObj = {[randomId]:inputedtask}
localStorage.setItem('taskList', JSON.stringify(taskObj))
} else {
let taskObj = JSON.parse(localStorage.getItem('taskList'))
taskObj[randomId] = inputedtask
localStorage.setItem('taskList', JSON.stringify(taskObj))
}
const taskEl = document.createElement('div') //allows us create dom notes
taskEl.classList.add('task')
const taskContentEl = document.createElement('div')
taskContentEl.classList.add('task-content')
taskEl.appendChild(taskContentEl)
const taskInputEl = document.createElement('input')
taskInputEl.classList.add('text')
taskInputEl.type = 'text'
taskInputEl.value = inputedtask
taskInputEl.setAttribute('readonly', 'readonly') //prevents editing
taskContentEl.appendChild(taskInputEl)
const taskActionsBtnsEl = document.createElement('div')
taskActionsBtnsEl.classList.add('task-actions')
const taskEditBtnEl = document.createElement('button')
taskEditBtnEl.classList.add('edit')
taskEditBtnEl.setAttribute('id', `${randomId}`)
taskEditBtnEl.innerHTML = 'Edit'
const taskDeleteBtnEl = document.createElement('button')
taskDeleteBtnEl.classList.add('delete')
taskDeleteBtnEl.setAttribute('id', `D${randomId}`)
taskDeleteBtnEl.innerHTML = 'Delete'
taskActionsBtnsEl.appendChild(taskEditBtnEl)
taskActionsBtnsEl.appendChild(taskDeleteBtnEl)
taskEl.appendChild(taskActionsBtnsEl)
listOfTasks.appendChild(taskEl)
input.value = ''
taskEditBtnEl.addEventListener('click', (e) => {
if (taskEditBtnEl.innerText.toLowerCase() == 'edit') {
taskEditBtnEl.innerText = 'Save'
taskInputEl.removeAttribute('readonly')
taskInputEl.focus()
}
else {
taskInputEl.setAttribute('readonly', 'readonly')
taskEditBtnEl.innerText = 'Edit'
let id = e.target.id
let taskObj = JSON.parse(localStorage.getItem('taskList'))
taskObj[id] = taskInputEl.value
localStorage.setItem('taskList', JSON.stringify(taskObj))
}
})
taskDeleteBtnEl.addEventListener('click', (e) => {
let taskObj = JSON.parse(localStorage.getItem('taskList'))
let id = e.target.id.slice(1)
delete taskObj[id]
localStorage.setItem('taskList', JSON.stringify(taskObj))
listOfTasks.removeChild(taskEl)
})
})
})