Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact connection #26

Merged
merged 5 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 All @@ -64,7 +72,41 @@ const User = mongoose.model("users", UserSchema)
// 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