From f05222daf7e4411d5250ffd1f743e891834dece7 Mon Sep 17 00:00:00 2001 From: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com> Date: Fri, 31 May 2024 22:25:04 -0400 Subject: [PATCH 1/2] feat: loops scafold --- src/pages/index.astro | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/pages/index.astro b/src/pages/index.astro index b944f57..9d40bf0 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -19,6 +19,16 @@ if (Astro.request.method === "POST") { }, ); + const loops = await fetch("https://app.loops.so/api/v1/contacts/create", + { + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": "Bearer " + import.meta.env.LOOPS_KEY, + }, + body: JSON.stringify({ name, email }), + ) + const jsondata = await response.json(); console.log(jsondata); if ((jsondata.ok = true)) { From 1fa4d2f32b23a9a88d9e15acbd91fe40d7af7f80 Mon Sep 17 00:00:00 2001 From: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com> Date: Sat, 1 Jun 2024 12:39:31 -0400 Subject: [PATCH 2/2] feat: fix loops and add better error checking --- src/pages/index.astro | 58 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/pages/index.astro b/src/pages/index.astro index 9d40bf0..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,22 +20,47 @@ if (Astro.request.method === "POST") { }, ); - const loops = await fetch("https://app.loops.so/api/v1/contacts/create", + 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/json", - "Authorization": "Bearer " + import.meta.env.LOOPS_KEY, + "Content-Type": "application/x-www-form-urlencoded", }, - body: JSON.stringify({ name, email }), - ) + }, + ); 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) { @@ -66,7 +92,19 @@ if (Astro.request.method === "POST") {

- {error &&

Error: {error}

} + { + error && ( +
+ { +

+ {error.message} +

+ } +
+ ) + }