Skip to content

Commit 98e8f23

Browse files
committed
Implement contact form functionality with TypeScript, including form submission handling and API integration. Update HTML to link the new script and adjust TypeScript configuration to include DOM library.
1 parent ded57ad commit 98e8f23

3 files changed

Lines changed: 48 additions & 27 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const contactName: HTMLInputElement | null = document.querySelector("#name");
2+
const email: HTMLInputElement | null = document.querySelector("#email");
3+
const message: HTMLTextAreaElement | null = document.querySelector("#message");
4+
const submitButton: HTMLButtonElement | null =
5+
document.querySelector("#submit-button");
6+
7+
async function sendContactForm({
8+
contactName,
9+
email,
10+
message,
11+
}: {
12+
contactName: string;
13+
email: string;
14+
message: string;
15+
}): Promise<void> {
16+
const url = "//api/contact-us-form";
17+
18+
await fetch(url, {
19+
method: "POST",
20+
body: JSON.stringify({ contactName, email, message }),
21+
headers: {
22+
"Content-Type": "application/json",
23+
},
24+
});
25+
}
26+
27+
const doSubmit = async () => {
28+
if (contactName?.value && email?.value && message?.value) {
29+
await sendContactForm({
30+
contactName: contactName.value,
31+
email: email.value,
32+
message: message.value,
33+
});
34+
}
35+
};
36+
37+
if (submitButton) {
38+
submitButton.addEventListener("click", (event: Event) => {
39+
event.preventDefault();
40+
event.stopPropagation();
41+
void doSubmit();
42+
});
43+
}

hugo/layouts/contact-us/single.html

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,13 @@ <h1 class="contact-us-page__title">{{ .Title }}</h1>
5555
>
5656
</div>
5757

58-
<button type="submit" class="button">Submit</button>
58+
<button type="submit" id="submit-button" class="button">Submit</button>
5959
</form>
6060
</section>
61-
62-
<div id="confirmation-message" class="form__confirmation" hidden>
63-
<h2>Thank you!</h2>
64-
<p>Your message has been sent. We'll get back to you shortly.</p>
65-
</div>
6661
{{ end }}
6762

6863
{{ define "footer-scripts" }}
69-
<script>
70-
document.addEventListener("DOMContentLoaded", function () {
71-
const form = document.querySelector(".contact-us-page__form");
72-
const confirmationMessage = document.getElementById(
73-
"confirmation-message",
74-
);
75-
76-
form.addEventListener("submit", function (event) {
77-
event.preventDefault();
78-
event.stopPropagation();
79-
80-
if (!form.checkValidity()) {
81-
form.classList.add("was-validated");
82-
} else {
83-
form.hidden = true;
84-
confirmationMessage.hidden = false;
85-
}
86-
});
87-
});
88-
</script>
64+
{{ $opts := dict "targetPath" "contact-us-form.js" }}
65+
{{ $built := resources.Get "ts/contact-us-form.ts" | js.Build $opts | resources.Minify | resources.Fingerprint }}
66+
<script type="text/javascript" src="{{ $built.RelPermalink }}" defer></script>
8967
{{ end }}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
// Environment setup & latest features
4-
"lib": ["ESNext"],
4+
"lib": ["ESNext", "dom"],
55
"target": "ESNext",
66
"module": "Preserve",
77
"moduleDetection": "force",

0 commit comments

Comments
 (0)