Public
Express
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 { getTimeInTimezone } from "https://esm.town/v/ryanguill/getTimeInTimezone";
import { submitRoamRequest } from "https://esm.town/v/ryanguill/submitRoamRequest";
import { createRoamPayload } from "https://esm.town/v/ryanguill/createRoamPayload";
import { config as config2 } from "https://esm.town/v/ryanguill/config";
import process from "node:process";
let { submittedEmailAddresses } = await import("https://esm.town/v/ryanguill/submittedEmailAddresses");
import { layout } from "https://esm.town/v/ryanguill/layout";
import { thisExpressURL } from "https://esm.town/v/ryanguill/thisExpressURL";
export const roamForm =
(async (req: express.Request, res: express.Response) => {
const thisURL = thisExpressURL();
if (req.method === "GET") {
return res.send(layout(`
<form action="${thisURL}" method="post">
<form class="align-items-center">
<div class="row">
<div class="col-auto">
<label for="username" class="form-label">username:</label>
<input type="text" id="username" name="username" required class="form-control hide-alert-on-blur">
</div>
</div>
<div class="row">
<div class="col-auto">
<label for="password" class="form-label">password:</label>
<input type="password" id="password" name="password" required class="form-control hide-alert-on-blur">
</div>
</div>
<div class="row">
<div class="col-auto">
<label for="contents" class="form-label">note:</label>
<textarea name="contents" id="contents" rows="10" cols="100" placeholder="the note you want to send to roam" class="form-control hide-alert-on-blur"></textarea>
<small>Your note will automatically include the date and time, and your name.</small>
</div>
</div>
<div class="row">
<div class="col-auto">
<button type="submit" class="btn btn-primary hide-alert-on-blur">Send note to roam</button>
</div>
</div>
</form>
</div>
<script>
// Assign to individual textarea (most efficient)
document.getElementById("contents").addEventListener('keydown', function(e) {
if(e.keyCode == 13 && e.metaKey) {
this.form.submit();
}
});
document.getElementById("username").focus()
</script>
`));
}
// Otherwise, someone has submitted a form so let's handle that
if (submittedEmailAddresses === undefined) {
submittedEmailAddresses = [];
}
const submittedUsername: string = req.body.username;
const submittedPassword: string = req.body.password;
const usernamesAndPasswords = JSON.parse(
process.env.roamform_users_passwords,
);
if (
!usernamesAndPasswords.hasOwnProperty(submittedUsername.toLowerCase()) ||
usernamesAndPasswords[submittedUsername.toLowerCase()] !==
submittedPassword
) {
return res.send(
layout(
`<div class="row"><div class="alert alert-danger" role="alert" id="success-alert">Invalid Password!</div></div>`,
),
);
}
const submittedContents: string = req.body.contents;
const config = config2;
const payload = createRoamPayload(
config,
submittedContents,
submittedUsername,
);
const response = await submitRoamRequest(config, payload);
if (response.status === 200) {
console.email({
html: `${submittedUsername} just sent the following note to roam:
${submittedContents}
`,
subject: `roamForm: ${submittedUsername} submitted a note at ${
getTimeInTimezone(config.timezone)
}`,
});
return res.send(layout(`
<div class="row">
<div class="alert alert-success" role="alert" id="success-alert">
Successfully posted note to Roam!
</div>
</div>
<div class="row">
<div class="col">
<a href="${thisURL}">
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
ryanguill-roamform.express.val.run
October 23, 2023