Public
Back
Version 200
2/22/2024
/** @jsxImportSource https://esm.sh/react */
import { zip } from "https://esm.sh/lodash-es";
import { useEffect, useState } from "https://esm.sh/react@18.2.0";
import { Form, hydrate } from "https://esm.town/v/stevekrouse/ssr_hydrate_react";
export async function loader(req: Request) {
const { sqlite } = await import("https://esm.town/v/std/sqlite?v=4");
sqlite.execute(`CREATE TABLE IF NOT EXISTS todos2 (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
text TEXT NOT NULL
)`);
let { columns, rows } = await sqlite.execute(`select * from todos2`);
const initialTodos = rows.map(row => Object.fromEntries(zip(columns, row)));
return { initialTodos, initialLogs: [`Server rendered at ${new Date().toISOString()}`] };
}
export async function action(req: Request) {
const { sqlite } = await import("https://esm.town/v/std/sqlite?v=4");
const formData = await req.formData();
if (req.method === "POST") {
const name = formData.get("text") as string;
await sqlite.execute({ sql: `insert into todos2(text) values (?)`, args: [name] });
}
else if (req.method === "PUT") {
const id = formData.get("id") as string;
const completed_at = formData.get("completed_at") ? new Date() : null;
await sqlite.execute({ sql: `update todos2 set completed_at = ? where id = ?`, args: [completed_at, id] });
}
else if (req.method === "DELETE") {
const id = formData.get("id") as string;
await sqlite.execute({ sql: `delete from todos2 where id = ?`, args: [id] });
}
return Response.json("OK");
stevekrouse-todoapp.web.val.run
Updated: February 29, 2024