Skip to content

Commit 5efdeca

Browse files
committed
Return form submissions
1 parent ae77e36 commit 5efdeca

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

contact-urgent.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Urgent Contact
55

66
If you have an access token then you can send me an urgent message using this form:
77

8-
<form id="contact-form" method="post" action="/api/contact-urgent">
8+
<form id="contact-form" method="post" action="/api/form/contact-urgent">
99
<fieldset style="display:none">
1010
<label for="name">Leave blank if you're human:</label>
1111
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">

contact.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Contact
55

66
If you'd like to get in touch then you can contact me using the below form.
77

8-
<form id="contact-form" method="post" action="/api/contact">
8+
<form id="contact-form" method="post" action="/api/form/contact">
99
<fieldset style="display:none">
1010
<label for="name">Leave blank if you're human:</label>
1111
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">

functions/api/contact-urgent.js renamed to functions/api/form/contact-urgent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleForm } from "../../functions-src/forms.js"
1+
import { handleForm } from "../../../functions-src/forms.js"
22

33
export async function onRequest(context) {
44
if (context.request.method !== "POST") {

functions/api/contact.js renamed to functions/api/form/contact.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleForm } from "../../functions-src/forms.js"
1+
import { handleForm } from "../../../functions-src/forms.js"
22

33
export async function onRequest(context) {
44
if (context.request.method !== "POST") {

functions/secure/api/form.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export async function onRequest(context) {
2+
if (context.request.method !== "GET") {
3+
return new Response("Invalid request method", { status: 405 });
4+
}
5+
6+
const submissions = await context.env.DB_FORMS.prepare("SELECT * FROM submissions WHERE submitted_ts > ?")
7+
.bind(
8+
new Date().subtractDays(30).toISOString(),
9+
)
10+
.all();
11+
12+
return Response.json(submissions);
13+
}
14+
15+
// TODO: this is bad practice, consider replacing (see https://www.reddit.com/r/learnjavascript/comments/qgtut6/comment/hi8jg6w/)
16+
Date.prototype.subtractDays = function(days) {
17+
var date = new Date(this.valueOf());
18+
date.setDate(date.getDate() - days);
19+
return date;
20+
}

0 commit comments

Comments
 (0)