import Crafter.CppDOM; import std; using namespace Crafter; using namespace Crafter::CppDOMBindings; // Structure to represent a note struct Note { std::uint32_t id; std::string content; }; void RenderNotes(); std::vector notes; std::vector noteButtons; // Create the head section HtmlElementPtr head("head", R"( Note Taking App )"); // Create the body section HtmlElementPtr body("body", R"(

📝 Note Taking App

Add New Note


Your Notes

)"); HtmlElementPtr addNoteBtn("addNoteBtn"); HtmlElementPtr noteInput("noteInput"); HtmlElementPtr loadingIndicator("loading"); HtmlElementPtr notesContainer ("notesContainer"); void FetchNotes() { loadingIndicator.SetInnerHTML("

Loading notes...

"); Fetch("http://localhost:3000/getNotes", [](std::string input) { notes.clear(); std::istringstream stream(input); std::string line; while (std::getline(stream, line)) { // Skip empty lines if (line.empty()) continue; // First line is ID std::uint32_t id = std::stoi(line); // Read content until the next empty line std::string content; while (std::getline(stream, line) && !line.empty()) { if (!content.empty()) content += "\n"; // preserve line breaks content += line; } notes.push_back(Note{id, content}); } loadingIndicator.SetInnerHTML("

Notes loaded!

"); RenderNotes(); }); } // Function to delete a note via the backend void DeleteNote(std::uint32_t id) { loadingIndicator.SetInnerHTML("

Deleting note...

"); // Make POST request to delete note Fetch("http://localhost:3000/deleteNote", std::to_string(id), [](std::string content) { FetchNotes(); loadingIndicator.SetInnerHTML("

Note deleted!

"); }); } // Function to render all notes to the DOM void RenderNotes() { if (notes.empty()) { notesContainer.SetInnerHTML("

No notes yet. Add one below!

"); return; } std::string html = "
"; noteButtons.clear(); for (const Note& note : notes) { html += std::format( R"(
{}
)", note.id, note.content, note.id ); } html += "
"; notesContainer.SetInnerHTML(html); 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); }); } } // Main function int main() { FetchNotes(); // Add click listener to add note button addNoteBtn.AddClickListener([&](MouseEvent event) { std::string noteValue = noteInput.GetValue(); std::cout << noteValue << std::endl; if (!noteValue.empty()) { loadingIndicator.SetInnerHTML("

Saving note...

"); Fetch("http://localhost:3000/createNote", noteValue, [noteValue](std::string content) { std::cout << noteValue << std::endl; notes.push_back({static_cast(std::stoi(content)), noteValue}); noteInput.SetValue(""); RenderNotes(); loadingIndicator.SetInnerHTML("

Saved note!

"); }); } }); }