From 344c937311919ec0ee6837e2d8ef8be778ab5b3a Mon Sep 17 00:00:00 2001 From: okorie2 Date: Thu, 16 Nov 2023 17:42:36 +0100 Subject: [PATCH] feat: make contact us form function --- .../home-module/sub-modules/contact/index.tsx | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/src/app/modules/home-module/sub-modules/contact/index.tsx b/src/app/modules/home-module/sub-modules/contact/index.tsx index 3c48dd87f..a63d90a66 100644 --- a/src/app/modules/home-module/sub-modules/contact/index.tsx +++ b/src/app/modules/home-module/sub-modules/contact/index.tsx @@ -1,10 +1,11 @@ import React from "react"; import EmpowerBlock from "../partners/components/empowerBlock"; -import { Box, Container, TextField } from "@material-ui/core"; +import { Box, Container, Snackbar, TextField } from "@material-ui/core"; import HomeFooter from "../../components/Footer"; import { ReactComponent as FullEllipse } from "app/modules/home-module/assets/contact-lg-ellispe.svg"; import NewsletterForm from "app/modules/common/newsletterForm"; import { FieldErrors } from "react-hook-form"; +import axios, { AxiosError, AxiosResponse } from "axios"; const DXLogo = ( >({}); + + const [contactFormDetails, setContactFormDetails] = React.useState([ + { name: "email", value: "" }, + { name: "firstname", value: "" }, + { name: "lastname", value: "" }, + { name: "company", value: "" }, + { name: "message", value: "" }, + ]); + + const resetForm = () => { + setContactFormDetails([ + { name: "email", value: "" }, + { name: "firstname", value: "" }, + { name: "lastname", value: "" }, + { name: "company", value: "" }, + { name: "message", value: "" }, + ]); + }; + const [contactFormFailed, setContactFormFailed] = React.useState(false); + + const handleContactFormChange = ( + event: React.ChangeEvent + ) => { + const { name, value } = event.target; + setContactFormDetails((prevState) => { + const newState = prevState.map((item) => { + if (item.name === name) { + return { ...item, value }; + } + return item; + }); + return newState; + }); + }; + + const handleContactFormSubmit = (e: React.FormEvent) => { + e.preventDefault(); + + axios + .post( + `https://api.hsforms.com/submissions/v3/integration/submit/${process.env.REACT_APP_HUBSPOT_PORTAL_ID}/${process.env.REACT_APP_HUBSPOT_CONTACT_FORM_ID}`, + { + portalId: process.env.REACT_APP_HUBSPOT_PORTAL_ID, + formGuid: process.env.REACT_APP_HUBSPOT_CONTACT_FORM_ID, + fields: contactFormDetails, + }, + { + headers: { + "Content-Type": "application/json", + }, + } + ) + .then((response: AxiosResponse) => { + if (response.status === 200) { + setOpenSnackbar(true); + setMessage(response.data.inlineMessage); + resetForm(); + } else { + setContactFormFailed(true); + } + }) + .catch((error: AxiosError) => { + console.log(error); + setContactFormFailed(true); + }); + }; return ( <> + setOpenSnackbar(false)} + message={message} + />