Files
Portfolio/src/components/Contact/ContactForm.astro
2026-05-15 21:50:30 +02:00

169 lines
5.3 KiB
Plaintext

---
const { title, description } = Astro.props;
import Box from "../Box.astro";
---
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<Box>
<form id="contactForm">
<input type="text" name="_honey" id="honey" style="display:none;" tabindex="-1" autocomplete="off" />
<input type="text" name="name" id="nameInput" class="name"placeholder="John Doe" autocomplete="name" required>
<input type="email" name="email" id="emailInput" class="email" placeholder="john@example.com" autocomplete="email" required>
<textarea name="message" id="messageInput" placeholder="Your message here" required></textarea>
<input type="submit" value="Indienen" class="submitBtn">
</form>
</Box>
<script>
import Swal from 'sweetalert2';
const form = document.getElementById('contactForm') as HTMLFormElement | null;
if (form) {
form.addEventListener('submit', async (e) => {
e.preventDefault();
const nameInput = document.getElementById('nameInput') as HTMLInputElement | null;
const emailInput = document.getElementById('emailInput') as HTMLInputElement | null;
const messageInput = document.getElementById('messageInput') as HTMLTextAreaElement | null;
const honeyInput = document.getElementById('honey') as HTMLInputElement | null;
if (!nameInput || !emailInput || !messageInput || !honeyInput) {
console.error("One or more form fields were not found in the DOM.");
return;
}
const formData = new FormData();
formData.append('name', nameInput.value);
formData.append('email', emailInput.value);
formData.append('message', messageInput.value);
formData.append('_honey', honeyInput.value);
Swal.fire({
title: 'Sending...',
didOpen: () => Swal.showLoading()
});
try {
const response = await fetch('https://contact.herpiederpiee.nl/submit', {
method: 'POST',
body: formData
});
const result = await response.json();
if (response.status === 201) {
Swal.fire({
icon: 'success',
title: 'Success!',
text: 'Your message has been received!',
});
form.reset();
} else if (response.status === 429) {
Swal.fire({
icon: 'error',
title: 'Slow down!',
text: result.detail,
});
} else {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: result.detail || 'Something went wrong.',
});
}
} catch (error) {
Swal.fire({
icon: 'error',
title: 'Error',
text: 'Could not connect to the contact server.',
});
}
});
}
</script>
<style>
input, textarea {
font-family: Inter, Roboto, 'Helvetica Neue', 'Arial Nova', 'Nimbus Sans', Arial, sans-serif;
border-radius: 10px;
color: white;
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(18, 18, 28, 0.24);
backdrop-filter: blur(2px);
transition: all 200ms ease;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
font-size: 0.75rem;
padding: 0.8rem 1rem;
}
input::placeholder, textarea::placeholder {
color: rgba(255, 255, 255, 0.5);
}
input:focus, textarea:focus {
outline: none;
border-color: rgba(255, 255, 255, 0.3);
background: rgba(18, 18, 28, 0.4);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 0 0 3px rgba(255, 255, 255, 0.1);
transform: translateY(-2px);
}
input:hover, textarea:hover {
border-color: rgba(255, 255, 255, 0.2);
background: rgba(18, 18, 28, 0.35);
}
.submitBtn {
background: linear-gradient(135deg, rgba(140, 90, 20, 0.4), rgba(96, 67, 8, 0.3));
border: 1px solid rgba(255, 255, 255, 0.15);
color: white;
font-weight: 600;
cursor: pointer;
padding: unset !important;
transition: all 200ms ease;
height: 3rem !important;
border-radius: 10px;
}
.submitBtn:hover {
background: linear-gradient(135deg, rgba(160, 110, 30, 0.5), rgba(116, 87, 18, 0.4));
border-color: rgba(255, 255, 255, 0.25);
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
}
.submitBtn:active {
transform: translateY(0);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
form {
display: flex;
flex-direction: column;
justify-content: flex-start;
width: 100%;
gap: 0.6rem;
}
form textarea {
height: 8rem;
resize: none;
}
@media (max-width: 768px) {
form {
width: 100%;
gap: 0.6rem;
}
input, textarea {
font-size: 0.75rem;
padding: 0.8rem 1rem;
}
}
</style>