aboutsummaryrefslogtreecommitdiff
path: root/template/script.js
blob: 0e1291c0f2fafd06ba690a8b634119b62cc11851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
window.addEventListener('load', async () => {
  // dither image on mouse over replace
  // document.querySelectorAll('article img').forEach(img => {
  //   const ditheredImage = img.src;
  //   const originalImage = img.src.replace('.dith.gif', '');

  //   img.addEventListener('mouseover', evt => {
  //     evt.target.src = originalImage;
  //   });

  //   img.addEventListener('mouseout', evt => {
  //     evt.target.src = ditheredImage;
  //   });
  // });

  // flip CV image on mouse over
  const cvImage = document.querySelector('.cv-picture img');
  if (cvImage) {
    cvImage.addEventListener('mouseover', evt => {
      evt.target.style.transform = 'scaleX(-1)';
    });

    cvImage.addEventListener('mouseout', evt => {
      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 = `<p><b>${comment.name}</b> - ${date}<p><p>${comment.comment}<p><hr>`;
      commentsPlaceholder.appendChild(commentElement);
    }
  }

});