-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
84 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,108 @@ | ||
'use strict' | ||
"use strict"; | ||
|
||
// Read existing notes from localStorage | ||
const getSavedNotes = () => { | ||
const notesJSON = localStorage.getItem('notes') | ||
try { | ||
return notesJSON ? JSON.parse(notesJSON) : [] | ||
} catch (e) { | ||
return [] | ||
} | ||
} | ||
const notesJSON = localStorage.getItem("notes"); | ||
try { | ||
return notesJSON ? JSON.parse(notesJSON) : []; | ||
} catch (e) { | ||
return []; | ||
} | ||
}; | ||
|
||
// Save the notes to localStorage | ||
const saveNotes = (notes) => { | ||
localStorage.setItem('notes', JSON.stringify(notes)) | ||
} | ||
localStorage.setItem("notes", JSON.stringify(notes)); | ||
}; | ||
|
||
//Remove note from the list | ||
const removeNote = (id) => { | ||
const noteIndex = notes.findIndex((note) => note.id === id) | ||
if(noteIndex > -1) { | ||
notes.splice(noteIndex, 1) | ||
} | ||
} | ||
const noteIndex = notes.findIndex((note) => note.id === id); | ||
if (noteIndex > -1) { | ||
notes.splice(noteIndex, 1); | ||
} | ||
}; | ||
|
||
// Generate the DOM structure for a note | ||
const generateNoteDOM = (note) => { | ||
const noteEl = document.createElement('div') | ||
noteEl.classList.add("app-content__notes-item") | ||
const textEl = document.createElement('a') | ||
textEl.classList.add('app-content__notes-item__name') | ||
const button = document.createElement('button') | ||
button.classList.add('app-content__notes-item__button-remove') | ||
const noteEl = document.createElement("div"); | ||
noteEl.classList.add("app-content__notes-item"); | ||
const textEl = document.createElement("a"); | ||
textEl.classList.add("app-content__notes-item__name"); | ||
const button = document.createElement("button"); | ||
button.classList.add("app-content__notes-item__button-remove"); | ||
|
||
button.addEventListener('click',() => { | ||
removeNote(note.id) | ||
saveNotes(notes) | ||
renderNotes(notes, filters) | ||
}) | ||
button.addEventListener("click", () => { | ||
removeNote(note.id); | ||
saveNotes(notes); | ||
renderNotes(notes, filters); | ||
}); | ||
|
||
// Setup the remove note button | ||
|
||
// Setup the remove note button | ||
|
||
// Setup the note title text | ||
if (note.title.length > 0) { | ||
textEl.textContent = note.title | ||
} else { | ||
textEl.textContent = 'Unnamed note' | ||
} | ||
// Setup the note title text | ||
if (note.title.length > 0) { | ||
textEl.textContent = note.title; | ||
} else { | ||
textEl.textContent = "Unnamed note"; | ||
} | ||
|
||
textEl.setAttribute('href', `/edit.html#${note.id}`) | ||
noteEl.appendChild(textEl) | ||
noteEl.appendChild(button) | ||
return noteEl | ||
} | ||
textEl.setAttribute("href", `/notes-app/edit.html#${note.id}`); | ||
noteEl.appendChild(textEl); | ||
noteEl.appendChild(button); | ||
return noteEl; | ||
}; | ||
|
||
const sortNotes = (notes, sortBy) => { | ||
if(sortBy === 'byEdited') { | ||
return notes.sort((a, b) => { | ||
if(a.updatedAt > b.updatedAt) { | ||
return -1 | ||
} else if(a.updatedAt < b.updatedAt) { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
}) | ||
} else if(sortBy === 'byCreated') { | ||
return notes.sort((a, b) => { | ||
if(a.createdAt > b.createdAt) { | ||
return -1 | ||
} else if(a.createdAt < b.createdAt) { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
}) | ||
} else if(sortBy === 'alphabetical') { | ||
return notes.sort((a, b) => { | ||
if(a.title.toLowerCase() < b.title.toLowerCase()) { | ||
return -1 | ||
} else if(a.title.toLowerCase() > b.title.toLowerCase()) { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
}) | ||
} else { | ||
return notes | ||
} | ||
} | ||
if (sortBy === "byEdited") { | ||
return notes.sort((a, b) => { | ||
if (a.updatedAt > b.updatedAt) { | ||
return -1; | ||
} else if (a.updatedAt < b.updatedAt) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else if (sortBy === "byCreated") { | ||
return notes.sort((a, b) => { | ||
if (a.createdAt > b.createdAt) { | ||
return -1; | ||
} else if (a.createdAt < b.createdAt) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else if (sortBy === "alphabetical") { | ||
return notes.sort((a, b) => { | ||
if (a.title.toLowerCase() < b.title.toLowerCase()) { | ||
return -1; | ||
} else if (a.title.toLowerCase() > b.title.toLowerCase()) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else { | ||
return notes; | ||
} | ||
}; | ||
|
||
// Render application notes | ||
const renderNotes = (notes, filters) => { | ||
notes = sortNotes(notes, filters.sortBy) | ||
notes = sortNotes(notes, filters.sortBy); | ||
|
||
const filteredNotes = notes.filter((note) => note.title.toLowerCase().includes(filters.searchText.toLowerCase())) | ||
document.querySelector('#notes').innerHTML = '' | ||
const filteredNotes = notes.filter((note) => | ||
note.title.toLowerCase().includes(filters.searchText.toLowerCase()) | ||
); | ||
document.querySelector("#notes").innerHTML = ""; | ||
|
||
filteredNotes.forEach((note) => { | ||
const noteEl = generateNoteDOM(note) | ||
document.querySelector('#notes').appendChild(noteEl) | ||
}) | ||
} | ||
filteredNotes.forEach((note) => { | ||
const noteEl = generateNoteDOM(note); | ||
document.querySelector("#notes").appendChild(noteEl); | ||
}); | ||
}; | ||
|
||
const generateLastEdited = (timeStamp) => `Last edited ${moment(timeStamp).fromNow()}` | ||
const generateLastEdited = (timeStamp) => | ||
`Last edited ${moment(timeStamp).fromNow()}`; |