From a6839bdd152cf493fb6ff7545aa1168da701cd7b Mon Sep 17 00:00:00 2001 From: "Stephen J. Lu" Date: Sat, 4 Jan 2025 15:54:13 -0800 Subject: [PATCH] deletion dev --- app/routes/r2-test/r2-worker-test.tsx | 70 +++++++++++++++++++++------ 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/app/routes/r2-test/r2-worker-test.tsx b/app/routes/r2-test/r2-worker-test.tsx index d2eefae..9a1e35b 100644 --- a/app/routes/r2-test/r2-worker-test.tsx +++ b/app/routes/r2-test/r2-worker-test.tsx @@ -12,6 +12,7 @@ interface ActionData { name?: string; comment?: string; turnstile?: string; + delete?: string; }; } @@ -43,8 +44,30 @@ export const loader = async ({ context }: { context: any }) => { }; export const action = async ({ request, context }: { request: Request; context: any }) => { - try { - const formData = await request.formData(); +const formData = await request.formData(); + const intent = formData.get('intent'); + + // Handle delete + if (intent === 'delete') { + const timestamp = formData.get('timestamp') as string; + if (!timestamp) { + return json({ success: false, errors: { comment: 'Missing timestamp for deletion' } }); + } + + const response = await fetch('https://r2-worker.stephenjlu.com/comments.json', { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + 'X-Custom-Auth-Key': context.cloudflare.env.AUTH_KEY_SECRET, + }, + body: JSON.stringify({ timestamp }) + }); + + if (!response.ok) throw new Error('Failed to delete comment'); + return json({ success: true }); + } + + try { const name = formData.get('name') as string; const comment = formData.get('comment') as string; const token = formData.get('cf-turnstile-response') as string; @@ -136,20 +159,37 @@ export default function R2WorkerTest() { )}
-

Comments ({loaderData.comments.length})

-
- {loaderData.comments.length === 0 ? ( -
No comments yet!
- ) : ( - loaderData.comments.map((comment, index) => ( -
- {comment.name} -

{comment.comment}

- {new Date(comment.timestamp).toLocaleString()} -
- )) - )} +

Comments ({loaderData.comments.length})

+
+ {loaderData.comments.length === 0 ? ( +
No comments yet!
+ ) : ( + loaderData.comments.map((comment, index) => ( +
+
+ {comment.name} +
+ + + +
+
+

{comment.comment}

+ {new Date(comment.timestamp).toLocaleString()}
+ )) + )} +
); } \ No newline at end of file