Skip to content

Commit

Permalink
feat: 🎸 dynamically create promo code (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskaransarkaria authored Mar 6, 2024
1 parent cb8ef5f commit 22770b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
export let onSignupFn = (email) => {};
let mcEmailInput = "";
export let mcEmailInput = "";
let mcForm: HTMLFormElement;
</script>

Expand Down
22 changes: 16 additions & 6 deletions src/lib/components/NewsletterSignup/NewsletterSignup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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("@")) {
Expand All @@ -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();
};
Expand All @@ -44,7 +53,7 @@
<h2>Sign up and save 10% off your first online purchase!</h2>
{/if}
{#if $isSignedUp}
{#await getPromoCode()}
{#await getPromoCode(mcEmailInput)}
<h1>...waiting</h1>
{:then promoCode}
<h1 in:fade={{ duration: 500 }}>
Expand All @@ -57,10 +66,10 @@
<h3>code copied to clipboard</h3>
{/if}
{:catch error}
<p style="color: red">{error.message}</p>
<h4>{error.message}</h4>
{/await}
{:else}
<MailChimpSubscribe onSignupFn={signupFn} />
<MailChimpSubscribe onSignupFn={signupFn} bind:mcEmailInput />
{/if}
</div>

Expand All @@ -79,7 +88,8 @@
}
h2,
h3 {
h3,
h4 {
font-family: "Caviar Dreams";
color: black;
}
Expand Down

0 comments on commit 22770b4

Please sign in to comment.