From 180e3a087c94dc52488bfd0ad16ede719fac896c Mon Sep 17 00:00:00 2001 From: Sergij Pankiv Date: Tue, 7 Nov 2023 19:39:50 +0200 Subject: [PATCH] note item path fix --- notes-functions.js | 166 +++++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/notes-functions.js b/notes-functions.js index 89de77f..5883a8d 100644 --- a/notes-functions.js +++ b/notes-functions.js @@ -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()}` \ No newline at end of file +const generateLastEdited = (timeStamp) => + `Last edited ${moment(timeStamp).fromNow()}`;