forked from hannah-dunn/team-renault-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
215 lines (177 loc) · 6.19 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
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
window.addEventListener("load", ()=>loadPage());
const form = document.getElementById('form');
const description = document.getElementById('description');
const dueDate = document.getElementById('dueDate');
const assignedTo = document.getElementById('assignedTo');
const taskName = document.getElementById('taskName');
const setStatus = document.getElementById('setStatus');
const submitButton = document.getElementById('submitButton');
const modalBtnDel = document.getElementById("modalBtnDel");
// TASK - 4 - Task Form Inputs Validation
function validateTaskForm() {
//validate Name length
const validateName = document.getElementById('taskName').value;
if(validateName.length === 0){
alert("Please enter Name!");
return false
}
if(validateName.length < 8){
alert("Task name should be longer than 8 characters");
return false
}
// validate Description
const validateDescription = document.getElementById('description').value;
if(validateDescription.length === 0){
alert("Please enter Description!");
return false
}
if(validateDescription.length < 15){
alert("Description should be longer than 15 characters");
return false
}
// validate AssignTo
const validateAssignedTo = document.getElementById('assignedTo').value;
if(validateAssignedTo.length === 0){
alert("Please assign to someone!");
return false
}
if(validateAssignedTo.length < 8){
alert("The name of the assigned should be longer than 8 characters");
return false
}
// validate DueDate
const validateDueDate = document.getElementById('dueDate').value;
const currentDate = new Date().toJSON().slice(0,10);
if(validateDueDate.length === 0){
alert("Due date can't be empty!");
return false
}
if(validateDueDate.length != 0 && validateDueDate < currentDate){
alert("Due date can't be later than current date!");
return false
}
return true
}
//TASK 5 -- Displaying Date
// Date & Time Display
function display_c(){
let refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
let CDate = new Date()
let NewDate=CDate.toDateString();
NewDate = NewDate + " - " + CDate.toLocaleTimeString();
document.getElementById('ct').innerHTML = NewDate;
display_c();
}
// TASK 6 -- Create a class, add tasks programmatically
// TASK 7 -- Display Task Cards
function loadPage(){
const allTasks = TaskManager.getAllTasks()
console.log(allTasks)
allTasks.map(task=>{
TaskManager.createHtmlCard(task)
})
}
form.addEventListener('submit', (event) => submitFunction(event))
function submitFunction(event){
event.preventDefault()
if(validateTaskForm()){
const target = event.target
const taskName = target.taskName.value
const description = target.description.value
const assignedTo = target.assignedTo.value
const dueDate = target.dueDate.value
const setStatus = target.setStatus.value
const newTask = new TaskManager(taskName, description, assignedTo, dueDate, setStatus)
TaskManager.createHtmlCard(newTask)
}}
class TaskManager{
static id = 0
static array = []
constructor(taskName,description,assignedTo,dueDate,setStatus){
this.id = TaskManager.id++;
this.taskName = taskName;
this.description = description;
this.assignedTo = assignedTo;
this.dueDate = dueDate;
this.setStatus = setStatus;
}
static saveToLocal(){
localStorage.setItem("tasks", JSON.stringify(TaskManager.array))
}
static getLocalData(){
return JSON.parse(localStorage.getItem("tasks"))
}
static render(object){
createHtmlTask(object)
}
static getAllTasks(){
return JSON.parse(localStorage.getItem("tasks"))
}
static createHtmlCard(object){
TaskManager.array.push(object)
TaskManager.saveToLocal()
let card = document.createElement("div")
card.innerHTML =
`<div class="card mx-3" style="margin-bottom: 10px;">
<div class="mx-3">
<label>Task Name:</label>
<h4>${object.taskName}</h4>
</div>
<div class="mx-3">
<label>Assigned to: </label>
<p>${object.assignedTo}</p>
</div>
<div class="mx-3">
<label>Due Date: </label>
<p>${object.dueDate}</p>
</div>
<div class="mx-3 ">
<label>Description:</label>
<p>${object.description}</p>
</div>
<div class="mx-3 ">
<label>Status:</label>
<p>${object.setStatus}</p>
</div>
<div class="card-footer bg-transparent border-light">
<button type="submit" class="btn btn-success done-button" >Mark as Done</button>
<button type="submit" class="delete-button btn btn-danger" >Delete</button>
</div>
</div>`
TaskManager.render(card)
}
static render(card){
const taskCardContainer = document.getElementById("taskCardContainer")
taskCardContainer.appendChild(card)
}
}
// EXPERIMENTAL DELETE BUTTON STUFF
// it doesn't work at all
// id="del" data-id=${object.id}
// id="done" data-id=${object.id}
// itemsContainer.addEventListener('click', function(e){}
function DeleteCard(taskId){
TaskManager.removeTasks(taskId)
displayCards()
}
document.getElementById("taskCardContainer").addEventListener("click", (e) => {
let element = e.target
if (element === e.currentTarget) {
return
}
if (element.nodeName === "button"&&element.className==="delete-button") {
removeTasks(Number(element.id))
displayCards()
TaskManager.saveToLocal(taskList)
}
})
function removeTasks(id) {
for(let i=0;i<taskList.length;i++) {
if (taskList[i].id===id){
taskList.splice(i,1)
}
}
}