diff options
Diffstat (limited to 'src/views')
-rw-r--r-- | src/views/index.pug | 49 | ||||
-rw-r--r-- | src/views/sub-search.pug | 36 |
2 files changed, 39 insertions, 46 deletions
diff --git a/src/views/index.pug b/src/views/index.pug index 140cd57..636f9ac 100644 --- a/src/views/index.pug +++ b/src/views/index.pug | |||
@@ -5,50 +5,7 @@ include ../utils | |||
5 | doctype html | 5 | doctype html |
6 | html | 6 | html |
7 | +head("home") | 7 | +head("home") |
8 | script(defer). | 8 | include ../mixins/subUtils |
9 | async function subscribe(sub) { | ||
10 | await doThing(sub, 'subscribe'); | ||
11 | } | ||
12 | |||
13 | async function unsubscribe(sub) { | ||
14 | await doThing(sub, 'unsubscribe'); | ||
15 | } | ||
16 | |||
17 | function getCookie(name) { | ||
18 | const value = `; ${document.cookie}`; | ||
19 | const parts = value.split(`; ${name}=`); | ||
20 | if (parts.length === 2) return parts.pop().split(";").shift(); | ||
21 | } | ||
22 | |||
23 | async function doThing(sub, thing) { | ||
24 | const jwtToken = getCookie("auth_token"); | ||
25 | const response = await fetch(`/${thing}`, { | ||
26 | method: 'POST', | ||
27 | headers: { | ||
28 | 'Authorization': `Bearer ${jwtToken}`, | ||
29 | 'Content-Type': 'application/json', | ||
30 | }, | ||
31 | body: JSON.stringify({ subreddit: sub }), | ||
32 | }); | ||
33 | |||
34 | let thinger = document.getElementById('thinger'); | ||
35 | if (thing == 'subscribe') { | ||
36 | thinger.innerText = 'unsubscribe'; | ||
37 | } else { | ||
38 | thinger.innerText = 'subscribe'; | ||
39 | } | ||
40 | |||
41 | if (!response.ok) { | ||
42 | console.error(`Failed to do ${thing}`); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | function toggleDetails(details_id) { | ||
47 | var detailsElement = document.getElementById(details_id); | ||
48 | if (detailsElement) { | ||
49 | detailsElement.open = !detailsElement.open; | ||
50 | } | ||
51 | } | ||
52 | 9 | ||
53 | body | 10 | body |
54 | main#content | 11 | main#content |
@@ -64,9 +21,9 @@ html | |||
64 | if !isMulti | 21 | if !isMulti |
65 | div#button-container | 22 | div#button-container |
66 | if isSubbed | 23 | if isSubbed |
67 | button(onclick=`unsubscribe('${subreddit}')`)#thinger unsubscribe | 24 | button(onclick=`toggleSub('${subreddit}')` id=`thinger_${subreddit}`) unsubscribe |
68 | else | 25 | else |
69 | button(onclick=`subscribe('${subreddit}')`)#thinger subscribe | 26 | button(onclick=`toggleSub('${subreddit}')` id=`thinger_${subreddit}`) subscribe |
70 | if about && !isMulti | 27 | if about && !isMulti |
71 | p #{about.public_description} | 28 | p #{about.public_description} |
72 | else | 29 | else |
diff --git a/src/views/sub-search.pug b/src/views/sub-search.pug new file mode 100644 index 0000000..4fa707e --- /dev/null +++ b/src/views/sub-search.pug | |||
@@ -0,0 +1,36 @@ | |||
1 | include ../mixins/header | ||
2 | include ../mixins/head | ||
3 | |||
4 | doctype html | ||
5 | html | ||
6 | +head("search subreddits") | ||
7 | include ../mixins/subUtils | ||
8 | body | ||
9 | main#content | ||
10 | +header(user) | ||
11 | div.hero | ||
12 | h1 search subreddits | ||
13 | form(action="/search" method="get").search-bar | ||
14 | input(type="text" name="q" placeholder="search subreddits (add +nsfw to include over-18 results)" required).search-input | ||
15 | button(type="submit").search-button go | ||
16 | if message | ||
17 | div.search-message | ||
18 | | #{message} | ||
19 | if items | ||
20 | div.search-results | ||
21 | each i in items | ||
22 | div.search-result | ||
23 | - var subreddit = i.data.display_name | ||
24 | - var isSubbed = subs.includes(subreddit) | ||
25 | div.sub-title | ||
26 | h3 | ||
27 | a(href=`/r/${subreddit}`) | ||
28 | | r/#{subreddit} | ||
29 | div#button-container | ||
30 | if isSubbed | ||
31 | button(onclick=`toggleSub('${subreddit}')` id=`thinger_${subreddit}`) unsubscribe | ||
32 | else | ||
33 | button(onclick=`toggleSub('${subreddit}')` id=`thinger_${subreddit}`) subscribe | ||
34 | |||
35 | if i.data.public_description | ||
36 | p #{i.data.public_description} | ||