-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (57 loc) · 2.38 KB
/
script.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
const taskContainer = document.querySelector(".task_container");
let globalStore = [];//array of objects
console.log(taskContainer);
const generateNewCard = (taskData) => `
<div class="col-sm-12 col-md-6 col-lg-4">
<div class="card">
<div class="card-header d-flex justify-content-end gap-2">
<button type="button" class="btn btn-outline-success"><i class="fas fa-pencil-alt"></i></button>
<button type="button" class="btn btn-outline-danger" id=${taskData.id} onclick="deleteCard.apply(this,arguments)"><i class="fas fa-trash-alt" id=${taskData.id} onclick="deleteCard.apply(this,arguments)"></i></button>
</div>
<div class="card-body">
<img class="card-img-top" src=${taskData.imageUrl} alt="...">
<h5 class="card-title mt-3 fw-bolder text-primary">${taskData.taskTitle}</h5>
<p class="card-text">${taskData.taskDescription}</p>
<a href="#" class="btn btn-primary">${taskData.taskType}</a>
</div>
</div>
</div>
`;
const loadinitialData = () => {
//localstorage to get tasky card data
const getCardData = localStorage.getItem("tasky");
//covert to normal object
const {cards} = JSON.parse(getCardData);
//loop over those array of task obejct to create HTML card , inject it to DOM
cards.map((cardObject) => {
taskContainer.insertAdjacentHTML("beforeend", generateNewCard(cardObject));
//update our globalStore
globalStore.push(cardObject);
}
)
};
//Delete function
const deleteCard = (event) => {
event = window.event;
const targetID = event.target.id;
const tagname = event.target.tagName;
globalStore = globalStore.filter((cardObject) => cardObject.id !== targetID);
localStorage.setItem("tasky",JSON.stringify({cards: globalStore}));
if(tagname === "BUTTON") {
return taskContainer.removeChild(event.target.parentNode.parentNode.parentNode);
} else {
return taskContainer.removeChild(event.target.parentNode.parentNode.parentNode.parentNode);
}
};
const saveChanges = () => {
const taskData = {
id: `${Date.now()}`,
imageUrl: document.getElementById("imageurl").value,
taskTitle: document.getElementById("tasktitle").value,
taskType: document.getElementById("tasktype").value,
taskDescription: document.getElementById("taskdescription").value
};
taskContainer.insertAdjacentHTML("beforeend", generateNewCard(taskData));
globalStore.push(taskData);
localStorage.setItem("tasky",JSON.stringify({cards:globalStore}));
};