From 64e0862ee37b29709db46986dc82ee6726f5e56f Mon Sep 17 00:00:00 2001 From: oleeh-shd Date: Mon, 14 Aug 2023 14:05:48 +0300 Subject: [PATCH 1/4] create new mutation to save user credentials --- app/home-page.tsx | 65 ++++++++++++++++++++++++---------- components/Home/Modal.tsx | 2 +- components/Home/Newsletter.tsx | 34 +++++++++--------- lib/subscribeUser.ts | 23 ++++++++++++ types.d.ts | 5 +++ 5 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 lib/subscribeUser.ts diff --git a/app/home-page.tsx b/app/home-page.tsx index cabf794..76a2841 100644 --- a/app/home-page.tsx +++ b/app/home-page.tsx @@ -12,6 +12,10 @@ import Newsletter from "../components/Home/Newsletter"; import Chapters from "../components/Home/Chapters"; import NotificationBanner from "../components/Shared/NotificationBanner"; import Image from "next/image"; +import { useTheme } from "next-themes"; +import { subscribeUser } from "../lib/subscribeUser"; + +export type SubscribeMessage = { isSuccess: boolean; message: string }; interface Props extends ChaptersProps { notification: { name: string; message: string; status: string }; @@ -19,6 +23,7 @@ interface Props extends ChaptersProps { function HomePage({ chapters, notification }: Props) { const [modalVisible, setModalVisible] = useState(false); + const { theme } = useTheme(); const pathName = usePathname(); const [cookies, setCookie, removeCookie] = useCookies(["access_token"]); @@ -30,16 +35,32 @@ function HomePage({ chapters, notification }: Props) { setCookie("access_token", access_token[1]); } }, [pathName, setCookie]); - function handleSubscribe( + + async function handleSubscribe( e: FormEvent, - formData: NewsletterFormData - ): boolean { + { name, email }: NewsletterFormData + ): Promise { e.preventDefault(); - if (formData.name && formData.email) { - // todo call newsletter subscribe API - setModalVisible(true); - return true; - } else return false; + if (name && email) { + try { + await subscribeUser(name, email); + + setModalVisible(true); + return { + isSuccess: true, + message: "", + }; + } catch (error) { + return { + isSuccess: false, + message: "ERROR: Email already exists", + }; + } + } else + return { + isSuccess: false, + message: "ERROR: Name or Email Cannot be Empty", + }; } return ( @@ -52,16 +73,24 @@ function HomePage({ chapters, notification }: Props) { />
-
- background image -
- flower + {theme === "light" && ( + <> +
+ background image +
+ flower + + )}
diff --git a/components/Home/Modal.tsx b/components/Home/Modal.tsx index 619e22a..8e32dfb 100644 --- a/components/Home/Modal.tsx +++ b/components/Home/Modal.tsx @@ -7,7 +7,7 @@ interface Props { function Modal({ modalVisible, setModalVisible }: Props) { return ( -
+
, formData: NewsletterFormData - ) => boolean; + ) => Promise; } const Newsletter = ({ handleSubscribe }: Props) => { @@ -14,6 +15,19 @@ const Newsletter = ({ handleSubscribe }: Props) => { email: "", }); const [isValid, setIsValid] = useState(true); + const [errorMessage, setErrorMessage] = useState(""); + + const onSubmit = async (e) => { + const { isSuccess, message } = await handleSubscribe(e, formData); + if (isSuccess) { + setFormData({ name: "", email: "" }); + setIsValid(true); + setErrorMessage(""); + } else { + setIsValid(false); + setErrorMessage(message); + } + }; return (
@@ -32,17 +46,7 @@ const Newsletter = ({ handleSubscribe }: Props) => {

Have the Shloka of the Day delivered to your inbox each morning.

-
{ - if (handleSubscribe(e, formData)) { - setFormData({ name: "", email: "" }); - setIsValid(true); - } else { - setIsValid(false); - } - }} - > + {
{!isValid && ( -
-

- ERROR: Name or Email Cannot be Empty -

+
+

{errorMessage}

)}
diff --git a/lib/subscribeUser.ts b/lib/subscribeUser.ts new file mode 100644 index 0000000..9143480 --- /dev/null +++ b/lib/subscribeUser.ts @@ -0,0 +1,23 @@ +import { gql } from "@apollo/client"; +import apolloClient from "./apolloClient"; + +export const subscribeUser = async ( + name: string, + email: string +): Promise => { + await apolloClient.mutate({ + mutation: gql` + mutation MyMutation { + insert_newsletter_subscriptions_one( + object: { + user_name: "${name}" + user_email: "${email}" + } + ) { + user_name + user_email + } + } + `, + }); +}; diff --git a/types.d.ts b/types.d.ts index e9960cc..364fb1b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -77,3 +77,8 @@ type CurrentVerse = Omit; interface SvgProps { className: string; } + +type SubscriptionCredentials = { + user_name: string; + user_email: string; +}; From 0a755f287bb2059f398071b86ed744beacf8cf39 Mon Sep 17 00:00:00 2001 From: oleeh-shd Date: Mon, 14 Aug 2023 14:08:45 +0300 Subject: [PATCH 2/4] minor fix --- types.d.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/types.d.ts b/types.d.ts index 364fb1b..e9960cc 100644 --- a/types.d.ts +++ b/types.d.ts @@ -77,8 +77,3 @@ type CurrentVerse = Omit; interface SvgProps { className: string; } - -type SubscriptionCredentials = { - user_name: string; - user_email: string; -}; From 24d86f70c7fc1736bc3f1ca9e1f26da4807cca69 Mon Sep 17 00:00:00 2001 From: oleeh-shd Date: Mon, 14 Aug 2023 14:52:13 +0300 Subject: [PATCH 3/4] change text and aligned modal --- app/home-page.tsx | 2 +- components/Home/Modal.tsx | 7 ++++--- layouts/HomeLayout.tsx | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/home-page.tsx b/app/home-page.tsx index 76a2841..7eabcd0 100644 --- a/app/home-page.tsx +++ b/app/home-page.tsx @@ -68,7 +68,7 @@ function HomePage({ chapters, notification }: Props) {
diff --git a/components/Home/Modal.tsx b/components/Home/Modal.tsx index 8e32dfb..3885d35 100644 --- a/components/Home/Modal.tsx +++ b/components/Home/Modal.tsx @@ -14,7 +14,7 @@ function Modal({ modalVisible, setModalVisible }: Props) { x-ref="dialog" aria-modal="true" > -
+
diff --git a/layouts/HomeLayout.tsx b/layouts/HomeLayout.tsx index 0db8e60..b9d8649 100644 --- a/layouts/HomeLayout.tsx +++ b/layouts/HomeLayout.tsx @@ -6,7 +6,7 @@ const HomeLayout = ({ children }: { children: ReactNode }) => { return (
-
{children}
+
{children}
); From ad573b1d1ce20762e597c254c015593c5ef20c57 Mon Sep 17 00:00:00 2001 From: oleeh-shd Date: Mon, 14 Aug 2023 14:54:47 +0300 Subject: [PATCH 4/4] minor fix --- app/home-page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/home-page.tsx b/app/home-page.tsx index 7eabcd0..76a2841 100644 --- a/app/home-page.tsx +++ b/app/home-page.tsx @@ -68,7 +68,7 @@ function HomePage({ chapters, notification }: Props) {