Skip to content

Instantly share code, notes, and snippets.

@PavlikPolivka
Created July 10, 2022 18:33
Show Gist options
  • Save PavlikPolivka/c86410024aeb55366cc4d76c26004df0 to your computer and use it in GitHub Desktop.
Save PavlikPolivka/c86410024aeb55366cc4d76c26004df0 to your computer and use it in GitHub Desktop.
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
import {useState} from 'react';
export default function Contact() {
const [send, setSend] = useState(false);
async function handleSubmit(event) {
event.preventDefault()
const data = {
email: event.target.email.value,
name: event.target.name.value,
message: event.target.message.value,
}
const JSONdata = JSON.stringify(data)
const endpoint = '/api/email'
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSONdata,
}
const response = await fetch(endpoint, options)
setSend(true)
}
let content;
if (send) {
content = <>
<p>Thank you for contacting me!</p>
</>
} else {
content = <>
<form onSubmit={handleSubmit}>
<TextField id="email" label="Email" variant="outlined" required />
<TextField id="name" label="Name" variant="outlined" required />
<TextField id="message" label="Message" variant="outlined" required multiline rows={4} />
<Button variant="contained" type="submit">Send</Button>
</form>
</>
}
return (
<>
{content}
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment