Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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) => {
// A visit from a web browser? Serve a HTML page with a form
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" } },
);
}
// Get existing list of submitted email addresses
let data = await blob.getJSON("viduje_interest") as Data[];
// If there were no email addresses stored, create an empty array
data ??= [];
// Pick out the form data
const formData = await req.formData();
const emailAddress = formData.get("email") as string;
const id = formData.get("id") as string;
pauliusef-renderformandsavedata.web.val.run
April 26, 2024