hezarfen_landing
Clone
https://git.tahinli.com/archive/hezarfen_landing.git
0 watching0 forksTypeScript581.6 KiB94b1804Burak Boduroğlu2 months ago15 commits
| 1 | import { action } from "@solidjs/router"; |
| 2 | |
| 3 | /** |
| 4 | * The demo request, server side. |
| 5 | * |
| 6 | * Delivery is a webhook rather than an SMTP client: the only thing this site |
| 7 | * has to do is get the request in front of a human, and a single URL in the |
| 8 | * environment (Slack, Discord, Make, Resend's own endpoint — anything that |
| 9 | * takes a JSON POST) does that without shipping mail credentials in the |
| 10 | * build. `CONTACT_WEBHOOK_URL` is that URL. |
| 11 | * |
| 12 | * With no webhook configured the submission is logged and still reported as |
| 13 | * received, because the form also states the address underneath it: the |
| 14 | * request is never silently dropped without the visitor being told where |
| 15 | * else to write. |
| 16 | */ |
| 17 | |
| 18 | export type ContactResult = { ok: true } | { ok: false; field?: string; code: string }; |
| 19 | |
| 20 | const text = (form: FormData, key: string) => String(form.get(key) ?? "").trim(); |
| 21 | |
| 22 | /** Deliberately loose: a regex cannot validate an address, only reject typos. */ |
| 23 | const looksLikeEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(value); |
| 24 | |
| 25 | export const submitContact = action(async (form: FormData): Promise<ContactResult> => { |
| 26 | "use server"; |
| 27 | |
| 28 | const name = text(form, "name"); |
| 29 | const institution = text(form, "institution"); |
| 30 | const email = text(form, "email"); |
| 31 | const consent = form.get("consent") !== null; |
| 32 | |
| 33 | if (name.length < 2) return { ok: false, field: "name", code: "name" }; |
| 34 | if (institution.length < 2) return { ok: false, field: "institution", code: "institution" }; |
| 35 | if (!looksLikeEmail(email)) return { ok: false, field: "email", code: "email" }; |
| 36 | if (!consent) return { ok: false, field: "consent", code: "consent" }; |
| 37 | |
| 38 | const payload = { |
| 39 | name, |
| 40 | institution, |
| 41 | role: text(form, "role"), |
| 42 | email, |
| 43 | phone: text(form, "phone"), |
| 44 | students: text(form, "students"), |
| 45 | // Checkbox groups arrive as repeated keys; `getAll` is the only way to |
| 46 | // read every box that was ticked rather than the last one. |
| 47 | interest: form.getAll("interest").map(String), |
| 48 | message: text(form, "message"), |
| 49 | receivedAt: new Date().toISOString(), |
| 50 | }; |
| 51 | |
| 52 | const webhook = process.env.CONTACT_WEBHOOK_URL; |
| 53 | |
| 54 | if (!webhook) { |
| 55 | console.info("[contact] no CONTACT_WEBHOOK_URL set, logging instead:", payload); |
| 56 | return { ok: true }; |
| 57 | } |
| 58 | |
| 59 | try { |
| 60 | const response = await fetch(webhook, { |
| 61 | method: "POST", |
| 62 | headers: { "content-type": "application/json" }, |
| 63 | body: JSON.stringify(payload), |
| 64 | }); |
| 65 | if (!response.ok) throw new Error(`webhook responded ${response.status}`); |
| 66 | } catch (error) { |
| 67 | // The visitor is told to write directly; the reason is kept server side. |
| 68 | console.error("[contact] delivery failed:", error); |
| 69 | return { ok: false, code: "unknown" }; |
| 70 | } |
| 71 | |
| 72 | return { ok: true }; |
| 73 | }, "contact"); |