Skip to content

Commit 0efa0b5

Browse files
author
Max Kelly
committed
145. Converting Notes App: Part IV
1 parent d8c4cbf commit 0efa0b5

13 files changed

+170
-18
lines changed

Diff for: notes-app/node_modules/.cache/uglifyjs-webpack-plugin/content-v2/sha512/44/ab/3c001212f37e885e63cf9f7da6f928e33954f683e56f561bb794761241227d18d4b2c95898ffb4677338ae9948fd4008b14cea8650e08cea23f00b4b6f24

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/node_modules/.cache/uglifyjs-webpack-plugin/content-v2/sha512/b8/43/93d6a793e636dce7ae92379e690015bd9384aff80628ccbde77da99bc7fa550c069845a3515665a303957d9f06222d655c367299fa4d652721ffdaa5c2b9

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/node_modules/.cache/uglifyjs-webpack-plugin/index-v5/49/7e/0fa652999ef4ddc6a3f1063c36e5a54a9dc3b302b7012fcb6b5fb25f1233

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/node_modules/.cache/uglifyjs-webpack-plugin/index-v5/87/e4/42957236f21c042cadcb0e8e3e7ed9ae62ef17dca6315ed41c3d0e8504bc

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/public/scripts/bundle.js

-1
This file was deleted.

Diff for: notes-app/public/scripts/edit-bundle.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/public/scripts/edit-bundle.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/public/scripts/index-bundle.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/public/scripts/index-bundle.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: notes-app/src/edit.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,40 @@
1-
console.log('edit.js')
1+
import { initializedEditPage, generateLastEdited } from './views'
2+
import { updateNote, removeNote } from './notes'
3+
4+
const titleElement = document.querySelector("#note-title");
5+
const bodyElement = document.querySelector("#note-body");
6+
const dateElement = document.querySelector("#edited");
7+
const removeElement = document.querySelector("#remove-note");
8+
const noteId = location.hash.substring(1);
9+
10+
initializedEditPage(noteId)
11+
12+
13+
// This saves the title of the note.
14+
titleElement.addEventListener("input", e => {
15+
const note = updateNote(noteId, {
16+
title: e.target.value
17+
})
18+
dateElement.textContent = generateLastEdited(note.updatedAt)
19+
});
20+
21+
// This saves the body of the note.
22+
bodyElement.addEventListener("input", e => {
23+
const note = updateNote(noteId, {
24+
body: e.target.value
25+
})
26+
dateElement.textContent = generateLastEdited(note.updatedAt);
27+
});
28+
29+
// This removes the note and redirects the user back the /index.html
30+
removeElement.addEventListener("click", e => {
31+
removeNote(noteId);
32+
location.assign("/index.html");
33+
});
34+
35+
// The below code lets the console listen and any changes made in the window will update across other windows.
36+
window.addEventListener("storage", e => {
37+
if (e.key === "notes") {
38+
initializedEditPage(noteId)
39+
}
40+
});

Diff for: notes-app/src/index.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
import { getNotes, createNote, removeNote, updateNote } from './notes.js'
2-
import { getFilters, setFilters } from './filters.js'
1+
import { createNote } from './notes.js'
2+
import { setFilters } from './filters.js'
3+
import { renderNotes } from './views'
34

4-
// console.log(getNotes())
5-
// createNote()
6-
//removeNote("s");
7-
// updateNote("7e114e26-2e28-4bc6-8afa-c277fd6f1fd6", {
8-
// title: 'Max is awesome',
9-
// body: 'This is the body'
10-
// });
11-
// console.log(getNotes());
5+
renderNotes(); // This calls the function before the user interacts with the page.
126

13-
console.log(getFilters())
14-
setFilters({
15-
searchText: 'Office',
16-
sortBy: 'byCreated'
17-
})
18-
console.log(getFilters())
7+
document.querySelector("#create-button").addEventListener("click", e => {
8+
const id = createNote()
9+
location.assign(`/edit.html#${id}`); // Makes it so if the button is clicked they're redirected.
10+
});
11+
12+
document.querySelector("#search-text").addEventListener("input", e => {
13+
setFilters({
14+
searchText: e.target.value
15+
})
16+
renderNotes();
17+
}); // This calls when the user interacts with the function.
18+
19+
document.querySelector("#filter-by").addEventListener("change", e => {
20+
setFilters({
21+
sortBy: e.target.value
22+
})
23+
renderNotes();
24+
});
25+
26+
// This makes it so if the title was changed on the edit note page it will update the note page while tab is open
27+
window.addEventListener("storage", e => {
28+
if (e.key === "notes") {
29+
renderNotes();
30+
}
31+
});

