Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Add Loops.so as fallback for catapult (#55)
Browse files Browse the repository at this point in the history
* feat: loops scafold

* feat: fix loops and add better error checking

---------

Co-authored-by: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com>
  • Loading branch information
jaspermayone and taciturnaxolotl authored Jun 1, 2024
1 parent 9f169c3 commit 33f0f79
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
import Layout from "../layouts/Layout.astro";
let error;
let error: { ok: boolean; message: string } | null = null;
if (Astro.request.method === "POST") {
try {
const data = await Astro.request.formData();
Expand All @@ -19,12 +20,47 @@ if (Astro.request.method === "POST") {
},
);
const loopsFormBody = `name=${encodeURIComponent(
name,
)}&email=${encodeURIComponent(email)}`;
const loops = await fetch(
"https://app.loops.so/api/newsletter-form/" +
import.meta.env.LOOPS_FORM_ID,
{
method: "POST",
body: loopsFormBody,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
},
);
const jsondata = await response.json();
const loopsJson = await loops.json();
console.log(jsondata);
if ((jsondata.ok = true)) {
//do nothing
console.log(loopsJson);
if (jsondata.ok && loopsJson.success) {
error = { ok: true, message: "Success! You're in the loop." };
} else {
error = jsondata.error;
if (
!jsondata.ok &&
jsondata.message === "Email already subscribed" &&
loopsJson.success
) {
error = {
ok: true,
message: "Success! You're in the loop.",
};
} else {
error = {
ok: false,
message:
"Something went wrong. Please try again and if it continues happening please contact us as listed under 'Want to chat? Got questions?'",
};
}
}
} catch (anError) {
if (anError instanceof Error) {
Expand Down Expand Up @@ -56,7 +92,19 @@ if (Astro.request.method === "POST") {
</p>
</section>
<section>
{error && <p class="text-red-500">Error: {error}</p>}
{
error && (
<div>
{
<h3
style={{ color: error.ok ? "green" : "red", textAlign: "center" }}
>
{error.message}
</h3>
}
</div>
)
}
<form method="POST" accept-charset="utf-8">
<label for="name">Name</label><br />
<input
Expand Down

0 comments on commit 33f0f79

Please sign in to comment.