import { blob } from "https://esm.town/v/std/blob?v=11";
import { email } from "https://esm.town/v/std/email?v=11";
import { thisWebURL } from "https://esm.town/v/stevekrouse/thisWebURL?v=2";
interface Data {
email: string;
product: {
id: string;
url?: string;
handle?: string;
title?: string;
};
}
export const renderFormAndSaveData = async (req: Request) => {
if (req.method === "GET") {
const requestUrl = new URL(req.url);
const id = requestUrl.searchParams.get("productId");
const url = requestUrl.searchParams.get("productUrl");
const handle = requestUrl.searchParams.get("productHandle");
const title = requestUrl.searchParams.get("productTitle")?.replace(/\+/g, " ");
return new Response(
`
<!DOCTYPE html>
<html>
<head>
<title>Email Form</title>
<style>
body { font-family: Satoshi, sans-serif; padding: 0; margin: 0; }
form {
display: flex;
flex-direction: column;
gap: 12px;
}
input[type=email] {
border-radius: 4px;
border: 1px solid rgb(80, 78, 53);
height: 45px;
color: rgb(80, 78, 53);
font-size: 16px;
font-variation-settings: 'wght' 450;
letter-spacing: 0.64px;
padding: 0 12px;
}
input[type=submit] {
border: none;
border-radius: 40px;
font-size: 15px;
letter-spacing: 1px;
line-height-18px;
color: rgb(80, 78, 53);
background-color: rgb(202, 193, 115);
appearance: none;
height: 45px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.hp {
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 0;
width: 0;
z-index: -1;
}
</style>
</head>
<body>
<form action="${thisWebURL()}" method="post">
<input type="email" id="email" name="email" placeholder="hi@email.com" required>
<input type="text" id="name" name="name" placeholder="Your Name" class="hp">
<input type="hidden" id="id" name="id" value="${id}">
<input type="hidden" id="url" name="url" value="${url}">
<input type="hidden" id="handle" name="handle" value="${handle}">
<input type="hidden" id="title" name="title" value="${title}">
<input type="submit" value="Registruotis">
</form>
</body>
</html>
`,
{ headers: { "Content-Type": "text/html" } },
);
}
let data = await blob.getJSON("viduje_interest") as Data[];
data ??= [];
const formData = await req.formData();
const emailAddress = formData.get("email") as string;
const id = formData.get("id") as string;