1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { blob } from "https://esm.town/v/std/blob?v=11";
import { email } from "https://esm.town/v/std/email?v=9";
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
export const renderFormAndSaveData = async (
req: Request,
): Promise<Response> => {
if (req.method !== "POST") {
return new Response("method not allowed", { status: 405 });
}
// Otherwise, someone has submitted a form so let's handle that
const events = (await blob.getJSON("events")) ?? {};
const formData = await req.formData();
const id = formData.get("id");
const name = formData.get("name");
const plusOnes = formData.get("plus_ones");
let event = events[id] ?? {};
// if (names.includes(name)) {
// return new Response("you're already signed up!");
// }
await email({
text: `${name} rsvp'd to ${id}. plus ones: ${plusOnes}`,
subject: `new rsvp to ${id}`,
});
const rsvps = event.rsvps ?? [];
event = {
...event,
rsvps,
};
events[id] = event;
await blob.setJSON(
"events",
events,
);
return new Response("thanks! you're signed up!");
};