|
| 1 | +var notyf = new Notyf(); |
| 2 | + |
| 3 | +const formContainer = document.getElementById('form-container') |
| 4 | +const notesContainer = document.getElementById('notes-container') |
| 5 | +const myForm = document.getElementById('my-form') |
| 6 | +const editForm = document.getElementById('edit-form') |
| 7 | +const editTitle = document.getElementById('edit-title') |
| 8 | +const editDescription = document.getElementById('edit-description') |
| 9 | +const menuIcon = document.getElementById('menu-icon') |
| 10 | +const allNotes = document.getElementById('all-notes') |
| 11 | +const input = document.getElementById('input') |
| 12 | +const notes = JSON.parse(localStorage.getItem('notes')) || [] |
| 13 | + |
| 14 | +let editIndex; |
| 15 | + |
| 16 | +const displayHome = () =>{ |
| 17 | + formContainer.style.display="block" |
| 18 | + allNotes.style.display="none" |
| 19 | +} |
| 20 | +const displayNotes = () =>{ |
| 21 | + formContainer.style.display="none" |
| 22 | + allNotes.style.display="block" |
| 23 | +} |
| 24 | + |
| 25 | +myForm.addEventListener('submit',(e)=>{ |
| 26 | + e.preventDefault() |
| 27 | + |
| 28 | + let note = { |
| 29 | + |
| 30 | + title:e.target.title.value, |
| 31 | + description:e.target.description.value |
| 32 | + } |
| 33 | + myForm.reset() |
| 34 | + addNote(note) |
| 35 | + |
| 36 | +}) |
| 37 | + |
| 38 | +const addNote = (note) => { |
| 39 | + notes.push(note) |
| 40 | + notyf.success('Note Added!'); |
| 41 | + updateLs() |
| 42 | +} |
| 43 | + |
| 44 | +const editNote = (index) => { |
| 45 | + editTitle.value=notes[index].title |
| 46 | + editDescription.value=notes[index].description |
| 47 | + editIndex = index |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +const fetchNotes = () => { |
| 52 | + notesContainer.innerHTML = "" |
| 53 | + if(notes.length==0){ |
| 54 | + notesContainer.innerHTML = `<p class="fs-5 text-center">Nothing to display !!</p>` |
| 55 | + return |
| 56 | + } |
| 57 | + notes.forEach((note,index)=>{ |
| 58 | + let noteBox = document.createElement('div') |
| 59 | + noteBox.classList.add('card') |
| 60 | + noteBox.innerHTML = ` |
| 61 | + <h2 class="card-title">${note.title}</h2> |
| 62 | + |
| 63 | + <p>${note.description}</p> |
| 64 | + <div class="d-flex gap-2"> |
| 65 | + <button class="btn btn-danger"><i class="fa-solid fa-trash"></i> </button> |
| 66 | + <button class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#exampleModal"><i class="fa-solid fa-edit"></i> </button> |
| 67 | + </div> |
| 68 | + |
| 69 | + |
| 70 | + ` |
| 71 | + notesContainer.appendChild(noteBox) |
| 72 | + |
| 73 | + noteBox.querySelector('.btn-danger').addEventListener('click',()=>{ |
| 74 | + notes.splice(index,1) |
| 75 | + updateLs() |
| 76 | + notyf.success('Note deleted!'); |
| 77 | + }) |
| 78 | + noteBox.querySelector('.btn-warning').addEventListener('click',()=>{ |
| 79 | + editNote(index) |
| 80 | + }) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +editForm.addEventListener('submit',(e)=>{ |
| 85 | + e.preventDefault() |
| 86 | + notes[editIndex].title=editTitle.value |
| 87 | + notes[editIndex].description=editDescription.value |
| 88 | + editForm.reset() |
| 89 | + notyf.success('Note saved!'); |
| 90 | + updateLs() |
| 91 | + |
| 92 | +}) |
| 93 | + |
| 94 | +fetchNotes() |
| 95 | + |
| 96 | +const toastTrigger = document.getElementById('liveToastBtn') |
| 97 | +const toastLiveExample = document.getElementById('liveToast') |
| 98 | + |
| 99 | +if (toastTrigger) { |
| 100 | + const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample) |
| 101 | + toastTrigger.addEventListener('click', () => { |
| 102 | + toastBootstrap.show() |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +function toggleMenu(){ |
| 107 | + if(menuIcon.classList.contains('fa-bars')){ |
| 108 | + menuIcon.classList.replace('fa-bars','fa-xmark') |
| 109 | + }else{ |
| 110 | + menuIcon.classList.replace('fa-xmark','fa-bars') |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function updateLs(){ |
| 115 | + localStorage.setItem('notes',JSON.stringify(notes)) |
| 116 | + fetchNotes() |
| 117 | +} |
| 118 | + |
| 119 | +input.addEventListener('keyup',(e)=>{ |
| 120 | + let searchTerm = e.target.value.toUpperCase() |
| 121 | + |
| 122 | + notesContainer.querySelectorAll('.card-title').forEach((title)=>{ |
| 123 | + if(title.textContent.toUpperCase().includes(searchTerm)){ |
| 124 | + title.parentElement.style.display="" |
| 125 | + }else{ |
| 126 | + title.parentElement.style.display="none" |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | +}) |
| 131 | + |
0 commit comments