Skip to content

Commit

Permalink
Optimized form submission logic for testing and add test data for dev…
Browse files Browse the repository at this point in the history
… env
  • Loading branch information
OudomMunint committed Aug 17, 2024
1 parent e184126 commit d85cbc0
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/components/Contact.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import React, { useState } from "react";
import React, { useState, useEffect, useCallback } from "react";

function ContactForm() {
const isDevelopment = process.env.NODE_ENV === "development";
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [message, setMessage] = useState("");

const handleSubmit = async (event) => {
const handleSubmit = useCallback(async (event) => {
event.preventDefault();
let data = { name, email, message };
try {
// clear form
setName('');
setEmail('');
setMessage('');
data = { name: "testUser1", email: "[email protected]", message: "Hello, this is a test message." };
console.log('Form submission successful:');
console.log(data);
if (isDevelopment) {
data = {
name: "testUser1",
email: "[email protected]",
message: "Hello, this is a test message."
};
}
console.log('Form submission successful:', data);
} catch (error) {
console.error('Form submission error:', error);
console.log('Error caught');
}
};
}, [name, email, message, isDevelopment]);

if (isDevelopment) {
try {
document.getElementsByClassName("submit").addEventListener("click", handleSubmit);
}
catch (error) {
console.error('Error:', error);
}
}
else {
try {
document.getElementsByClassName("submit").removeEventListener("click", handleSubmit);
}
catch (error) {
console.error('Error:', error);
useEffect(() => {
if (isDevelopment) {
const button = document.querySelector(".submit");
if (button) {
button.addEventListener("click", handleSubmit);
}
return () => {
if (button) {
button.removeEventListener("click", handleSubmit);
}
};
}
}
}, [isDevelopment, handleSubmit]);

return (
<>
Expand Down Expand Up @@ -84,7 +85,7 @@ function ContactForm() {
</div>
<div data-netlify-recaptcha="true" className="reCaptcha"></div>
{/* Submit */}
<button className="btn btn-danger submit" type="submit" style={{ position: "relative", marginTop: "68px" }}>
<button title="submit" className="btn btn-danger submit" type="submit" style={{ position: "relative", marginTop: "68px" }}>
Submit
</button>
</form>
Expand Down

0 comments on commit d85cbc0

Please sign in to comment.