-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
252 lines (193 loc) · 7.78 KB
/
solution.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const state = {
taskList: [],
};
// DOM
const taskContents = document.querySelector(".task__contents");
const taskModal = document.querySelector(".task__modal__body");
console.log(taskContents);
console.log(taskModal);
const htmlTaskContent = ({id, title, description, type, url}) => `
<div class='col-md-6 col-lg-4 mt-3' id=${id} key=${id}>
<div class='card shadow task__card'>
<div class='card-header d-flex gap-2 justify-content-end task__card__header'>
<button type='button' class='btn btn-outline-info mr-2' name=${id} onclick="editTask.apply(this, arguments)"><i class='fas fa-pencil-alt'></i></button>
<button type='button' class='btn btn-outline-danger mr-2' name=${id} onclick="deleteTask.apply(this, arguments)">
<i class='fas fa-trash-alt'></i></button>
</div>
<div class='card-body'>
${
url ?
`<img src=${url} alt='card image class='card-img-top md-3 rounded-md' />`
:
`<img src="https://tse3.mm.bing.net/th?id=OIP.FjLkalx51D8xJcpixUGJywHaE8&pid=Api&P=0&h=180" alt='card image class='card-img-top md-3 rounded-md' />`
}
<h4 class='card-title'>${title}</h4>
<p class='card-text text-muted'>${description}</p>
<div class='tags d-flex flex-wrap'>
<span class='badge text-white bg-primary m-1'>${type}</span>
</div>
</div>
<div class='card-footer'>
<button type='button' class='btn btn-outline-primary float-end' data-bs-toggle='modal'
data-bs-target='#showTask' id=${id} onclick='openTask.apply(this, arguments)'>Open Task</button>
</div>
</div>
</div>
`
const htmlModalContent = ({id, title, description, url}) => {
const date = new Date(parseInt(id));
return `
<div id=${id}>
${
url ?
`<img src=${url} alt='card image class='img-fluid rounded place__holder__image mb-3' />`
:
`<img src="https://tse3.mm.bing.net/th?id=OIP.FjLkalx51D8xJcpixUGJywHaE8&pid=Api&P=0&h=180" alt='card image class='card-img-top md-3 rounded-md' />`
}
<strong class='text-sm text-muted'>Created on ${date.toDateString()}</strong>
<h4 class='my-2'>${title}</h4>
<p class='lead text-muted'>${description}</p>
</div>
`
};
const updateLocalStorage = () => {
localStorage.setItem('task', JSON.stringify({
tasks: state.taskList,
}));
};
const loadInitialData = () => {
const localStorageCopy = JSON.parse(localStorage.task);
if(localStorageCopy) state.taskList = localStorageCopy.tasks;
state.taskList.map((cardDate) => {
taskContents.insertAdjacentHTML("beforeend", htmlTaskContent(cardDate));
});
};
const handleSubmit = () =>{
const id = `${Date.now()}`
const input = {
url: document.getElementById('imageUrl').value,
title: document.getElementById('taskTitle').value,
description: document.getElementById('taskDescription').value,
type: document.getElementById('tags').value,
};
if(input.title === '' || input.description=== '' || input.type===''){
return alert("Please fill out the all the necessary fileds!");
}
taskContents.insertAdjacentHTML(
"beforeend", htmlTaskContent({...input, id, })
);
state.taskList.push({...input, id});
updateLocalStorage();
}
// Opening modal from the cards
const openTask = (e) => {
if(!e) e = window.event;
//console.log("event" , e);
const getTask = state.taskList.find(({id}) => id === e.target.id);
taskModal.innerHTML= htmlModalContent(getTask)
}
// CRUD => Delete Operation
const deleteTask = (e) => {
if(!e) e = window.event;
const targetID = e.target.getAttribute("name");
// console.log(e.target);
const type = e.target.tagName;
console.log(type);
const removeTask = state.taskList.filter(({id}) => id !== targetID);
// console.log(removeTask);
state.taskList = removeTask;
// console.log("updated arr", state.taskList);
updateLocalStorage();
// I tag was not working fine
if(type === "BUTTON"){
// console.log(e.target.parentNode.parentNode.parentNode);
return e.target.parentNode.parentNode.parentNode.parentNode.removeChild(
e.target.parentNode.parentNode.parentNode
);
}
return e.target.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(
e.target.parentNode.parentNode.parentNode.parentNode
);
// updateLocalStorage();
}
const editTask = (e) => {
if(!e) e = window.event;
const targetID = e.target.id;
const type = e.target.tagName;
let parentNode;
let taskTitle;
let taskDescription;
let taskType;
let submitButton;
if(type === "BUTTON"){
parentNode = e.target.parentNode.parentNode;
// console.log(parentNode);
}else{
parentNode = e.target.parentNode.parentNode.parentNode;
}
// Uncomment following 2 lines to know abt childNodes
taskTitle = parentNode.childNodes;
console.log(taskTitle);
// taskTitle = parentNode.childNodes[3].childNodes[3];
taskDescription = parentNode.childNodes[3].childNodes[5];
taskType = parentNode.childNodes[3].childNodes[7].childNodes[1];
// console.log(taskTitle, taskDescription, taskType);
submitButton = parentNode.childNodes[5].childNodes[1];
// console.log(submitButton);
taskTitle.setAttribute("contenteditable", "true");
taskDescription.setAttribute("contenteditable", "true");
taskType.setAttribute("contenteditable", "true");
submitButton.setAttribute('onclick', "saveEdit.apply(this, arguments)");
submitButton.removeAttribute("data-bs-toggle");
submitButton.removeAttribute("data-bs-target");
submitButton.innerHTML = "Save Changes";
};
// ... spread opeartor
const saveEdit = (e) => {
if(!e) e=window.event;
const targetID = e.target.id;
const parentNode = e.target.parentNode.parentNode;
// console.log(parentNode);
const taskTitle = parentNode.childNodes[3].childNodes[3];
const taskDescription = parentNode.childNodes[3].childNodes[5];
const taskType = parentNode.childNodes[3].childNodes[7].childNodes[1];
const submitButton = parentNode.childNodes[5].childNodes[1];
const updateData = {
taskTitle: taskTitle.innerHTML,
taskDescription: taskDescription.innerHTML,
taskType: taskType.innerHTML,
};
let stateCopy = state.taskList;
stateCopy = stateCopy.map((task)=>
task.id === targetID
? {
id: task.id,
title: updateData.taskTitle,
description: updateData.taskDescription,
type: updateData.taskType,
}
: task
);
state.taskList = stateCopy;
console.log(state.taskList);
updateLocalStorage();
taskTitle.setAttribute("contenteditable", "false");
taskDescription.setAttribute("contenteditable", "false");
taskType.setAttribute("contenteditable", "false");
submitButton.setAttribute('onclick', "openTask.apply(this, arguments)");
submitButton.setAttribute("data-bs-toggle", "modal");
submitButton.setAttribute("data-bs-target", "#showTask");
submitButton.innerHTML = "Open Task";
};
// Search Functionality
const searchTask = (e) =>{
if(!e) e = window.event;
while(taskContents.firstChild){
taskContents.removeChild(taskContents.firstChild);
}
const resultData = state.taskList.filter(({title})=> title.includes(e.target.value));
console.log(resultData);
resultData.map((cardData) => {
taskContents.insertAdjacentHTML("beforeend", htmlTaskContent(cardData));
});
}