aboutsummaryrefslogtreecommitdiff
path: root/assets_src
diff options
context:
space:
mode:
Diffstat (limited to 'assets_src')
-rw-r--r--assets_src/js/like.js75
1 files changed, 3 insertions, 72 deletions
diff --git a/assets_src/js/like.js b/assets_src/js/like.js
index a438182..03b15a1 100644
--- a/assets_src/js/like.js
+++ b/assets_src/js/like.js
@@ -11,81 +11,12 @@ async function updateLikeCount(el) {
11 11
12 const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {}); 12 const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {});
13 13
14 dom.firstChild(el, e => e.classList.contains('like-count')).textContent = count; 14 el.textContent = count;
15}
16
17async function isLikedByCurrentUser(el) {
18 const type = parseInt(el.getAttribute('content-type'), 10);
19 const contentId = parseInt(el.getAttribute('content-id'), 10);
20 const userId = parseInt(el.getAttribute('update-with'), 10);
21
22 if (isNaN(type) || isNaN(contentId) || isNaN(userId)) {
23 return;
24 }
25
26 const b = await ajax.get(`/api/likes/liked_by?id=${contentId}&type=${type}&user=${userId}`, {});
27
28 console.log(b);
29 return b === 'true';
30}
31
32async function updateLikeByCurrentUser(el, to) {
33 const type = parseInt(el.getAttribute('content-type'), 10);
34 const contentId = parseInt(el.getAttribute('content-id'), 10);
35 const userId = parseInt(el.getAttribute('update-with'), 10);
36
37 if (isNaN(type) || isNaN(contentId) || isNaN(userId)) {
38 return;
39 }
40
41 updateLikeClass(el, to);
42
43 if (to) {
44 await ajax.put('/api/likes', `id=${contentId}&type=${type}&user=${userId}`, {
45 headers: {
46 'Content-Type': 'application/x-www-form-urlencoded',
47 }
48 });
49 } else {
50 await ajax.del('/api/likes', `id=${contentId}&type=${type}&user=${userId}`, {
51 headers: {
52 'Content-Type': 'application/x-www-form-urlencoded',
53 }
54 });
55 }
56 const cont = dom.firstChild(el, e => e.classList.contains('like-count'));
57
58 cont.textContent = parseInt(cont.textContent, 10) + (to ? 1 : -1);
59}
60
61function updateLikeClass(el, to) {
62 el.classList.toggle('liked', to);
63} 15}
64 16
65dom.ready(() => { 17dom.ready(() => {
66 dom.withClass('like-div') 18 dom.withClass('like-count')
67 .forEach(async el => { 19 .forEach(el => {
68 updateLikeCount(el); 20 updateLikeCount(el);
69
70 const userId = parseInt(el.getAttribute('update-with'), 10);
71
72 let liked = await isLikedByCurrentUser(el);
73 updateLikeClass(el, liked);
74 if (!isNaN(userId)) {
75 el.addEventListener('click', async () => {
76 liked = !liked;
77 await updateLikeByCurrentUser(el, liked);
78 });
79 const cap = dom.firstChild(el, e => e.classList.contains('like-caption'));
80 if (cap) {
81 el.addEventListener('mouseover', () => {
82 cap.textContent = liked ? 'nicht mehr approven' : 'approven!';
83 });
84 el.addEventListener('mouseout', () => cap.textContent = 'approven');
85 }
86 } else {
87 el.classList.add('disabled');
88 }
89
90 }); 21 });
91}); 22});