aboutsummaryrefslogtreecommitdiffhomepage
path: root/static/index.js
blob: d546dba72da6eefd48e46b895c3f081f358c4e1e (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
const markAllArticles = (feedId, el, read) => {
  const basePath = window.BASE_PATH;
  const apiUrl = `${basePath}/api/feeds/${feedId}/${read ? 'read' : 'unread'}`;
  fetch(apiUrl, { method: 'PUT' })
    .then(_data => {
      el.parentNode.nextElementSibling.remove();
      el.parentNode.remove();
    })
    .catch(error => {
      alert(error);
    });
};

const markOneArticle = (articleId, el, read) => {
  const basePath = window.BASE_PATH;
  const apiUrl = `${basePath}/api/articles/${articleId}/${read ? 'read' : 'unread'}`;
  fetch(apiUrl, { method: 'PUT' })
    .then(_data => {
      el.parentNode.remove();
    })
    .catch(error => {
      alert(error);
    });
};

document.addEventListener('DOMContentLoaded', () => {
  document.querySelectorAll('.js-unread-feed').forEach(el => {
    el.addEventListener('click', () => {
      markAllArticles(el.dataset.feedId, el, false);
    });
  });
  document.querySelectorAll('.js-unread-article').forEach(el => {
    el.addEventListener('click', () => {
      markOneArticle(el.dataset.articleId, el, false);
    });
  });
  document.querySelectorAll('.js-read-feed').forEach(el => {
    el.addEventListener('click', () => {
      markAllArticles(el.dataset.feedId, el, true);
    });
  });
  document.querySelectorAll('.js-read-article').forEach(el => {
    el.addEventListener('click', () => {
      markOneArticle(el.dataset.articleId, el, true);
    });
  });
});