From 33f0f794f7119f7c1a20e168e84ba61536097864 Mon Sep 17 00:00:00 2001
From: Jasper Mayone
Date: Sat, 1 Jun 2024 19:01:34 -0400
Subject: [PATCH] Add Loops.so as fallback for catapult (#55)
* feat: loops scafold
* feat: fix loops and add better error checking
---------
Co-authored-by: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com>
---
src/pages/index.astro | 58 +++++++++++++++++++++++++++++++++++++++----
1 file changed, 53 insertions(+), 5 deletions(-)
diff --git a/src/pages/index.astro b/src/pages/index.astro
index b944f57..165a88c 100644
--- a/src/pages/index.astro
+++ b/src/pages/index.astro
@@ -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();
@@ -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) {
@@ -56,7 +92,19 @@ if (Astro.request.method === "POST") {
- {error && Error: {error}
}
+ {
+ error && (
+
+ {
+
+ {error.message}
+
+ }
+
+ )
+ }