-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
848 lines (722 loc) · 26.5 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// VARIABLES
// main dom
const appBody = document.body;
const mainSection = document.getElementById('main-section');
// new user constructor
let userName;
let User = function (firstName, lastName, email, password, agreedToTerms) {
this.firstName = firstName,
this.lastName = lastName,
this.email = email,
this.password = password,
this.agreedToTerms = agreedToTerms,
this.lists = []
}
// new list constructor
const List = function (name) {
this.name = name;
this.items = [];
};
// new todo constructor
const Todo = function (name) {
this.name = name;
this.checked = false;
}
// TEMPLATES
const introHeader = document.createElement('header');
function introHeaderTemp() {
introHeader.classList.add('intro-header');
introHeader.innerHTML = `
<h1 id='app-title'>TODO APP</h1>
<h5 id='tagline-description'>Set it, don't forget it</h5>`;
return introHeader;
}
const introButtons = document.createElement('div');
function introButtonsTemp() {
introButtons.classList.add('btn-intro-div');
introButtons.innerHTML = `
<button id='login-btn' class='btn-lg btn-teal'>Login</button>
<button id='signup-btn' class='btn-lg btn-white'>Sign Up</button>`;
return introButtons;
}
// load login form
const loginDiv = document.createElement('div');
const loginForm = document.createElement('form');
function loginFormTemp() {
loginDiv.classList.add('login-div', 'form-div');
loginForm.id = 'login-form';
loginForm.classList.add('app-form');
loginDiv.innerHTML = `
<h2>Log In</h2>`;
loginForm.innerHTML = `
<input type="text" placeholder='Email Address' class='login-email form-input' />
<input type="password" placeholder='Password' class='login-password form-input' />
<button id='login-form-submit' class='btn-lg btn-teal'>Log In</button>`;
loginDiv.appendChild(loginForm);
return loginDiv;
}
// load sign up form
const signupDiv = document.createElement('div');
const signupForm = document.createElement('form');
function signupFormTemp() {
signupDiv.classList.add('signup-div', 'form-div');
signupForm.id = 'signup-form'
signupForm.classList.add('app-form');
signupDiv.innerHTML = `<h2>Sign Up</h2>`;
signupForm.innerHTML = `
<input class='form-input' type="text" placeholder='First Name' required>
<input class='form-input' type="text" placeholder='Last Name' required>
<input class='form-input' type="text" placeholder='Email Address' required>
<input class='form-input' type="password" placeholder='Password' required>
<div class='terms-div'>
<input id='agree-to-terms' class='form-checkbox' type="checkbox">
<label For="agree-to-terms">I Agree to Terms of Use</label>
</div>
<button id='signup-form-submit' class='btn-lg' type='submit'>Sign Up</button>`;
signupDiv.appendChild(signupForm);
return signupDiv;
}
// load the signup confirmation view
const signupConfirmationDiv = document.createElement('div');
function signupConfirmationTemp(firstName, lastName, emailAddress) {
signupConfirmationDiv.classList.add('confirmation-div');
signupConfirmationDiv.innerHTML = `
<h2>Please Confirm Your Info</h2>
<div class='conf-table-div'>
<div class='conf-label-div'>
<p class='conf-label'>First Name</p>
<p class='conf-label'>Last Name</p>
<p class='conf-label'>Email Address</p>
</div>
<div class='conf-info-div'>
<p class='conf-label'>${firstName}</p>
<p class='conf-label'>${lastName}</p>
<p class='conf-label'>${emailAddress}</p>
</div>
</div>
<button id='signup-confirm-btn' class='btn-lg btn-teal'>Go to Dashboard</button>`
return signupConfirmationDiv;
}
// dashboard header
const dashboardHeader = document.createElement('header');
function dashboardHeaderTemp() {
dashboardHeader.classList.add('dashboard-header');
dashboardHeader.innerHTML = `
<h1 id='app-title-dashboard'>TODO APP</h1>`;
return dashboardHeader;
}
// add new list form and button
const addListForm = document.createElement('form');
function addListFormTemp() {
addListForm.id = 'add-list-form';
addListForm.classList.add('add-list-form');
addListForm.innerHTML = `
<button id='add-list-btn' class='btn-md btn-teal add-list-btn'>+</button>
<input type="text" class='add-list-input' placeholder='list name'>`
return addListForm;
}
// list container
const listContainer = document.createElement('div');
function listContainerTemp() {
let userData = JSON.parse(localStorage.getItem(sessionUser));
listContainer.classList.add('list-container');
if (userData.lists.length < 1) {
listContainer.innerHTML = `
<div class='no-list-msg'>
<span class='material-icons md-36'>arrow_upward</span>
<p>Use the + button to add a list</p>
</div>`;
} else {
listContainer.innerHTML = `
<ul class='list-ul'>
${userData.lists.map((list) => `${listItemTemp(list.name)}`).join('')}
</ul>`;
}
return listContainer;
}
function listItemTemp(name) {
return `
<li class='list-item'>
<button class='list-item-btn'>
<span class='item-text'>${name}</span>
</button>
<div class='list-management'>
<button class='list-edit-btn'>
<span class='material-icons list-edit-span'>mode_edit</span>
</button>
<button class='remove-item'>X</button>
</div>
</li>`;
}
let updatedListItem = document.createElement('li');
function updatedListItemTemp(name) {
updatedListItem.classList.add('list-item');
updatedListItem.innerHTML = `
<button class='list-item-btn'>
<span class='item-text'>${name}</span>
</button>
<div class='list-management'>
<button class='list-edit-btn'>
<span class='material-icons list-edit-span'>mode_edit</span>
</button>
<button class='remove-item'>X</button>
</div>`;
return updatedListItem;
}
function listItemEditTemp(name) {
return `
<li class='list-item-edit-view'>
<form class='edit-list-form'>
<input class='list-edit-input' type=text value='${name}' required>
</form>
</li>`;
}
// todo item header
const todoHeaderDiv = document.createElement('div');
function todoHeaderTemp(list) {
todoHeaderDiv.classList.add('todo-header-div');
todoHeaderDiv.innerHTML = `
<h3 class='todoHeader'>${list}</h3>`;
return todoHeaderDiv;
}
// add todo items form and button
const addTodoForm = document.createElement('form');
function addTodoFormTemp() {
addTodoForm.classList.add('add-todo-form');
addTodoForm.innerHTML = `
<button class='add-todo-btn btn-sm btn-white' type='submit'>+</button>
<input class='add-todo-input' type="text" placeholder = 'List Item' required/>`
return addTodoForm;
}
// todo container and todo items
// initial variables to be used in the function
const todoContainer = document.createElement('div');
let todoItems;
function todoContainerTemp(listName) {
// pull data from local storage and store as a variable
let userData = JSON.parse(localStorage.getItem(sessionUser));
// add class to containing div
todoContainer.classList.add('todo-container');
for (let list of userData.lists) {
if (list.name === listName) {
todoItems = list;
}
}
todoContainer.innerHTML = `
<ul class='todo-list'>
${todoItems.items.map((todo) => {
return `${todoItemTemp(todo.name, todo.checked)}`
}).join('')}
</ul>`
return todoContainer;
}
function todoItemTemp(name, check) {
return `
<li class='todo-item'>
<div class=''checkbox-item>
<input class='checkbox' type="checkbox" ${check ? `checked` : ``} />
<span class='todo-text'>${name}</span>
</div>
<div class='todo-management'>
<a href="" class='todo-edit-link'>
<span class='material-icons todo-edit-btn'>mode_edit</span>
</a>
<button class='btn-remove remove-todo'>X</button>
</div>
</li>`
}
// updated todo item
let updatedTodoItem = document.createElement('li');
function updatedTodoItemTemp(name, check) {
updatedTodoItem.classList.add('todo-item');
updatedTodoItem.innerHTML = `
<div class=''checkbox-item>
<input class='checkbox' type="checkbox" ${check ? `checked` : ``} />
<span class='todo-text'>${name}</span>
</div>
<div class='todo-management'>
<a href="" class='todo-edit-link'>
<span class='material-icons todo-edit-btn'>mode_edit</span>
</a>
<button class='btn-remove remove-todo'>X</button>
</div>`;
return updatedTodoItem;
}
// account settings page
const accountSettingsHeader = document.createElement('div');
function accountSettingsHeaderTemp() {
accountSettingsHeader.classList.add('account-settings-header-div');
accountSettingsHeader.innerHTML = `
<h3 class='account-settings-header'>Account Settings</h3>`;
return accountSettingsHeader;
}
const accountSettingsForm = document.createElement('form');
function accountSettingsFormTemp() {
let userData = JSON.parse(localStorage.getItem(sessionUser));
accountSettingsForm.classList.add('user-info-form');
accountSettingsForm.innerHTML = `
<input type="text" class='user-first-name' value='${userData.firstName}' placeholder='First Name' required />
<input type="text" class='user-last-name' value='${userData.lastName}' placeholder='Last Name' required />
<input type="text" class='user-email' value='${userData.email}' placeholder='Email Address' required />
<input type="password" class='user-password' value='${userData.password}' required />
<input type="submit" class='account-settings-submit btn btn-lg btn-teal' value='Save & Return' />`;
return accountSettingsForm;
}
// list edit view
let editListItem = document.createElement('li');
let editListForm = document.createElement('form');
// list item in edit view
function editlistItemTemp(name) {
editListItem.classList.add('list-item-edit-view');
editListItem.appendChild(editListFormTemp(name));
return editListItem;
}
// list edit form
function editListFormTemp(formValue) {
editListForm.classList.add('edit-list-form');
editListForm.innerHTML = `
<input class='list-edit-input' type="text" value='${formValue}' placeholder='List Name' required />`;
// event listener placed inside of templating function so formValue is accessible
editListForm.addEventListener('submit', (e) => {
e.preventDefault();
let formInput = e.target.children[0].value.trim();
let listItemParent = e.target.parentElement.parentElement;
let listItemChild = e.target.parentElement;
updateListName(formValue, formInput, listItemParent, listItemChild);
});
return editListForm;
}
// todo edit view
let editTodoItem = document.createElement('li');
let editTodoForm = document.createElement('form');
// todo item in edit view
function editTodoItemTemp(name) {
editTodoItem.classList.add('todo-item-edit-view');
editTodoItem.appendChild(editTodoItemFormTemp(name));
return editTodoItem;
}
function editTodoItemFormTemp(formValue) {
editTodoForm.classList.add('todo-edit-form');
editTodoForm.innerHTML = `
<input class='todo-edit-input' type="text" value='${formValue}' required />`;
editTodoForm.addEventListener('submit', (e) => {
e.preventDefault();
let formInput = e.target.children[0].value.trim();
let listTitle = e.target.parentElement.parentElement.parentElement.parentElement.children[2].children[0].innerText;
let todoItemParent = e.target.parentElement.parentElement;
let todoItemChild = e.target.parentElement;
updateTodoName(formValue, formInput, listTitle, todoItemParent, todoItemChild);
})
return editTodoForm;
}
// dashboard navigation bar
const primaryNav = document.createElement('nav');
function primaryNavTemp() {
primaryNav.classList.add('primary-nav');
primaryNav.innerHTML = `
<button id='account-settings-btn' class='account-settings-btn btn-teal'>Account Settings</button>
<button id='signout-btn' class='signout-btn btn-white'>Sign Out</button>`
return primaryNav;
}
// todo page or secondary nav
const secondaryNav = document.createElement('nav');
function secondaryNavTemp() {
secondaryNav.classList.add('secondary-nav');
secondaryNav.innerHTML = `
<button class='secondary-back-btn btn-teal'>Back to Dashboard</button>
<button class='account-settings-btn btn-teal'>Account Settings</button>
<button class='signout-btn btn-white'>Sign Out</button>`;
return secondaryNav;
}
// tertiary nav
const tertiaryNav = document.createElement('nav');
function tertiaryNavTemp() {
tertiaryNav.classList.add('tertiary-nav')
tertiaryNav.innerHTML = `
<button class='secondary-back-btn btn-teal'>Back to Dashboard</button>
<button id='signout-btn' class='signout-btn btn-white'>Sign Out</button>`;
return tertiaryNav;
}
// FUNCTIONS
// set signup inputs to local storage
function newUserLocalStorage(firstName, lastName, emailAddress, pass, agreeToTerms) {
let newUser = new User(firstName, lastName, emailAddress, pass, agreeToTerms);
localStorage.setItem(emailAddress, JSON.stringify(newUser));
}
function loginValidator(emailAddres, pass, form) {
let userData = JSON.parse(localStorage.getItem(emailAddres));
let errorArr = [];
if (document.querySelector('.error-div')) {
loginDiv.lastChild.remove();
}
try {
// empty email field
if (emailAddres === '' || emailAddres === null) {
errorArr.push('Please enter an email address');
}
// empty password
else if (pass === '' || pass === null) {
errorArr.push('Please enter a password');
}
// incorrect password
else if (emailAddres === userData['email'] && pass !== userData['password']) {
errorArr.push('Invalid password')
}
// incorrect email
else if (emailAddres !== userData['email']) {
errorArr.push('Invalid email address')
}
} catch {
errorArr.push('Invalid login')
}
if (errorArr.length > 0) {
let errorDiv = document.createElement('div');
errorDiv.classList.add('error-div');
errorDiv.innerHTML = `
<ul>
${errorArr.map((error) => `<li>${error}</li>`).join('')}
</ul>`;
loginDiv.appendChild(errorDiv);
form.children[0].value = '';
form.children[1].value = '';
errorArr = [];
} else {
sessionUser = emailAddres;
mainSection.replaceChild(dashboardHeaderTemp(), introHeaderTemp());
mainSection.replaceChild(primaryNavTemp(), loginFormTemp());
mainSection.appendChild(addListFormTemp());
mainSection.appendChild(listContainerTemp());
}
}
// validates signup form, displays error and loads confirmation
function signupValidator(fname, lname, email, password, terms, form) {
let errorArr = [];
if (document.querySelector('.error-div')) {
signupDiv.lastChild.remove();
}
if (password.length < 8) {
errorArr.push('Your password must be 8 or more characters');
}
if (!terms) {
errorArr.push('You must agree to the terms of use')
}
if (JSON.parse(localStorage.getItem(email))) {
errorArr.push('This email is already is use');
}
if (errorArr.length > 0) {
let errorDiv = document.createElement('div');
errorDiv.classList.add('error-div');
errorDiv.innerHTML = `
<ul>
${errorArr.map((error) => `<li>${error}</li>`).join('')}
</ul>`;
signupDiv.appendChild(errorDiv);
errorArr = [];
} else {
sessionUser = email;
newUserLocalStorage(fname, lname, email, password, terms);
mainSection.replaceChild(signupConfirmationTemp(fname, lname, email), signupFormTemp());
}
}
// add list items to local storage
function addListToLocalStorage(list) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
userData.lists.push(new List(list));
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
// add todo to local storage
function addTodosToLocalStorage (todoItem, listItem) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
for (let list of userData.lists) {
if (list.name === listItem) {
list.items.push(new Todo(todoItem));
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
}
}
// delete a list from the dashboard UI
function removeList(listItem) {
listItem.remove();
removeListFromLocalStorage(listItem.children[0].children[0].textContent);
if (!document.querySelector('.list-item')) {
listContainer.innerHTML = `
<div class='no-list-msg'>
<span class='material-icons md-36'>arrow_upward</span>
<p>Use the + button to add a list</p>
</div>`
} else {
return;
}
}
// delete list items from local storage
function removeListFromLocalStorage (listItem) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
let userLists = userData.lists;
for(let i in userLists) {
if (listItem === userLists[i].name) {
userLists.splice(i, 1);
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
}
}
// delete a todo item from the dashboard UI
function removeTodo(todoItem, listName) {
todoItem.parentElement.parentElement.remove();
removeTodoFromLocalStorage(todoItem.parentElement.parentElement.children[0].children[1].textContent, listName);
}
// delete a todo item from local storage
function removeTodoFromLocalStorage(todoItem, listName) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
for (let i of userData.lists) {
if (i.name === listName) {
let listArr = i.items;
let todoIndex = listArr.indexOf(todoItem);
listArr.splice(todoIndex, 1);
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
}
}
function updateUserInfo(first, last, email, password) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
localStorage.removeItem(sessionUser);
sessionUser = email;
userData.firstName = first;
userData.lastName = last;
userData.email = email;
userData.password = password;
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
function checkedStatusLocalStorage(listName, itemName) {
const userData = JSON.parse(localStorage.getItem(sessionUser));
// loop through list items
for (let list of userData.lists) {
if (list.name === listName) {
// loop through todo items
for (let item of list.items) {
if (item.name === itemName) {
// if checked is false, mark true
if (item.checked === false) {
item.checked = true;
localStorage.setItem(sessionUser, JSON.stringify(userData));
return;
}
// if checked is true, mark false
if (item.checked === true) {
item.checked = false;
localStorage.setItem(sessionUser, JSON.stringify(userData));
return;
}
}
}
}
}
}
function updateListName(oldList, newList, parent, child) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
for (let list of userData.lists) {
if (list.name == oldList) {
list.name = newList;
localStorage.setItem(sessionUser, JSON.stringify(userData));
}
}
parent.replaceChild(updatedListItemTemp(newList), child);
}
function updateTodoName(oldTodo, newTodo, listName, parent, child) {
let userData = JSON.parse(localStorage.getItem(sessionUser));
for (let list of userData.lists) {
if (list.name === listName) {
for (let todo of list.items) {
if (todo.name === oldTodo) {
todo.name = newTodo;
localStorage.setItem(sessionUser, JSON.stringify(userData));
parent.replaceChild(updatedTodoItemTemp(newTodo, todo.checked), child);
}
}
}
}
}
// EVENT LISTENERS
mainSection.appendChild(introHeaderTemp());
mainSection.appendChild(introButtonsTemp());
// load signin/signup page
introButtons.addEventListener('click', (e) => {
if (e.target.id === 'login-btn') {
mainSection.replaceChild(loginFormTemp(), introButtonsTemp());
}
if (e.target.id === 'signup-btn') {
mainSection.replaceChild(signupFormTemp(), introButtonsTemp());
}
});
// submitting the login form
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
let emailInput = e.target.children[0].value.toLowerCase().trim();
let passwordInput = e.target.children[1].value.trim();
loginValidator(emailInput, passwordInput, e.target);
});
// submitting the signup form
signupForm.addEventListener('submit', (e) => {
e.preventDefault();
let inputFName = e.target.children[0].value.trim();
let inputLName = e.target.children[1].value.trim();
let inputEmail = e.target.children[2].value.toLowerCase().trim();
let inputPassword = e.target.children[3].value.trim();
let agreeToTerms = e.target.children[4].children[0].checked;
signupValidator(inputFName, inputLName, inputEmail, inputPassword, agreeToTerms, e.target);
});
// confirming user info and signing into the dashboard
signupConfirmationDiv.addEventListener('click', (e) => {
if (e.target.id === 'signup-confirm-btn') {
mainSection.replaceChild(dashboardHeaderTemp(), introHeaderTemp());
mainSection.replaceChild(primaryNavTemp(), signupConfirmationTemp());
mainSection.appendChild(addListFormTemp());
mainSection.appendChild(listContainerTemp());
}
})
// add list items
addListForm.addEventListener('submit', (e) => {
e.preventDefault();
let formInput = e.target.children[1].value.trim();
// remove existing DOM element
listContainer.remove();
// update local storage
addListToLocalStorage(formInput);
// go into list item
mainSection.replaceChild(secondaryNavTemp(), primaryNavTemp());
mainSection.replaceChild(todoHeaderTemp(formInput), addListFormTemp());
mainSection.appendChild(addTodoFormTemp());
mainSection.appendChild(todoContainerTemp(formInput));
// clear form of input value
e.target.children[1].value = '';
});
// list item interaction
listContainer.addEventListener('click', (e) => {
e.preventDefault();
// deleting a list item
if (e.target.classList.contains('remove-item')) {
removeList(e.target.parentElement.parentElement);
}
// entering the list item
if (e.target.classList.contains('item-text')) {
mainSection.replaceChild(secondaryNavTemp(), primaryNavTemp());
mainSection.replaceChild(todoHeaderTemp(e.target.innerText), addListFormTemp());
mainSection.replaceChild(addTodoFormTemp(), listContainerTemp());
mainSection.appendChild(todoContainerTemp(e.target.innerText));
}
// editing the item's text
if (e.target.classList.contains('list-edit-span')) {
let listParent = e.target.parentElement.parentElement.parentElement.parentElement;
let listItem = e.target.parentElement.parentElement.parentElement;
let listName = e.target.parentElement.parentElement.parentElement.children[0].children[0].innerText;
listParent.replaceChild(editlistItemTemp(listName), listItem);
}
});
// add todo items
addTodoForm.addEventListener('submit', (e) => {
e.preventDefault();
let currentList = todoHeaderDiv.children[0].innerText;
// remove existing DOM element
todoContainer.remove();
// update local storage
addTodosToLocalStorage(e.target.children[1].value, currentList);
// add DOM element with updated todo item
mainSection.appendChild(todoContainerTemp(currentList));
// clear form of input value
e.target.children[1].value = '';
});
// todo item interaction
todoContainer.addEventListener('click', (e) => {
let currentList = todoHeaderDiv.children[0].innerText;
// deleting todo item
if (e.target.classList.contains('remove-todo')) {
removeTodo(e.target, currentList);
}
// checking todo box
if (e.target.classList.contains('checkbox')) {
checkedStatusLocalStorage(todoHeaderDiv.children[0].textContent, e.target.nextElementSibling.textContent);
}
// editing todo item
if (e.target.classList.contains('todo-edit-btn')) {
e.preventDefault();
let todoParent = e.target.parentElement.parentElement.parentElement.parentElement;
let todoItem = e.target.parentElement.parentElement.parentElement;
let todoName = e.target.parentElement.parentElement.parentElement.children[0].children[1].innerText;
todoParent.replaceChild(editTodoItemTemp(todoName), todoItem);
}
});
// submitting new account info
accountSettingsForm.addEventListener('submit', (e) => {
e.preventDefault();
updateUserInfo(
e.target.children[0].value.trim(),
e.target.children[1].value.trim(),
e.target.children[2].value.toLowerCase().trim(),
e.target.children[3].value.trim()
);
mainSection.replaceChild(primaryNavTemp(), tertiaryNavTemp());
mainSection.replaceChild(addListFormTemp(), accountSettingsHeaderTemp());
mainSection.replaceChild(listContainerTemp(), accountSettingsFormTemp());
});
// primary nav intergation
primaryNav.addEventListener('click', (e) => {
// account settings
if (e.target.classList.contains('account-settings-btn')) {
mainSection.replaceChild(tertiaryNavTemp(), primaryNavTemp());
addListForm.remove();
listContainer.remove();
mainSection.appendChild(accountSettingsHeaderTemp());
mainSection.appendChild(accountSettingsFormTemp());
}
// signout
if (e.target.classList.contains('signout-btn')) {
while(mainSection.firstChild) {
mainSection.removeChild(mainSection.firstChild);
}
mainSection.appendChild(introHeaderTemp());
mainSection.appendChild(introButtonsTemp());
}
});
// secondary nav interagtion
secondaryNav.addEventListener('click', (e) => {
// back button
if (e.target.classList.contains('secondary-back-btn')) {
mainSection.replaceChild(primaryNavTemp(), secondaryNavTemp());
todoHeaderDiv.remove();
mainSection.replaceChild(addListFormTemp(), addTodoFormTemp());
mainSection.replaceChild(listContainerTemp(), todoContainerTemp());
}
// account settings
if (e.target.classList.contains('account-settings-btn')) {
mainSection.replaceChild(tertiaryNavTemp(), secondaryNavTemp());
todoHeaderDiv.remove()
addTodoForm.remove();
todoContainer.remove();
mainSection.appendChild(accountSettingsHeaderTemp());
mainSection.appendChild(accountSettingsFormTemp());
}
// signout
if (e.target.classList.contains('signout-btn')) {
while(mainSection.firstChild) {
mainSection.removeChild(mainSection.firstChild);
}
mainSection.appendChild(introHeaderTemp());
mainSection.appendChild(introButtonsTemp());
}
});
// tertiary nav integration
tertiaryNav.addEventListener('click', (e) => {
// back to dashboard
if (e.target.classList.contains('secondary-back-btn')) {
mainSection.replaceChild(primaryNavTemp(), tertiaryNavTemp());
mainSection.replaceChild(addListFormTemp(), accountSettingsHeaderTemp());
mainSection.replaceChild(listContainerTemp(), accountSettingsFormTemp());
}
// signout
if (e.target.classList.contains('signout-btn')) {
while(mainSection.firstChild) {
mainSection.removeChild(mainSection.firstChild);
}
mainSection.appendChild(introHeaderTemp());
mainSection.appendChild(introButtonsTemp());
}
});