Skip to content

Commit

Permalink
note item path fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SergijPSO committed Nov 7, 2023
1 parent 8ca3c0b commit 180e3a0
Showing 1 changed file with 84 additions and 82 deletions.
166 changes: 84 additions & 82 deletions notes-functions.js
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()}`;

0 comments on commit 180e3a0

Please sign in to comment.