Skip to content

Commit

Permalink
Merge pull request #66 from langx/xuelink/issue62
Browse files Browse the repository at this point in the history
API Integration for Newsletter Subscription
  • Loading branch information
xuelink authored Apr 27, 2024
2 parents 66127f8 + e835fe8 commit 42932cb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"type": "module",
"dependencies": {
"@fontsource/comfortaa": "^5.0.17"
"@fontsource/comfortaa": "^5.0.17",
"sweetalert2": "^11.10.8"
}
}
42 changes: 28 additions & 14 deletions src/lib/components/molecules/NewsletterForm.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
<script lang="ts">
import Swal from 'sweetalert2';
import { enhance } from '$app/forms';
// /** @type {import('./$types').ActionData} */
// /* svelte-ignore unused-export-let */
// export let form;
export let form: any = {};
export let styles: string = '';
let sending: boolean = false;
let email: string = '';
// when the form is submitted, send a variable from form actions called 'success' and set it to true
// if (form?.success) {
// sending = false;
// }
if (form?.success) {
sending = false;
}
async function handleSubmit(event: any) {
event.preventDefault();
sending = true;
const response = await fetch('https://api.langx.io/api/mail', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email })
});
const data = await response.json();
if (data.status === 'ok') {
email = '';
await Swal.fire('Success!', 'You have been subscribed to our newsletter.', 'success');
} else {
await Swal.fire('Oops!', 'Something went wrong. Please try again.', 'error');
}
sending = false;
}
</script>

<form style={styles} method="POST" action="" use:enhance>
Expand All @@ -21,14 +40,9 @@
autocomplete="email"
required
placeholder="Enter your email"
bind:value={email}
/>
<button
type="submit"
on:click={() => {
sending = true;
}}
disabled={sending}
>
<button type="submit" on:click={handleSubmit} disabled={sending}>
{#if sending}
<span class="sr-only">Loading...</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/organisms/Footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@
</a>
</div>
</div>
<!-- <div class="footer-newsletter">
<div class="footer-newsletter">
<div>
<h3>Subscribe to our newsletter</h3>
<p>We care about your data in our privacy policy.</p>
</div>
<NewsletterForm />
</div> -->
</div>
<div class="socials-wrapper">
<div class="socials-container">
<Socials />
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(waves)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
<RecentPosts {posts} />
{/if}
<BCSponsors />
<!-- <Newsletter /> -->
<Newsletter />
<Faq />
</div>

0 comments on commit 42932cb

Please sign in to comment.