Skip to content

Commit

Permalink
Return form submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminEHowe committed Jun 23, 2024
1 parent ae77e36 commit 5efdeca
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion contact-urgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Urgent Contact

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

<form id="contact-form" method="post" action="/api/contact-urgent">
<form id="contact-form" method="post" action="/api/form/contact-urgent">
<fieldset style="display:none">
<label for="name">Leave blank if you're human:</label>
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">
Expand Down
2 changes: 1 addition & 1 deletion contact.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Contact

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

<form id="contact-form" method="post" action="/api/contact">
<form id="contact-form" method="post" action="/api/form/contact">
<fieldset style="display:none">
<label for="name">Leave blank if you're human:</label>
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleForm } from "../../functions-src/forms.js"
import { handleForm } from "../../../functions-src/forms.js"

export async function onRequest(context) {
if (context.request.method !== "POST") {
Expand Down
2 changes: 1 addition & 1 deletion functions/api/contact.js → functions/api/form/contact.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleForm } from "../../functions-src/forms.js"
import { handleForm } from "../../../functions-src/forms.js"

export async function onRequest(context) {
if (context.request.method !== "POST") {
Expand Down
20 changes: 20 additions & 0 deletions functions/secure/api/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export async function onRequest(context) {
if (context.request.method !== "GET") {
return new Response("Invalid request method", { status: 405 });
}

const submissions = await context.env.DB_FORMS.prepare("SELECT * FROM submissions WHERE submitted_ts > ?")
.bind(
new Date().subtractDays(30).toISOString(),
)
.all();

return Response.json(submissions);
}

// TODO: this is bad practice, consider replacing (see https://www.reddit.com/r/learnjavascript/comments/qgtut6/comment/hi8jg6w/)
Date.prototype.subtractDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() - days);
return date;
}

0 comments on commit 5efdeca

Please sign in to comment.