ls-deno/routes/note/create.tsx

26 lines
872 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 02:53:13 -05:00
import { type ContextState, type Note } from "@/types.ts";
2022-09-30 15:14:57 -05:00
2022-10-08 02:53:13 -05:00
export const handler: Handlers<Note, ContextState> = {
2022-09-30 15:14:57 -05:00
async POST(request, context) {
2022-10-08 02:53:13 -05:00
const userId = context.state.user ? context.state.user.id : null;
2022-09-30 15:14:57 -05:00
const content = (await request.formData()).get("content");
if (!content) throw "no content provided";
2022-10-08 02:53:13 -05:00
const note = await createNote({ userId, content: content.toString() });
2022-10-08 00:24:03 -05:00
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>
);
}