From 08e35c4cf6db623417fcee442a1cc11edaa99153 Mon Sep 17 00:00:00 2001 From: yequari Date: Thu, 26 Jun 2025 20:22:41 -0700 Subject: [PATCH] guestbook test --- content/guestbook.html | 28 +++++++++++ static/guestbook.js | 106 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 content/guestbook.html create mode 100644 static/guestbook.js diff --git a/content/guestbook.html b/content/guestbook.html new file mode 100644 index 0000000..a9d1315 --- /dev/null +++ b/content/guestbook.html @@ -0,0 +1,28 @@ +--- +title: "Guestbook" +date: 2025-06-26T20:17:07-07:00 +--- + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ diff --git a/static/guestbook.js b/static/guestbook.js new file mode 100644 index 0000000..dada823 --- /dev/null +++ b/static/guestbook.js @@ -0,0 +1,106 @@ + +class CommentList extends HTMLElement { + static get observedAttributes() { + return ['src']; + } + + constructor() { + super(); + this.attachShadow({ mode: 'open' }); + this.comments = []; + this.loading = false; + this.error = null; + } + + connectedCallback() { + if (this.hasAttribute('src')) { + this.fetchComments(); + } + } + + attributeChangedCallback(name, oldValue, newValue) { + if (name === 'src' && oldValue !== newValue) { + this.fetchComments(); + } + } + + async fetchComments() { + const src = this.getAttribute('src'); + if (!src) return; + this.loading = true; + this.error = null; + this.render(); + + try { + const response = await fetch(src); + if (!response.ok) throw new Error(`HTTP error: ${response.status}`); + const data = await response.json(); + this.comments = Array.isArray(data) ? data : []; + this.loading = false; + this.render(); + } catch (err) { + this.error = err.message; + this.loading = false; + this.render(); + } + } + + formatDate(isoString) { + if (!isoString) return ''; + const date = new Date(isoString); + return date.toLocaleString(); + } + + render() { + this.shadowRoot.innerHTML = ` + +
+ ${this.loading + ? `
Loading comments...
` + : this.error + ? `
Error: ${this.error}
` + : this.comments.length === 0 + ? `
No comments found.
` + : this.comments.map(comment => ` +
+ ${comment.AuthorName || 'Unknown Author'} + ${this.formatDate(comment.Created)} +
${comment.CommentText || ''}
+
+ `).join('') + } +
+ `; + } +} + +customElements.define('comment-list', CommentList);