diff options
author | Akshay" (aider) <[email protected]> | 2024-11-13 21:30:42 +0000 |
---|---|---|
committer | Akshay" (aider) <[email protected]> | 2024-11-13 21:30:42 +0000 |
commit | 43f1d4ec54fad383b3b0ef2ee62776bbbebec406 (patch) | |
tree | a1e7538a1ff61b298e7b5f818de3458a257548d6 /src | |
parent | c54fefd5227d76898c58bcf11d9ff67f6856d1b9 (diff) |
feat: Update subscription button to use POST request to /subscribe endpoint
Diffstat (limited to 'src')
-rw-r--r-- | src/views/index.pug | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/views/index.pug b/src/views/index.pug index 0e35a9d..dbd246f 100644 --- a/src/views/index.pug +++ b/src/views/index.pug | |||
@@ -9,7 +9,7 @@ html | |||
9 | +head("home") | 9 | +head("home") |
10 | +subMgmt() | 10 | +subMgmt() |
11 | script(defer). | 11 | script(defer). |
12 | function updateButton(sub) { | 12 | async function updateButton(sub) { |
13 | var b = document.getElementById("button-container"); | 13 | var b = document.getElementById("button-container"); |
14 | b.innerHTML = ''; | 14 | b.innerHTML = ''; |
15 | 15 | ||
@@ -17,14 +17,38 @@ html | |||
17 | 17 | ||
18 | if (issub(sub)) { | 18 | if (issub(sub)) { |
19 | button.innerText = "unsubscribe"; | 19 | button.innerText = "unsubscribe"; |
20 | button.onclick = ()=>unsubscribe(sub); | 20 | button.onclick = async () => await unsubscribe(sub); |
21 | } else { | 21 | } else { |
22 | button.innerText = "subscribe"; | 22 | button.innerText = "subscribe"; |
23 | button.onclick = ()=>subscribe(sub); | 23 | button.onclick = async () => await subscribe(sub); |
24 | } | 24 | } |
25 | b.appendChild(button); | 25 | b.appendChild(button); |
26 | } | 26 | } |
27 | 27 | ||
28 | async function subscribe(sub) { | ||
29 | await postSubscription(sub, true); | ||
30 | updateButton(sub); | ||
31 | } | ||
32 | |||
33 | async function unsubscribe(sub) { | ||
34 | await postSubscription(sub, false); | ||
35 | updateButton(sub); | ||
36 | } | ||
37 | |||
38 | async function postSubscription(sub, subscribe) { | ||
39 | const response = await fetch('/subscribe', { | ||
40 | method: 'POST', | ||
41 | headers: { | ||
42 | 'Content-Type': 'application/json', | ||
43 | }, | ||
44 | body: JSON.stringify({ subreddit: sub, subscribe: subscribe }), | ||
45 | }); | ||
46 | |||
47 | if (!response.ok) { | ||
48 | console.error('Failed to update subscription'); | ||
49 | } | ||
50 | } | ||
51 | |||
28 | function toggleDetails(details_id) { | 52 | function toggleDetails(details_id) { |
29 | var detailsElement = document.getElementById(details_id); | 53 | var detailsElement = document.getElementById(details_id); |
30 | if (detailsElement) { | 54 | if (detailsElement) { |
@@ -32,7 +56,7 @@ html | |||
32 | } | 56 | } |
33 | } | 57 | } |
34 | 58 | ||
35 | document.addEventListener('DOMContentLoaded', ()=>updateButton("#{subreddit}")); | 59 | document.addEventListener('DOMContentLoaded', () => updateButton("#{subreddit}")); |
36 | body | 60 | body |
37 | main#content | 61 | main#content |
38 | +header(user) | 62 | +header(user) |