diff --git a/.gitignore b/.gitignore index fd3dbb5..b9f8d23 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +.env \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f4b86b2..a9b163e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,14 +11,17 @@ "@hookform/resolvers": "^3.6.0", "@react-pdf/renderer": "^3.4.4", "@vercel/analytics": "^1.3.1", + "axios": "^1.7.2", "clsx": "^2.1.1", "jotai": "^2.8.3", + "mailgun.js": "^10.2.3", "next": "14.2.4", "react": "^18", "react-daisyui": "^5.0.0", "react-dom": "^18", "react-hook-form": "^7.52.0", "react-icons": "^5.2.1", + "react-toastify": "^10.0.5", "tailwind-merge": "^2.3.0", "yup": "^1.4.0" }, @@ -1081,6 +1084,11 @@ "integrity": "sha512-OH/2E5Fg20h2aPrbe+QL8JZQFko0YZaF+j4mnQ7BGhfavO7OpSLa8a0y9sBwomHdSbkhTS8TQNayBfnW5DwbvQ==", "dev": true }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/available-typed-arrays": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", @@ -1105,6 +1113,16 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz", + "integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/axobject-query": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz", @@ -1119,6 +1137,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/base-64": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/base-64/-/base-64-1.0.0.tgz", + "integrity": "sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg==" + }, "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -1356,6 +1379,17 @@ "simple-swizzle": "^0.2.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", @@ -1562,6 +1596,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/dequal": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", @@ -2338,6 +2380,25 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/fontkit": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fontkit/-/fontkit-2.0.2.tgz", @@ -2387,6 +2448,19 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3391,6 +3465,19 @@ "node": "14 || >=16.14" } }, + "node_modules/mailgun.js": { + "version": "10.2.3", + "resolved": "https://registry.npmjs.org/mailgun.js/-/mailgun.js-10.2.3.tgz", + "integrity": "sha512-7Mcw5IFtzN21i+qFQoWI+aQFDpLYSMUIWvDUXKLlpGFVVGfYVL8GIiveS+LIXpEJTQcF1hoNhOhDwenFqNSKmw==", + "dependencies": { + "axios": "^1.7.2", + "base-64": "^1.0.0", + "url-join": "^4.0.1" + }, + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/media-engine": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/media-engine/-/media-engine-1.0.3.tgz", @@ -3416,6 +3503,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -4066,6 +4172,11 @@ "resolved": "https://registry.npmjs.org/property-expr/-/property-expr-2.0.6.tgz", "integrity": "sha512-SVtmxhRE/CGkn3eZY1T6pC8Nln6Fr/lu1mKSgRud0eC73whjGfoAogbn78LkD8aFL0zz3bAFerKSnOl7NlErBA==" }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", @@ -4164,6 +4275,18 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, + "node_modules/react-toastify": { + "version": "10.0.5", + "resolved": "https://registry.npmjs.org/react-toastify/-/react-toastify-10.0.5.tgz", + "integrity": "sha512-mNKt2jBXJg4O7pSdbNUfDdTsK9FIdikfsIE/yUCxbAEXl4HMyJaivrVFcn3Elvt5xvCQYhUZm+hqTIu1UXM3Pw==", + "dependencies": { + "clsx": "^2.1.0" + }, + "peerDependencies": { + "react": ">=18", + "react-dom": ">=18" + } + }, "node_modules/read-cache": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", @@ -5102,6 +5225,11 @@ "punycode": "^2.1.0" } }, + "node_modules/url-join": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz", + "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==" + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index 8d3bf6a..3a36a82 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,17 @@ "@hookform/resolvers": "^3.6.0", "@react-pdf/renderer": "^3.4.4", "@vercel/analytics": "^1.3.1", + "axios": "^1.7.2", "clsx": "^2.1.1", "jotai": "^2.8.3", + "mailgun.js": "^10.2.3", "next": "14.2.4", "react": "^18", "react-daisyui": "^5.0.0", "react-dom": "^18", "react-hook-form": "^7.52.0", "react-icons": "^5.2.1", + "react-toastify": "^10.0.5", "tailwind-merge": "^2.3.0", "yup": "^1.4.0" }, diff --git a/src/components/LandingPage/EmailSignup.tsx b/src/components/LandingPage/EmailSignup.tsx index b4b5fc8..3de9573 100644 --- a/src/components/LandingPage/EmailSignup.tsx +++ b/src/components/LandingPage/EmailSignup.tsx @@ -1,6 +1,22 @@ import { Button, Input } from 'react-daisyui'; +import { SubmitHandler, useForm } from 'react-hook-form'; +import { yupResolver } from '@hookform/resolvers/yup'; + +import { MailListInputs, mailListSchema } from '@/schemas/mailList'; + +import useAddMailList from '@/hooks/useAddMailList'; const EmailSignup = () => { + const { addEmailToList } = useAddMailList(); + const { register, handleSubmit, reset } = useForm({ + resolver: yupResolver(mailListSchema), + }); + + const onFormSubmit: SubmitHandler = ({ email }) => { + addEmailToList(email); + reset(); + }; + return (
@@ -13,14 +29,19 @@ const EmailSignup = () => { Join our community for news, insights, and special promotions.

-
+
- -
+ +
diff --git a/src/hooks/useAddMailList.ts b/src/hooks/useAddMailList.ts new file mode 100644 index 0000000..7e003fe --- /dev/null +++ b/src/hooks/useAddMailList.ts @@ -0,0 +1,24 @@ +import { postRequest } from '@/utils/axios'; +import { toast } from 'react-toastify'; + +const useAddMailList = () => { + const addEmailToList = async (email: string) => { + try { + await postRequest('/joinMailList', { + email, + }); + + toast.success('Successfully Subscribed!', { + position: 'top-right', + }); + } catch (error) { + toast.error('Something went Wrong!', { + position: 'top-right', + }); + } + }; + + return { addEmailToList }; +}; + +export default useAddMailList; diff --git a/src/libs/mailgun.ts b/src/libs/mailgun.ts new file mode 100644 index 0000000..43e03f8 --- /dev/null +++ b/src/libs/mailgun.ts @@ -0,0 +1,16 @@ +import formData from 'form-data'; +import Mailgun from 'mailgun.js'; + +const mailgun = new Mailgun(formData); +const mailListAddress = process.env.MAILGUN_MAILLIST || 'dummy'; + +const mg = mailgun.client({ + username: 'API', + key: process.env.MAILGUN_API_KEY || 'dummy', +}); + +export const addToMailList = async (email: string) => { + await mg.lists.members.createMember(mailListAddress, { + address: email, + }); +}; diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 36bf6f3..7f63752 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -2,8 +2,10 @@ import { ReactElement, ReactNode } from 'react'; import { NextPage } from 'next'; import Head from 'next/head'; import { Theme } from 'react-daisyui'; +import { ToastContainer } from 'react-toastify'; import '@/styles/globals.css'; +import 'react-toastify/dist/ReactToastify.css'; import type { AppProps } from 'next/app'; import { Analytics } from '@vercel/analytics/react'; @@ -86,6 +88,7 @@ Menu design software" {getLayout()} + diff --git a/src/pages/api/joinMailList.ts b/src/pages/api/joinMailList.ts new file mode 100644 index 0000000..9fccc58 --- /dev/null +++ b/src/pages/api/joinMailList.ts @@ -0,0 +1,26 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +import { addToMailList } from '@/libs/mailgun'; + +type ResponseData = { + message: string; +}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse, +) { + try { + const { email } = req.body; + + if (!email) { + throw new Error('Missing Email'); + } + + await addToMailList(email); + res.status(200).json({ message: 'Thanks for Joining my Mail List!' }); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Something went wrong!' }); + } +} diff --git a/src/schemas/mailList.ts b/src/schemas/mailList.ts new file mode 100644 index 0000000..0e6cf89 --- /dev/null +++ b/src/schemas/mailList.ts @@ -0,0 +1,7 @@ +import * as yup from 'yup'; + +export const mailListSchema = yup.object({ + email: yup.string().trim().email().required(), +}); + +export type MailListInputs = yup.InferType; diff --git a/src/utils/axios.ts b/src/utils/axios.ts new file mode 100644 index 0000000..039d596 --- /dev/null +++ b/src/utils/axios.ts @@ -0,0 +1,56 @@ +import axios, { AxiosRequestConfig } from 'axios'; + +const axiosClient = axios.create({ + baseURL: process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:3000/api', + headers: { + 'Content-Type': 'application/json', + }, +}); + +axiosClient.interceptors.response.use( + async ({ data }) => { + return data; + }, + async (err) => { + if (axios.isAxiosError(err)) { + throw new Error(`Axios error: ${err.message}`, { + cause: err.response?.data, + }); + } + + throw new Error(`Unexpected error: ${err}`); + }, +); + +// ***** ***** ***** Axios Request Abstraction ***** ***** ***** // +export const getRequest = (url: string, config?: AxiosRequestConfig) => { + return axiosClient.get(url, config); +}; + +export const postRequest = ( + url: string, + payload?: any, + config?: AxiosRequestConfig, +) => { + return axiosClient.post(url, payload, config); +}; + +export const putRequest = ( + url: string, + payload?: any, + config?: AxiosRequestConfig, +) => { + return axiosClient.put(url, payload, config); +}; + +export const patchRequest = ( + url: string, + payload?: any, + config?: AxiosRequestConfig, +) => { + return axiosClient.patch(url, payload, config); +}; + +export const deleteRequest = (url: string, config?: AxiosRequestConfig) => { + return axiosClient.delete(url, config); +};