Skip to content

Commit bf1ca9d

Browse files
committed
update contact form with success message
1 parent 89897a5 commit bf1ca9d

File tree

4 files changed

+26
-41
lines changed

4 files changed

+26
-41
lines changed

.astro/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"_variables": {
3-
"lastUpdateCheck": 1733088513537
3+
"lastUpdateCheck": 1734236046563
44
}
55
}

.astro/types.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/// <reference types="astro/client" />
2-
/// <reference path="content.d.ts" />

src/components/ContactForm.vue

+25-37
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<script setup lang="ts">
22
import { ref, onMounted } from "vue";
33
4-
// Track form submission state and response message
54
const responseMessage = ref<string>();
65
const isSubmitting = ref<boolean>(false);
6+
const formVisible = ref<boolean>(true);
77
88
const TURNSTILE_SITE_KEY = import.meta.env.PUBLIC_TURNSTILE_SITE_KEY || '{TURNSTILE_SITE_KEY}';
99
10-
// Data for form inputs with two-way binding
1110
const formData = ref({
1211
name: '',
1312
email: '',
@@ -18,16 +17,13 @@ const formData = ref({
1817
turnstileToken: ''
1918
});
2019
21-
// Cloudflare Turnstile
2220
onMounted(() => {
23-
// Dynamically load the Turnstile widget
2421
const script = document.createElement("script");
2522
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
2623
script.async = true;
2724
script.defer = true;
2825
document.head.appendChild(script);
29-
30-
// Initialize Turnstile once the script is loaded
26+
3127
script.onload = () => {
3228
if (window.turnstile) {
3329
window.turnstile.render("#turnstile-container", {
@@ -40,13 +36,11 @@ onMounted(() => {
4036
},
4137
});
4238
};
43-
4439
};
4540
});
4641
47-
// Phone number formatting logic
4842
function formatPhoneNumber(value: string): string {
49-
const cleaned = value.replace(/\D/g, ''); // Remove non-digit characters
43+
const cleaned = value.replace(/\D/g, '');
5044
const match = cleaned.match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
5145
if (!match) return '';
5246
return !match[2] ? match[1] : `${match[1]}-${match[2]}${match[3] ? '-' + match[3] : ''}`;
@@ -57,14 +51,12 @@ function handlePhoneInput(event: Event) {
5751
formData.value.phone = formatPhoneNumber(input.value);
5852
};
5953
60-
// Set character limits
6154
const nameMaxLength = 100;
6255
const emailMaxLength = 150;
6356
const titleMaxLength = 50;
6457
const companyMaxLength = 100;
6558
const messageMaxLength = 500;
6659
67-
// Handle form submission
6860
async function submit(e: Event) {
6961
e.preventDefault();
7062
@@ -78,7 +70,7 @@ async function submit(e: Event) {
7870
};
7971
8072
isSubmitting.value = true;
81-
responseMessage.value = ''; // Clear previous response message
73+
responseMessage.value = '';
8274
8375
const dataToSend = new FormData();
8476
dataToSend.append('name', formData.value.name);
@@ -89,10 +81,6 @@ async function submit(e: Event) {
8981
dataToSend.append('message', formData.value.message);
9082
dataToSend.append('turnstileToken', formData.value.turnstileToken);
9183
92-
// TODO: Rework this.
93-
// POST works and message is sent but response is ignored.
94-
// Make use of the server response!
95-
9684
try {
9785
const response = await fetch("/api/contact", {
9886
method: "POST",
@@ -103,7 +91,8 @@ async function submit(e: Event) {
10391
10492
if (response.ok) {
10593
responseMessage.value = "Message sent successfully!";
106-
resetForm();
94+
// Instead of resetting the form, hide it.
95+
formVisible.value = false;
10796
} else {
10897
responseMessage.value = `Error: ${data.message || "Something went wrong"}`;
10998
}
@@ -113,27 +102,13 @@ async function submit(e: Event) {
113102
isSubmitting.value = false;
114103
};
115104
};
116-
117-
// Reset form data
118-
function resetForm() {
119-
formData.value = {
120-
name: '',
121-
phone: '',
122-
email: '',
123-
title: '',
124-
company: '',
125-
message: '',
126-
turnstileToken: ''
127-
};
128-
responseMessage.value = '';
129-
};
130105
</script>
131106

132-
133107
<template>
134108
<div class="flex flex-col max-w-xl mx-auto rounded-lg backdrop-blur border border-[var(--highlight-blue-200)] dark:border-[var(--highlight-blue-400)] bg-[var(--neutral-100)] dark:bg-[var(--highlight-blue-900)] shadow p-4 sm:p-6 lg:p-8 w-full">
135-
<form @submit="submit">
136-
109+
110+
<!-- Show the form if formVisible is true -->
111+
<form v-if="formVisible" @submit="submit">
137112
<div class="mb-[15px]">
138113
<label for="name">Name <span class="text-red-600">*</span></label>
139114
<input
@@ -170,9 +145,9 @@ function resetForm() {
170145
class="flex items-center cursor-pointer text-[var(--neutral-400)] dark:text-[var(--neutral-100)]"
171146
title="Callbacks for US numbers only!"
172147
>
173-
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
174-
<path fill="#6388c5" d="M11 9h2V7h-2m1 13c-4.41 0-8-3.59-8-8s3.59-8 8-8s8 3.59 8 8s-3.59 8-8 8m0-18A10 10 0 0 0 2 12a10 10 0 0 0 10 10a10 10 0 0 0 10-10A10 10 0 0 0 12 2m-1 15h2v-6h-2z" />
175-
</svg>
148+
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
149+
<path fill="#6388c5" d="M11 9h2V7h-2m1 13c-4.41 0-8-3.59-8-8s3.59-8 8-8s8 3.59 8 8s-3.59 8-8 8m0-18A10 10 0 0 0 2 12a10 10 0 0 0 10 10a10 10 0 0 0 10-10A10 10 0 0 0 12 2m-1 15h2v-6h-2z" />
150+
</svg>
176151
</span>
177152
</div>
178153
<div class="mb-[15px]">
@@ -254,5 +229,18 @@ function resetForm() {
254229

255230
<p v-if="responseMessage" class="response-message">{{ responseMessage }}</p>
256231
</form>
232+
233+
<!-- Show a success message if formVisible is false -->
234+
<div v-else class="success-message">
235+
<div class="flex justify-center items-center">
236+
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 24 24">
237+
<path fill="hsl(136, 48%, 46%)" d="M20 12a8 8 0 0 1-8 8a8 8 0 0 1-8-8a8 8 0 0 1 8-8c.76 0 1.5.11 2.2.31l1.57-1.57A9.8 9.8 0 0 0 12 2A10 10 0 0 0 2 12a10 10 0 0 0 10 10a10 10 0 0 0 10-10M7.91 10.08L6.5 11.5L11 16L21 6l-1.41-1.42L11 13.17z"/>
238+
</svg>
239+
</div>
240+
<h2 class="flex justify-center items-center mb-4">Message Sent!</h2>
241+
<p class="flex justify-center text-center mb-2">Thank you for contacting us!</p>
242+
<p class="flex justify-center text-center">We will get back to you within 1-2 business days.</p>
243+
<p v-if="responseMessage">{{ responseMessage }}</p>
244+
</div>
257245
</div>
258246
</template>

src/pages/api/contact.ts

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ export const POST: APIRoute = async ({ request }) => {
3535

3636
const data = await request.formData();
3737

38-
console.log('DATA DEBUG: ' + data);
39-
4038
const turnstileToken = data.get("turnstileToken") as string;
4139

4240
if (!turnstileToken) {

0 commit comments

Comments
 (0)