From a306363f9d102a162d582f0f19920f6b6578a0f6 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sun, 30 Jan 2022 21:39:50 +0100 Subject: Added comments made with Nginx NJS --- template/_includes.html | 2 +- template/_meta.html | 2 +- template/_navigation.html | 2 -- template/post.html | 13 ++++++-- template/script.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++- template/style.css | 30 ++++++++++++++++++ 6 files changed, 120 insertions(+), 7 deletions(-) diff --git a/template/_includes.html b/template/_includes.html index 3c73ccf..d16fa3d 100755 --- a/template/_includes.html +++ b/template/_includes.html @@ -1,3 +1,3 @@ - + diff --git a/template/_meta.html b/template/_meta.html index 93f168a..e29a9a8 100755 --- a/template/_meta.html +++ b/template/_meta.html @@ -5,7 +5,7 @@ - + diff --git a/template/_navigation.html b/template/_navigation.html index 8a6152c..f6567c9 100755 --- a/template/_navigation.html +++ b/template/_navigation.html @@ -11,10 +11,8 @@ - CV - diff --git a/template/post.html b/template/post.html index 0404820..31a77b6 100755 --- a/template/post.html +++ b/template/post.html @@ -28,8 +28,17 @@ {{if .Listing}} -
- ➝ Get in contact with me via email. + +
+ +
+

Comments

+
+

+

+

+
+

    diff --git a/template/script.js b/template/script.js index 4c508ed..0e1291c 100755 --- a/template/script.js +++ b/template/script.js @@ -1,4 +1,4 @@ -window.addEventListener('load', () => { +window.addEventListener('load', async () => { // dither image on mouse over replace // document.querySelectorAll('article img').forEach(img => { // const ditheredImage = img.src; @@ -24,4 +24,80 @@ window.addEventListener('load', () => { evt.target.style.transform = 'scaleX(1)'; }); } + + // comments code + const commentsEndpoint = 'https://mitjafelicijan.com/comments-api'; + const commentsPlaceholder = document.querySelector('.comments'); + + if (commentsPlaceholder) { + const guid = commentsPlaceholder.dataset.guid; + const name = commentsPlaceholder.querySelector('input'); + const comment = commentsPlaceholder.querySelector('textarea'); + const submit = commentsPlaceholder.querySelector('button'); + const comments = commentsPlaceholder.querySelector('ul'); + + if (guid) { + await readAndRenderComments(guid, comments); + + submit.addEventListener('click', async() => { + submit.disabled = true; + await writeComments(guid, name.value, comment.value); + + submit.disabled = false; + name.value = ''; + comment.value = ''; + + await readAndRenderComments(guid, comments); + }); + } + } + + async function writeComments(guid, name, comment) { + const response = await fetch(commentsEndpoint, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + action: 'write', + guid, + name, + comment, + }) + }); + } + + async function readAndRenderComments(guid, commentsPlaceholder) { + const response = await fetch(commentsEndpoint, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + action: 'read', + guid, + }) + }); + + // remove all existing comments from list + commentsPlaceholder.innerHTML = ''; + + const commentList = await response.json(); + for (const comment of commentList.reverse()) { + const date = new Date(comment.date).toLocaleDateString('en-US', { + year: 'numeric', + month: 'long', + day: 'numeric', + hour: 'numeric', + minute: 'numeric' + }); + + const commentElement = document.createElement('li'); + commentElement.innerHTML = `

    ${comment.name} - ${date}

    ${comment.comment}


    `; + commentsPlaceholder.appendChild(commentElement); + } + } + }); diff --git a/template/style.css b/template/style.css index aa0be85..cce9621 100755 --- a/template/style.css +++ b/template/style.css @@ -14,6 +14,8 @@ --badge-label-color: #111111; --code-font-size: 13px; + + --comment-form-font: 16px 'Times New Roman', Times, serif; } * { @@ -337,6 +339,34 @@ audio { color: #666; } +/* comments */ + +.comments input{ + width: 100%; + font: var(--comment-form-font); + border: 1px solid #bbb; + padding: 5px; +} + +.comments textarea{ + width: 100%; + height: 100px; + resize: vertical; + font: var(--comment-form-font); + border: 1px solid #bbb; + padding: 5px; +} + +.comments ul { + list-style-type: none; + padding: 0; + margin-top: 30px; +} + +.comments hr { + border: initial !important; + border-top: initial !important; +} /* responsive */ -- cgit v1.2.3