lifecycle update

This commit is contained in:
Jorijn van der Graaf 2025-11-14 18:40:13 +01:00
commit 0b7a43efbd
14 changed files with 950 additions and 490 deletions

View file

@ -11,12 +11,11 @@ struct Note {
void RenderNotes();
// Global vector to store notes
std::vector<Note>* notes = new std::vector<Note>();
std::vector<HtmlElementView>* noteButtons = new std::vector<HtmlElementView>();
std::vector<Note> notes;
std::vector<HtmlElementView> noteButtons;
// Create the head section
HtmlElementView* head = new HtmlElementView("head", R"(
HtmlElementPtr head("head", R"(
<title>Note Taking App</title>
<style>
body {
@ -123,7 +122,7 @@ HtmlElementView* head = new HtmlElementView("head", R"(
)");
// Create the body section
HtmlElementView* body = new HtmlElementView("body", R"(
HtmlElementPtr body("body", R"(
<div class="app-container">
<h1>📝 Note Taking App</h1>
@ -142,16 +141,16 @@ HtmlElementView* body = new HtmlElementView("body", R"(
</div>
</div>
)");
HtmlElementView* addNoteBtn = new HtmlElementView("addNoteBtn");
HtmlElementView* noteInput = new HtmlElementView("noteInput");
HtmlElementView* loadingIndicator = new HtmlElementView("loading");
HtmlElementView* notesContainer = new HtmlElementView("notesContainer");
HtmlElementPtr addNoteBtn("addNoteBtn");
HtmlElementPtr noteInput("noteInput");
HtmlElementPtr loadingIndicator("loading");
HtmlElementPtr notesContainer ("notesContainer");
void FetchNotes() {
loadingIndicator->SetInnerHTML("<p>Loading notes...</p>");
loadingIndicator.SetInnerHTML("<p>Loading notes...</p>");
Fetch("http://localhost:3000/getNotes", [](std::string input) {
notes->clear();
notes.clear();
std::istringstream stream(input);
std::string line;
@ -169,35 +168,35 @@ void FetchNotes() {
content += line;
}
notes->push_back(Note{id, content});
notes.push_back(Note{id, content});
}
loadingIndicator->SetInnerHTML("<p>Notes loaded!</p>");
loadingIndicator.SetInnerHTML("<p>Notes loaded!</p>");
RenderNotes();
});
}
// Function to delete a note via the backend
void DeleteNote(std::uint32_t id) {
loadingIndicator->SetInnerHTML("<p>Deleting note...</p>");
loadingIndicator.SetInnerHTML("<p>Deleting note...</p>");
// Make POST request to delete note
Fetch("http://localhost:3000/deleteNote", std::to_string(id), [](std::string content) {
FetchNotes();
loadingIndicator->SetInnerHTML("<p>Note deleted!</p>");
loadingIndicator.SetInnerHTML("<p>Note deleted!</p>");
});
}
// Function to render all notes to the DOM
void RenderNotes() {
if (notes->empty()) {
notesContainer->SetInnerHTML("<p>No notes yet. Add one below!</p>");
if (notes.empty()) {
notesContainer.SetInnerHTML("<p>No notes yet. Add one below!</p>");
return;
}
std::string html = "<div class='notes-list'>";
noteButtons->clear();
for (const Note& note : *notes) {
noteButtons.clear();
for (const Note& note : notes) {
html += std::format(
R"(<div class='note-item' id='note-{}'>
<div class='note-content'>{}</div>
@ -208,10 +207,10 @@ void RenderNotes() {
}
html += "</div>";
notesContainer->SetInnerHTML(html);
notesContainer.SetInnerHTML(html);
for (const Note& note : *notes) {
HtmlElementView& view = noteButtons->emplace_back(std::format("note-{}-delete", note.id));
for (const Note& note : notes) {
HtmlElementView& view = noteButtons.emplace_back(std::format("note-{}-delete", note.id));
view.AddClickListener([id = note.id](MouseEvent event) {
DeleteNote(id);
});
@ -223,17 +222,17 @@ int main() {
FetchNotes();
// Add click listener to add note button
addNoteBtn->AddClickListener([&](MouseEvent event) {
std::string noteValue = noteInput->GetValue();
addNoteBtn.AddClickListener([&](MouseEvent event) {
std::string noteValue = noteInput.GetValue();
std::cout << noteValue << std::endl;
if (!noteValue.empty()) {
loadingIndicator->SetInnerHTML("<p>Saving note...</p>");
loadingIndicator.SetInnerHTML("<p>Saving note...</p>");
Fetch("http://localhost:3000/createNote", noteValue, [noteValue](std::string content) {
std::cout << noteValue << std::endl;
notes->push_back({static_cast<std::uint32_t>(std::stoi(content)), noteValue});
noteInput->SetValue("");
notes.push_back({static_cast<std::uint32_t>(std::stoi(content)), noteValue});
noteInput.SetValue("");
RenderNotes();
loadingIndicator->SetInnerHTML("<p>Saved note!</p>");
loadingIndicator.SetInnerHTML("<p>Saved note!</p>");
});
}
});

View file

@ -1 +1 @@
caddy file-server --listen :8080 --root bin/executable
caddy file-server --listen :8080 --root bin/executable