selfhostable, read-only reddit client
16
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: Update subscription button to use POST request to /subscribe endpoint

+28 -4
+28 -4
src/views/index.pug
··· 9 9 +head("home") 10 10 +subMgmt() 11 11 script(defer). 12 - function updateButton(sub) { 12 + async function updateButton(sub) { 13 13 var b = document.getElementById("button-container"); 14 14 b.innerHTML = ''; 15 15 ··· 17 17 18 18 if (issub(sub)) { 19 19 button.innerText = "unsubscribe"; 20 - button.onclick = ()=>unsubscribe(sub); 20 + button.onclick = async () => await unsubscribe(sub); 21 21 } else { 22 22 button.innerText = "subscribe"; 23 - button.onclick = ()=>subscribe(sub); 23 + button.onclick = async () => await subscribe(sub); 24 24 } 25 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 52 function toggleDetails(details_id) { 29 53 var detailsElement = document.getElementById(details_id); 30 54 if (detailsElement) { ··· 32 56 } 33 57 } 34 58 35 - document.addEventListener('DOMContentLoaded', ()=>updateButton("#{subreddit}")); 59 + document.addEventListener('DOMContentLoaded', () => updateButton("#{subreddit}")); 36 60 body 37 61 main#content 38 62 +header(user)