diff options
Diffstat (limited to 'template/script.js')
| -rwxr-xr-x | template/script.js | 78 |
1 files changed, 77 insertions, 1 deletions
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 @@ | |||
| 1 | window.addEventListener('load', () => { | 1 | window.addEventListener('load', async () => { |
| 2 | // dither image on mouse over replace | 2 | // dither image on mouse over replace |
| 3 | // document.querySelectorAll('article img').forEach(img => { | 3 | // document.querySelectorAll('article img').forEach(img => { |
| 4 | // const ditheredImage = img.src; | 4 | // const ditheredImage = img.src; |
| @@ -24,4 +24,80 @@ window.addEventListener('load', () => { | |||
| 24 | evt.target.style.transform = 'scaleX(1)'; | 24 | evt.target.style.transform = 'scaleX(1)'; |
| 25 | }); | 25 | }); |
| 26 | } | 26 | } |
| 27 | |||
| 28 | // comments code | ||
| 29 | const commentsEndpoint = 'https://mitjafelicijan.com/comments-api'; | ||
| 30 | const commentsPlaceholder = document.querySelector('.comments'); | ||
| 31 | |||
| 32 | if (commentsPlaceholder) { | ||
| 33 | const guid = commentsPlaceholder.dataset.guid; | ||
| 34 | const name = commentsPlaceholder.querySelector('input'); | ||
| 35 | const comment = commentsPlaceholder.querySelector('textarea'); | ||
| 36 | const submit = commentsPlaceholder.querySelector('button'); | ||
| 37 | const comments = commentsPlaceholder.querySelector('ul'); | ||
| 38 | |||
| 39 | if (guid) { | ||
| 40 | await readAndRenderComments(guid, comments); | ||
| 41 | |||
| 42 | submit.addEventListener('click', async() => { | ||
| 43 | submit.disabled = true; | ||
| 44 | await writeComments(guid, name.value, comment.value); | ||
| 45 | |||
| 46 | submit.disabled = false; | ||
| 47 | name.value = ''; | ||
| 48 | comment.value = ''; | ||
| 49 | |||
| 50 | await readAndRenderComments(guid, comments); | ||
| 51 | }); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | async function writeComments(guid, name, comment) { | ||
| 56 | const response = await fetch(commentsEndpoint, { | ||
| 57 | method: 'POST', | ||
| 58 | headers: { | ||
| 59 | 'Accept': 'application/json', | ||
| 60 | 'Content-Type': 'application/json' | ||
| 61 | }, | ||
| 62 | body: JSON.stringify({ | ||
| 63 | action: 'write', | ||
| 64 | guid, | ||
| 65 | name, | ||
| 66 | comment, | ||
| 67 | }) | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | |||
| 71 | async function readAndRenderComments(guid, commentsPlaceholder) { | ||
| 72 | const response = await fetch(commentsEndpoint, { | ||
| 73 | method: 'POST', | ||
| 74 | headers: { | ||
| 75 | 'Accept': 'application/json', | ||
| 76 | 'Content-Type': 'application/json' | ||
| 77 | }, | ||
| 78 | body: JSON.stringify({ | ||
| 79 | action: 'read', | ||
| 80 | guid, | ||
| 81 | }) | ||
| 82 | }); | ||
| 83 | |||
| 84 | // remove all existing comments from list | ||
| 85 | commentsPlaceholder.innerHTML = ''; | ||
| 86 | |||
| 87 | const commentList = await response.json(); | ||
| 88 | for (const comment of commentList.reverse()) { | ||
| 89 | const date = new Date(comment.date).toLocaleDateString('en-US', { | ||
| 90 | year: 'numeric', | ||
| 91 | month: 'long', | ||
| 92 | day: 'numeric', | ||
| 93 | hour: 'numeric', | ||
| 94 | minute: 'numeric' | ||
| 95 | }); | ||
| 96 | |||
| 97 | const commentElement = document.createElement('li'); | ||
| 98 | commentElement.innerHTML = `<p><b>${comment.name}</b> - ${date}<p><p>${comment.comment}<p><hr>`; | ||
| 99 | commentsPlaceholder.appendChild(commentElement); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 27 | }); | 103 | }); |
