ls-deno/routes/note/create.tsx

25 lines
761 B
TypeScript
Raw Normal View History

2022-09-30 15:14:57 -05:00
import { Handlers, PageProps } from "$fresh/server.ts";
2022-10-08 00:24:03 -05:00
import { createNote } from "@/db/mod.ts";
2022-10-01 14:03:15 -05:00
import { Page } from "@/components/Page.tsx";
2022-10-08 00:24:03 -05:00
import { type Note } from "@/types.ts";
2022-09-30 15:14:57 -05:00
2022-10-08 00:24:03 -05:00
export const handler: Handlers<Note> = {
2022-09-30 15:14:57 -05:00
async POST(request, context) {
const content = (await request.formData()).get("content");
if (!content) throw "no content provided";
2022-10-08 00:24:03 -05:00
const note = await createNote({ content: content.toString() });
if (!note) throw "no note created";
return await context.render(note);
2022-09-30 15:14:57 -05:00
},
};
2022-10-08 00:24:03 -05:00
export default function NotesPage({ data: { id } }: PageProps<Note>) {
2022-09-30 15:14:57 -05:00
return (
<Page>
<h1>You created a note!</h1>
<a href="/note">Back to notes</a>
2022-10-08 00:24:03 -05:00
<a href={`/note/${id}`}>View your note</a>
2022-09-30 15:14:57 -05:00
</Page>
);
}