Skip to content

Commit

Permalink
add state so double submissions are prevented
Browse files Browse the repository at this point in the history
  • Loading branch information
ksentak committed Feb 8, 2021
1 parent 4871d7a commit b827db1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-contact-form",
"version": "0.1.0",
"version": "1.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
Expand Down
8 changes: 7 additions & 1 deletion src/ContactForm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import emailjs from 'emailjs-com';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.min.css';

const ContactForm = () => {
const { register, errors, handleSubmit, reset } = useForm();
const [disabled, setDisabled] = useState(false);

const toastifySuccess = () => {
toast('Form sent!', {
Expand All @@ -22,12 +24,14 @@ const ContactForm = () => {
const onSubmit = async (data) => {
// Send form email
try {
setDisabled(true);
const templateParams = {
name: data.name,
email: data.email,
subject: data.subject,
message: data.message
};
console.log('clicked');

await emailjs.send(
process.env.REACT_APP_SERVICE_ID,
Expand All @@ -38,6 +42,7 @@ const ContactForm = () => {

reset();
toastifySuccess();
setDisabled(false);
} catch (e) {
console.log(e);
}
Expand Down Expand Up @@ -120,7 +125,8 @@ const ContactForm = () => {
{errors.message && <span className='errorMessage'>Please enter a message</span>}
</div>
</div>
<button className='submit-btn' type='submit'>

<button className='submit-btn' disabled={disabled} type='submit'>
Submit
</button>
</form>
Expand Down
6 changes: 6 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ form {
border: 2px solid var(--green);
}

.submit-btn:disabled:hover {
background-color: var(--dark);
color: var(--green) !important;
cursor: wait;
}

@media (max-width: 768px) {
.submit-message {
width: 125px;
Expand Down

0 comments on commit b827db1

Please sign in to comment.