From 583e0e1959611416a22a0ecb6209b72e82817f7a Mon Sep 17 00:00:00 2001 From: Jaskaran Sarkaria Date: Wed, 6 Mar 2024 21:26:46 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20dynamically=20create=20p?= =?UTF-8?q?romo=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MailChimpSubscribe.svelte | 2 +- .../NewsletterSignup/NewsletterSignup.svelte | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/lib/components/MailChimpSubscribe/MailChimpSubscribe.svelte b/src/lib/components/MailChimpSubscribe/MailChimpSubscribe.svelte index c8aeb9a..70bae2c 100644 --- a/src/lib/components/MailChimpSubscribe/MailChimpSubscribe.svelte +++ b/src/lib/components/MailChimpSubscribe/MailChimpSubscribe.svelte @@ -1,7 +1,7 @@ diff --git a/src/lib/components/NewsletterSignup/NewsletterSignup.svelte b/src/lib/components/NewsletterSignup/NewsletterSignup.svelte index 9125e1f..7ce56a5 100644 --- a/src/lib/components/NewsletterSignup/NewsletterSignup.svelte +++ b/src/lib/components/NewsletterSignup/NewsletterSignup.svelte @@ -6,8 +6,10 @@ import MailChimpSubscribe from "../MailChimpSubscribe/MailChimpSubscribe.svelte"; import { isSignedUp } from "$lib/stores/newsletterModal"; import { PUBLIC_SERVER_URL } from "$env/static/public"; + import { stringify } from "fp-ts/lib/Json"; let promoCopied = false; + let mcEmailInput = ""; const signupFn = (input: string) => { if (input.includes("@")) { @@ -24,8 +26,15 @@ } }; - const getPromoCode = async () => { - const res = await fetch(`${PUBLIC_SERVER_URL}/get-promo-code`); + const getPromoCode = async (email) => { + const res = await fetch( + `${PUBLIC_SERVER_URL}/get-promo-code?email=${encodeURIComponent(email)}` + ); + + if (res.status === 403) { + throw new Error(email + " has already redeemed a newsletter coupon"); + } + return res.text(); }; @@ -44,7 +53,7 @@

Sign up and save 10% off your first online purchase!

{/if} {#if $isSignedUp} - {#await getPromoCode()} + {#await getPromoCode(mcEmailInput)}

...waiting

{:then promoCode}

@@ -57,10 +66,10 @@

code copied to clipboard

{/if} {:catch error} -

{error.message}

+

{error.message}

{/await} {:else} - + {/if} @@ -79,7 +88,8 @@ } h2, - h3 { + h3, + h4 { font-family: "Caviar Dreams"; color: black; }