Diff for: notes-app/src/notes.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const createNote = () => {
4242
dateLastEdited: timeStamp
4343
});
4444
saveNotes()
45+
return id
4546
}
4647

4748
// Remove a note from the list
@@ -112,6 +113,7 @@ const updateNote = (id, updates) => {
112113
}
113114

114115
saveNotes()
116+
return note
115117
}
116118

117119

Diff for: notes-app/src/views.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import moment from 'moment'
2+
import { getFilters } from './filters'
3+
import { sortNotes, getNotes } from './notes'
4+
5+
// Generate the DOM Structure for a note
6+
const generateNoteDOM = note => {
7+
const noteEl = document.createElement("a");
8+
const textEl = document.createElement("p"); // this makes it so the text is now an anchor tag linking them to edit.html
9+
const button = document.createElement("button");
10+
const statusEl = document.createElement("p");
11+
12+
// Setup the remove note button
13+
button.textContent = "X";
14+
noteEl.appendChild(button);
15+
button.addEventListener("click", function(e) {
16+
removeNote(note.id);
17+
saveNotes(notes);
18+
renderNotes(notes, filters);
19+
});
20+
21+
// Setup the note title text
22+
if (note.title.length > 0) {
23+
textEl.textContent = note.title;
24+
} else {
25+
textEl.textContent = "Unnamed note";
26+
}
27+
textEl.classList.add('list-item__title')
28+
noteEl.appendChild(textEl);
29+
30+
// Setup the link
31+
noteEl.setAttribute("href", `/edit.html#${note.id}`); // This changes the text to be a anchor tag linking them to edit.html and attaches the note id onto the href
32+
noteEl.classList.add('list-item') // classList.add - adds a class list to a javascript
33+
34+
// Setup the status message
35+
statusEl.textContent = generateLastEdited(note.updatedAt)
36+
statusEl.classList.add('list-item__subtitle')
37+
noteEl.appendChild(statusEl)
38+
return noteEl;
39+
};
40+
41+
// Render application notes
42+
// The below code takes all of the notes and filters to which one is relevant
43+
const renderNotes = () => {
44+
const notesEl = document.querySelector("#notes");
45+
const filters = getFilters()
46+
const notes = sortNotes(filters.sortBy)
47+
const filteredNotes = notes.filter( (note) => note.title.toLowerCase().includes(filters.searchText.toLowerCase()));
48+
49+
notesEl.innerHTML = ''
50+
51+
if (filteredNotes.length > 0) {
52+
filteredNotes.forEach(note => {
53+
const noteEl = generateNoteDOM(note); // This pulls code from notes-function.js
54+
notesEl.appendChild(noteEl)
55+
});
56+
} else {
57+
const emptyMessage = document.createElement('p')
58+
emptyMessage.textContent = 'No notes to display'
59+
emptyMessage.classList.add('empty-message')
60+
notesEl.appendChild(emptyMessage)
61+
}
62+
};
63+
64+
const initializedEditPage = (noteId) => {
65+
const titleElement = document.querySelector("#note-title");
66+
const bodyElement = document.querySelector("#note-body");
67+
const dateElement = document.querySelector("#edited");
68+
const notes = getNotes();
69+
const note = notes.find(note => note.id === noteId);
70+
71+
// If there is no note run the code
72+
if (!note) {
73+
location.assign(`/index.html`);
74+
}
75+
76+
titleElement.value = note.title;
77+
bodyElement.value = note.body;
78+
dateElement.textContent = generateLastEdited(notes.updatedAt); // Records the last edited note
79+
};
80+
81+
// Generate the last edited message
82+
const generateLastEdited = timeStamp => {
83+
return `Last Edited: ${moment(timeStamp).fromNow()}`;
84+
};
85+
86+
export { generateNoteDOM, renderNotes, generateLastEdited, initializedEditPage };
87+

0 commit comments

Comments
 (0)