Skip to content

Commit 6466f75

Browse files
committed
Update contact form to dynamically set API URL: retrieve URL from form data attribute and add error handling for missing URL.
1 parent 9167996 commit 6466f75

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

hugo/assets/ts/contact-us-form.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const contactForm: HTMLFormElement | null = document.querySelector(".form");
12
const contactName: HTMLInputElement | null = document.querySelector("#name");
23
const email: HTMLInputElement | null = document.querySelector("#email");
34
const message: HTMLTextAreaElement | null = document.querySelector("#message");
@@ -14,7 +15,10 @@ async function sendContactForm({
1415
email: string;
1516
message: string;
1617
}): Promise<void> {
17-
const url = "/api/contact-us-form";
18+
const url = contactForm?.dataset["apiUrl"];
19+
if (!url) {
20+
throw new Error("API URL not found");
21+
}
1822

1923
const response = await fetch(url, {
2024
method: "POST",

hugo/layouts/contact-us/single.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ <h1 class="contact-us-page__title">{{ .Title }}</h1>
2525
</div>
2626

2727
<section class="text-content">
28-
<form class="form" novalidate>
28+
<form
29+
class="form"
30+
novalidate
31+
data-api-url="{{ if hugo.IsProduction }}
32+
/api/contact-us-form
33+
{{ else }}
34+
http://127.0.0.1:5001/doula-cooperative/us-central1/contactUsForm
35+
{{ end }}">
2936
<div class="form-group full-width">
3037
<label for="name" class="form__label">Name (first and last)</label>
3138
<input type="text" id="name" class="form__input" required />

0 commit comments

Comments
 (0)