diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2021-08-30 10:58:12 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2021-08-30 10:58:12 -0600 |
commit | 3e9a17abb9a9d63af3c260fba9dc404dd9c59ade (patch) | |
tree | 48065b5b454176634b1e03beaa580d8c06e68320 | |
parent | 15ae483fadbd136acefcd602b2f2ac5a83165c73 (diff) |
fix finalize and unsubscribe pages by making them use new api module
-rw-r--r-- | static/src/mailinglist/finalize.md | 42 | ||||
-rw-r--r-- | static/src/mailinglist/unsubscribe.md | 41 |
2 files changed, 33 insertions, 50 deletions
diff --git a/static/src/mailinglist/finalize.md b/static/src/mailinglist/finalize.md index fe8f741..56878b9 100644 --- a/static/src/mailinglist/finalize.md +++ b/static/src/mailinglist/finalize.md @@ -4,6 +4,8 @@ title: "" nofollow: true --- +<script async type="module" src="/assets/api.js"></script> + <style> #result.success { color: green; } #result.fail { color: red; } @@ -14,39 +16,29 @@ nofollow: true <script> (async () => { + const resultSpan = document.getElementById("result"); - - function setErr(errStr) { - resultSpan.className = "fail"; - resultSpan.innerHTML = errStr; - } - const urlParams = new URLSearchParams(window.location.search); - const subToken = urlParams.get('subToken'); + try { - if (!subToken) { - setErr("No subscription token provided"); - return; - } + const urlParams = new URLSearchParams(window.location.search); + const subToken = urlParams.get('subToken'); - const finalizeForm = new FormData(); - finalizeForm.append('subToken', subToken); + if (!subToken) throw "No subscription token provided"; - const finalizeReq = new Request('/api/mailinglist/finalize', { - method: 'POST', - body: finalizeForm, - }); + const api = await import("/assets/api.js"); - const res = await fetch(finalizeReq) - .then(response => response.json()); + await api.call('POST', '/api/mailinglist/finalize', { + body: { subToken }, + }); - if (res.error) { - setErr(res.error); - return; - } + resultSpan.className = "success"; + resultSpan.innerHTML = "Your email subscription has been finalized! Please go on about your day."; - resultSpan.className = "success"; - resultSpan.innerHTML = "Your email subscription has been finalized! Please go on about your day."; + } catch (e) { + resultSpan.className = "fail"; + resultSpan.innerHTML = e; + } })(); diff --git a/static/src/mailinglist/unsubscribe.md b/static/src/mailinglist/unsubscribe.md index 6a118a0..cacd212 100644 --- a/static/src/mailinglist/unsubscribe.md +++ b/static/src/mailinglist/unsubscribe.md @@ -4,6 +4,8 @@ title: "" nofollow: true --- +<script async type="module" src="/assets/api.js"></script> + <style> #result.success { color: green; } #result.fail { color: red; } @@ -14,40 +16,29 @@ nofollow: true <script> (async () => { + const resultSpan = document.getElementById("result"); - function setErr(errStr) { - resultSpan.className = "fail"; - resultSpan.innerHTML = errStr; - } + try { + const urlParams = new URLSearchParams(window.location.search); + const unsubToken = urlParams.get('unsubToken'); - const urlParams = new URLSearchParams(window.location.search); - const unsubToken = urlParams.get('unsubToken'); + if (!unsubToken) throw "No unsubscribe token provided"; - if (!unsubToken) { - setErr("No unsubscribe token provided"); - return; - } - - const unsubscribeForm = new FormData(); - unsubscribeForm.append('unsubToken', unsubToken); + const api = await import("/assets/api.js"); - const unsubscribeReq = new Request('/api/mailinglist/unsubscribe', { - method: 'POST', - body: unsubscribeForm, - }); + await api.call('POST', '/api/mailinglist/unsubscribe', { + body: { unsubToken }, + }); - const res = await fetch(unsubscribeReq) - .then(response => response.json()); + resultSpan.className = "success"; + resultSpan.innerHTML = "You have been unsubscribed! Please go on about your day."; - if (res.error) { - setErr(res.error); - return; + } catch (e) { + resultSpan.className = "fail"; + resultSpan.innerHTML = e; } - resultSpan.className = "success"; - resultSpan.innerHTML = "You have been unsubscribed! Please go on about your day."; - })(); </script> |