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 === "GET") {
return html(`
<!DOCTYPE html>
<html>
<head>
<title>Email Form</title>
</head>
<body>
<form action="/" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
}
const submittedEmailAddresses = (await blob.getJSON("submittedEmailAddresses")) ?? [];
const formData = await req.formData();
const emailAddress = formData.get("email");
if (submittedEmailAddresses.includes(emailAddress)) {
return new Response("you're already signed up!");
}
await email({
text: `${emailAddress} just signed up!`,
subject: "new sign up",
});
submittedEmailAddresses.push(emailAddress);
await blob.setJSON(
"submittedEmailAddresses",
submittedEmailAddresses,
);
return new Response("thanks! you're signed up!");
};