Remove contact form debug logging
This commit is contained in:
parent
371e5f2f10
commit
4496ab97b7
3 changed files with 1 additions and 85 deletions
|
|
@ -57,26 +57,8 @@ const parseFormFields = async (request: Request) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePost: PagesFunction<Env> = async ({ request, env }) => {
|
const handlePost: PagesFunction<Env> = async ({ request, env }) => {
|
||||||
console.log(
|
|
||||||
"[contact] post start",
|
|
||||||
JSON.stringify({
|
|
||||||
contentType: request.headers.get("content-type"),
|
|
||||||
hasTurnstileSecret: Boolean(env.TURNSTILE_SECRET_KEY),
|
|
||||||
hasResendApiKey: Boolean(env.RESEND_API_KEY),
|
|
||||||
hasFromEmail: Boolean(env.CONTACT_FROM_EMAIL),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const { name, email, message, turnstileToken } =
|
const { name, email, message, turnstileToken } =
|
||||||
await parseFormFields(request);
|
await parseFormFields(request);
|
||||||
console.log(
|
|
||||||
"[contact] fields",
|
|
||||||
JSON.stringify({
|
|
||||||
hasName: Boolean(name),
|
|
||||||
hasEmail: Boolean(email),
|
|
||||||
hasMessage: Boolean(message),
|
|
||||||
hasTurnstileToken: Boolean(turnstileToken),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!name || !email || !message) {
|
if (!name || !email || !message) {
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
|
|
@ -115,7 +97,6 @@ const handlePost: PagesFunction<Env> = async ({ request, env }) => {
|
||||||
8000,
|
8000,
|
||||||
"Turnstile",
|
"Turnstile",
|
||||||
);
|
);
|
||||||
console.log("[contact] turnstile status", verifyResponse.status);
|
|
||||||
const verifyResult = (await verifyResponse.json()) as {
|
const verifyResult = (await verifyResponse.json()) as {
|
||||||
success?: boolean;
|
success?: boolean;
|
||||||
"error-codes"?: string[];
|
"error-codes"?: string[];
|
||||||
|
|
@ -187,7 +168,6 @@ const handlePost: PagesFunction<Env> = async ({ request, env }) => {
|
||||||
8000,
|
8000,
|
||||||
"Resend",
|
"Resend",
|
||||||
);
|
);
|
||||||
console.log("[contact] resend status", response.status);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const contentType = response.headers.get("content-type") ?? "";
|
const contentType = response.headers.get("content-type") ?? "";
|
||||||
const errorText = contentType.includes("application/json")
|
const errorText = contentType.includes("application/json")
|
||||||
|
|
@ -227,7 +207,6 @@ export const onRequest: PagesFunction<Env> = async (context) => {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
const message =
|
||||||
error instanceof Error ? error.message : "Unknown error";
|
error instanceof Error ? error.message : "Unknown error";
|
||||||
console.error("[contact] unhandled", message);
|
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ ok: false, error: "Unhandled exception.", details: message },
|
{ ok: false, error: "Unhandled exception.", details: message },
|
||||||
500,
|
500,
|
||||||
|
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
||||||
(() => {
|
|
||||||
const form = document.querySelector("[data-contact-form]");
|
|
||||||
const message = document.querySelector("[data-form-message]");
|
|
||||||
const submitButton = document.querySelector("[data-form-submit]");
|
|
||||||
|
|
||||||
if (!form || !message || !submitButton) return;
|
|
||||||
|
|
||||||
form.addEventListener("submit", async (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
if (submitButton.disabled) return;
|
|
||||||
|
|
||||||
submitButton.disabled = true;
|
|
||||||
submitButton.textContent = "Sending...";
|
|
||||||
message.textContent = "";
|
|
||||||
|
|
||||||
try {
|
|
||||||
const formData = new FormData(form);
|
|
||||||
const body = new URLSearchParams();
|
|
||||||
for (const [key, value] of formData.entries()) {
|
|
||||||
if (typeof value === "string") {
|
|
||||||
body.append(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const response = await fetch(form.action, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
||||||
body,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
let errorMessage = "Request failed";
|
|
||||||
const errorData = await response.json().catch(() => null);
|
|
||||||
if (errorData?.error) {
|
|
||||||
errorMessage = errorData.details
|
|
||||||
? `${errorData.error} (${errorData.details})`
|
|
||||||
: errorData.error;
|
|
||||||
} else {
|
|
||||||
const errorText = await response.text().catch(() => "");
|
|
||||||
if (errorText.trim()) {
|
|
||||||
errorMessage = `${response.status} ${response.statusText}: ${errorText}`;
|
|
||||||
} else {
|
|
||||||
errorMessage = `${response.status} ${response.statusText}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error(errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
message.textContent = "Thanks! Your message has been sent.";
|
|
||||||
form.reset();
|
|
||||||
if (window.turnstile && typeof window.turnstile.reset === "function") {
|
|
||||||
window.turnstile.reset();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
message.textContent =
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "Sorry, something went wrong. Please try again.";
|
|
||||||
} finally {
|
|
||||||
submitButton.disabled = false;
|
|
||||||
submitButton.textContent = "Send message";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
@ -24,7 +24,7 @@ const turnstileSiteKey = import.meta.env.PUBLIC_TURNSTILE_SITE_KEY ?? "";
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
<Fragment slot="head">
|
<Fragment slot="head">
|
||||||
<script src="/contact-form-v2.js" defer data-cfasync="false"></script>
|
<script src="/contact-form.js" defer data-cfasync="false"></script>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
<div class="flex flex-col min-h-screen items-center bg-[#e2e2e2]">
|
<div class="flex flex-col min-h-screen items-center bg-[#e2e2e2]">
|
||||||
<main class="w-full">
|
<main class="w-full">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue