-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating contact form with notification toast
- Loading branch information
Showing
8 changed files
with
194 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React, { useContext } from "react"; | ||
import ToastContext from "../contexts/ToastContext"; | ||
|
||
import axios from "axios"; | ||
|
||
const ContactForm = () => { | ||
const { showToast } = useContext(ToastContext); | ||
|
||
const handleSubmitMessage = async (e) => { | ||
e.preventDefault(); // Prevent the default form submission behavior | ||
|
||
const form = e.target; | ||
const formData = new FormData(form); | ||
|
||
try { | ||
const response = await axios.post(form.action, formData, { | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
}); | ||
|
||
if (response.status === 200) { | ||
showToast("Message sent successfully!", "success"); | ||
// Reset the form if needed | ||
form.reset(); | ||
} else { | ||
showToast("Error submitting Message. Please try again.", "error"); | ||
} | ||
} catch (error) { | ||
showToast("Error submitting Message. Please try again.", "error"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<form | ||
action="https://getform.io/f/b0d67c57-9226-4c38-be8c-76357605bb9c" | ||
method="POST" | ||
onSubmit={handleSubmitMessage} | ||
className="w-full" | ||
> | ||
<div className="flex flex-wrap -mx-3 mb-6"> | ||
<div className="w-full px-3"> | ||
<label | ||
htmlFor="name" | ||
className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" | ||
> | ||
Name | ||
</label> | ||
<input | ||
className="appearance-none block w-full text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" | ||
id="name" | ||
name="name" | ||
type="text" | ||
placeholder="Name" | ||
/> | ||
</div> | ||
</div> | ||
<div className="flex flex-wrap -mx-3 mb-6"> | ||
<div className="w-full px-3"> | ||
<label | ||
htmlFor="email" | ||
className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" | ||
> | ||
</label> | ||
<input | ||
className="appearance-none block w-full text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" | ||
id="email" | ||
name="email" | ||
type="email" | ||
placeholder="Email" | ||
/> | ||
</div> | ||
</div> | ||
<div className="flex flex-wrap -mx-3 mb-6"> | ||
<div className="w-full px-3"> | ||
<label | ||
htmlFor="message" | ||
className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" | ||
> | ||
Message | ||
</label> | ||
<textarea | ||
className="no-resize appearance-none block w-full text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 h-48 resize-none" | ||
id="message" | ||
name="message" | ||
placeholder="Message" | ||
></textarea> | ||
</div> | ||
</div> | ||
<div className="md:flex md:items-center"> | ||
<div className="md:w-1/3"> | ||
<button | ||
className="shadow bg-black hover:bg-gray-500 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded" | ||
type="submit" | ||
> | ||
Send | ||
</button> | ||
</div> | ||
<div className="md:w-2/3"></div> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ContactForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useContext } from 'react'; | ||
import { Toast } from 'flowbite-react'; | ||
import { FaTelegramPlane, FaTimesCircle } from 'react-icons/fa'; | ||
import ToastContext from '../contexts/ToastContext'; | ||
|
||
const ContactToast = () => { | ||
const { toastInfo, setToastInfo } = useContext(ToastContext); | ||
console.log(toastInfo) | ||
|
||
const backgroundColor = toastInfo.type === 'success' ? '!bg-green-200 dark:!bg-green-700' : '!bg-red-200 dark:!bg-red-700'; | ||
const textColor = toastInfo.type === 'success' ? 'text-green-500 dark:text-green-200' : 'text-red-500 dark:text-red-200'; | ||
|
||
return ( | ||
<div className="fixed top-5 right-5 z-50"> | ||
<Toast onClose={() => setToastInfo(null)} className={backgroundColor}> | ||
<div className={`inline-flex h-8 w-8 items-center justify-center rounded-lg ${textColor}`}> | ||
{toastInfo.type === 'success' ? ( | ||
<FaTelegramPlane className={`h-5 w-5 ${textColor}`} /> | ||
) : ( | ||
<FaTimesCircle className={`h-5 w-5 ${textColor}`} /> | ||
)} | ||
</div> | ||
<div className={`mx-3 text-sm font-normal ${textColor}`}>{toastInfo.message}</div> | ||
<Toast.Toggle /> | ||
</Toast> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ContactToast; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createContext, useState } from 'react'; | ||
import ContactToast from '../components/ContactToast'; | ||
|
||
const ToastContext = createContext(); | ||
|
||
export const ToastProvider = ({ children }) => { | ||
const [toastInfo, setToastInfo] = useState({ message:"", type:"", visible: false }); | ||
|
||
const showToast = (message, type, duration = 5000) => { | ||
setToastInfo({ message, type, visible: true }); | ||
|
||
setTimeout(() => { | ||
setToastInfo({ message:"", type:"", visible: false }); | ||
}, duration); | ||
}; | ||
|
||
return ( | ||
<ToastContext.Provider value={{ toastInfo, setToastInfo, showToast }}> | ||
{children} | ||
{toastInfo.visible && <ContactToast />} | ||
</ToastContext.Provider> | ||
); | ||
}; | ||
|
||
export default ToastContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters