Files
sweet/examples/todo-app/pages/index.tsx
T
Luca Casonato 44a0d490fe feat: add support for CSP (#72)
This commit adds support for running with CSP. CSP can be enabled
on a per page basis. If this is done, fresh will set a default CSP that
is very restrictive. Users can modify the CSP with the `useCSP` hook.
Fresh will add its own script resources to the CSP automatically.
2021-11-01 12:58:13 +01:00

53 lines
1.2 KiB
TypeScript

/** @jsx h */
import { h, HandlerContext, PageConfig, Suspense, useData } from "../deps.ts";
import { getDatabase, Todo } from "../utils/database.ts";
export default function TodoListPage() {
return (
<div>
<h1>Your TODOs</h1>
<AddTodoForm />
<Suspense fallback={<p>Loading...</p>}>
<TodoList />
</Suspense>
</div>
);
}
function AddTodoForm() {
return (
<form method="POST">
<input type="text" name="text" />
<button type="submit" style="margin-left: 4px">Add</button>
</form>
);
}
function TodoList() {
const todos = useData<Todo[]>("todos", async () => {
const db = await getDatabase();
const todos = await db.listTodos();
return todos;
});
return (
<ul>
{todos.map((todo) => <li key={todo.id}>{todo.text}</li>)}
</ul>
);
}
export const handler = {
async POST(ctx: HandlerContext): Promise<Response> {
const form = await ctx.req.formData();
const text = form.get("text");
if (typeof text !== "string") {
return new Response("misformed form", { status: 400 });
}
const db = await getDatabase();
await db.addTodo(text);
return Response.redirect(ctx.req.url, 303);
},
};
export const config: PageConfig = { csp: true };