Branchwell

Lumen & Gesso / website · lumengesso.com / app / components / CareersForm.tsx

1'use client';
2
3import { useEffect } from 'react';
4
5// Wires the template's form (#cr-form: name, email, message, honeypot name="website").
6// Same checks and payload as hosting-research's careers.js (the owner 2026-12-02: Name, Email and Message, all required).
7function useCareersForm() {
8 useEffect(() => {
9 const f = document.getElementById('cr-form') as HTMLFormElement | null;
10 if (!f) return undefined;
11 const form = f;
12 const m = document.getElementById('cr-msg') as HTMLElement;
13 const field = (name: string) => ((form.elements.namedItem(name) as HTMLInputElement | null)?.value || '').trim();
14 async function submit(e: Event) {
15 e.preventDefault();
16 const n = field('name');
17 const em = field('email');
18 const t = field('request');
19 if (!n) { m.textContent = 'Please add your name.'; return; }
20 if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(em)) { m.textContent = 'Please add a valid email.'; return; }
21 if (!t) { m.textContent = 'Please write a short message.'; return; }
22 const b = form.querySelector('button') as HTMLButtonElement;
23 b.disabled = true;
24 m.textContent = 'Sending…';
25 try {
26 const r = await fetch('/api/request', {
27 method: 'POST',
28 headers: { 'content-type': 'application/json' },
29 body: JSON.stringify({ request: 'Name: ' + n + '\nEmail: ' + em + '\n\n' + t, name: n, email: em, message: t, site: location.hostname, website: field('website') }),
30 });
31 if (!r.ok) throw new Error(String(r.status));
32 form.reset();
33 m.textContent = 'Thanks. We got your note and will be in touch about joining our team.';
34 } catch {
35 m.textContent = 'That didn’t go through. Please try again in a minute.';
36 } finally {
37 b.disabled = false;
38 }
39 }
40 form.addEventListener('submit', submit);
41 return () => form.removeEventListener('submit', submit);
42 }, []);
43}
44
45export function CareersFormScript() {
46 useCareersForm();
47 return null;
48}