Skip to content

Commit

Permalink
Merge pull request #26 from JumboCode/contact-connection
Browse files Browse the repository at this point in the history
Contact connection
  • Loading branch information
myix765 authored Nov 2, 2024
2 parents 7aeb08d + de9ec68 commit 2091cd6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
44 changes: 43 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ const User = mongoose.model("users", UserSchema)
// Contact Schema

// TODO (Frank & Madeline): Create a ContactSchema
const ContactSchema = new Schema ({
name: { type: String, required: false },
email: { type: String, required: true},
subject: { type: String, required: true},
message: { type: String, required: true}
})

const Contact = mongoose.model('Contact', ContactSchema);
// export default Contact;

// Class Schema

Expand Down Expand Up @@ -140,7 +148,41 @@ app.post('/api/login', async (req, res) => {
// Contact

// TODO (Frank & Madeline): Create an endpoint to receive and upload contact inquiries to the database

// async function uploadContact () {
// const article = new Contact({
// name: 'test user',
// email: '[email protected]',
// subject: 'Test Inquiry',
// message: 'Test message'

// });

// await article.save();
// const firstContact = await Contact.findOne({});
// console.log(firstContact);
// }
// uploadContact().catch(console.error);

// const firstArticle = await Contact.findOne({});
// console.log(firstArticle);
app.post('/api/contact', async (req, res) => {
const{ name, email, subject, message } = req.body
try {
const newContact = new Contact({
name,
email,
subject,
message
})
await newContact.save()

res.status(201).json({message: 'Inquiry submitted successfully'})
}
catch (err) {
console.error('Error submitting inquiry:', err);
res.status(500).json({message: 'Error submitting inquiry'})
}
})

// Classes

Expand Down
35 changes: 23 additions & 12 deletions src/pages/Contact.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@ export default function Contact( ) {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
const { name, email, subject, message } = formData

e.preventDefault();

const alertMessage = `
Name: ${formData.name}
Email: ${formData.email}
Subject: ${formData.subject}
Message: ${formData.message}
`;

alert(alertMessage);

console.log('Form submitted:', formData);
try {
const response = await fetch("http://localhost:4000/api/contact", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({name, email, subject, message})
})

if(!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const json = await response.json()
console.log(json)
alert("Inquiry submitted successfully!")

} catch (err) {
console.error(error.message)
alert("There was an error submitting the inquiry.")
}
};


Expand Down

0 comments on commit 2091cd6

Please sign in to comment.