From f10fdc159a502772ec874b33887ce843e7e1e0dd Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Sat, 2 Nov 2024 16:14:53 +0100 Subject: [PATCH] feat(frontend): working newsletter --- .../src/components/header/index.astro | 4 +- apps/frontend/src/directus/index.ts | 1 + .../newsletter/create-newsletter-user.ts | 11 ++++ .../frontend/src/directus/newsletter/index.ts | 1 + apps/frontend/src/directus/sdk-client.ts | 5 ++ .../pages/blog/_parts/newsletter-card.astro | 39 +++++++++++++ apps/frontend/src/pages/blog/index.astro | 58 +++++++++++-------- .../src/pages/home/_parts/jumbotron.astro | 4 +- .../src/pages/newsletter/thank-you.astro | 16 +++++ 9 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 apps/frontend/src/directus/newsletter/create-newsletter-user.ts create mode 100644 apps/frontend/src/directus/newsletter/index.ts create mode 100644 apps/frontend/src/pages/blog/_parts/newsletter-card.astro create mode 100644 apps/frontend/src/pages/newsletter/thank-you.astro diff --git a/apps/frontend/src/components/header/index.astro b/apps/frontend/src/components/header/index.astro index 30581982..0a81f461 100644 --- a/apps/frontend/src/components/header/index.astro +++ b/apps/frontend/src/components/header/index.astro @@ -36,7 +36,7 @@ import HeaderLink from './header-link.astro'; -
+ diff --git a/apps/frontend/src/directus/index.ts b/apps/frontend/src/directus/index.ts index 8ed996d6..c50e2721 100644 --- a/apps/frontend/src/directus/index.ts +++ b/apps/frontend/src/directus/index.ts @@ -1,3 +1,4 @@ export * from './articles'; export * from './get-image-url'; +export * from './newsletter'; export * from './sdk-client'; diff --git a/apps/frontend/src/directus/newsletter/create-newsletter-user.ts b/apps/frontend/src/directus/newsletter/create-newsletter-user.ts new file mode 100644 index 00000000..56af640a --- /dev/null +++ b/apps/frontend/src/directus/newsletter/create-newsletter-user.ts @@ -0,0 +1,11 @@ +import { createItem } from '@directus/sdk'; + +import { directusSdk } from '../sdk-client'; + +export function createNewsletterUser(email: string) { + return directusSdk.request( + createItem('Newsletter_List', { + email, + }), + ); +} diff --git a/apps/frontend/src/directus/newsletter/index.ts b/apps/frontend/src/directus/newsletter/index.ts new file mode 100644 index 00000000..8eb9aa47 --- /dev/null +++ b/apps/frontend/src/directus/newsletter/index.ts @@ -0,0 +1 @@ +export * from './create-newsletter-user'; diff --git a/apps/frontend/src/directus/sdk-client.ts b/apps/frontend/src/directus/sdk-client.ts index c3539bf7..ff738135 100644 --- a/apps/frontend/src/directus/sdk-client.ts +++ b/apps/frontend/src/directus/sdk-client.ts @@ -18,9 +18,14 @@ export type Article = { slug: string; }; +type NewsletterList = { + email: string; +}; + type Schema = { Author: Author[]; Article: Article[]; + Newsletter_List: NewsletterList[]; }; export const directusSdk = createDirectus(import.meta.env.PUBLIC_VITE_DIRECTUS_URL).with(rest()); diff --git a/apps/frontend/src/pages/blog/_parts/newsletter-card.astro b/apps/frontend/src/pages/blog/_parts/newsletter-card.astro new file mode 100644 index 00000000..f02767dc --- /dev/null +++ b/apps/frontend/src/pages/blog/_parts/newsletter-card.astro @@ -0,0 +1,39 @@ +--- +import { Mail } from 'lucide-astro'; + +interface Props { + errors: string[]; +} + +const { errors } = Astro.props; + +--- +
+

Subscribe to our Newsletter

+

+ Stay updated with the latest articles, tips, and exclusive promotions. Subscribe to our newsletter and never miss out on the best content for AI lovers! +

+
+ + + + {errors.length > 0 && ( +
+ {errors.map(error =>

{error}

)} +
+ )} +
+
diff --git a/apps/frontend/src/pages/blog/index.astro b/apps/frontend/src/pages/blog/index.astro index d9a1b648..3a6d9fb7 100644 --- a/apps/frontend/src/pages/blog/index.astro +++ b/apps/frontend/src/pages/blog/index.astro @@ -1,13 +1,42 @@ --- -import { Mail } from 'lucide-astro'; - import ArticleCard from '~/components/blog/article-card.astro'; -import { getArticles } from '~/directus'; +import { createNewsletterUser, getArticles } from '~/directus'; import DefaultLayout from '~/layouts/default-layout.astro'; +import NewsletterCard from './_parts/newsletter-card.astro'; + +const errors = []; +let redirectTo = ''; + +if (Astro.request.method === 'POST') { + try { + const data = await Astro.request.formData(); + const email = data.get('email'); + + if (!email || typeof email !== 'string') { + errors.push('Email is required.'); + } + + if (!errors.length) { + await createNewsletterUser(email!.toString())!; + redirectTo = '/newsletter/thank-you'; + } + } + catch (error) { + console.error(error); + errors.push('Something went wrong. Please try again later.'); + } +} + +if (redirectTo) { + return Astro.redirect(redirectTo); +} + const articles = await getArticles({ limit: 14, }); + + --- -
-

Subscribe to our Newsletter

-

- Stay updated with the latest articles, tips, and exclusive promotions. Subscribe to our newsletter and never miss out on the best content for AI lovers! -

-
- - -
-
+
diff --git a/apps/frontend/src/pages/home/_parts/jumbotron.astro b/apps/frontend/src/pages/home/_parts/jumbotron.astro index fbb19aa6..7bbdf2e6 100644 --- a/apps/frontend/src/pages/home/_parts/jumbotron.astro +++ b/apps/frontend/src/pages/home/_parts/jumbotron.astro @@ -14,8 +14,8 @@ import jumbotronImage from '~/images/jumbotron.jpg'; With a community-driven approach, DashHub continuously evolves to meet the needs of modern enterprises. Join our community today and contribute to the future of enterprise AI.

- - Join Our Community + + Join Our Community (Coming Soon)

diff --git a/apps/frontend/src/pages/newsletter/thank-you.astro b/apps/frontend/src/pages/newsletter/thank-you.astro new file mode 100644 index 00000000..39f73168 --- /dev/null +++ b/apps/frontend/src/pages/newsletter/thank-you.astro @@ -0,0 +1,16 @@ +--- +import DefaultLayout from '~/layouts/default-layout.astro'; +--- + + +
+
+

Thank You for Subscribing!

+

You have successfully subscribed to our newsletter. Stay tuned for the latest updates and exclusive content.

+ Go back to Blog +
+
+