import { Handlers, PageProps } from "$fresh/server.ts";
import { createNote } from "@/db/mod.ts";
import { Page } from "@/components/Page.tsx";
import { type Note } from "@/types.ts";

export const handler: Handlers<Note> = {
  async POST(request, context) {
    const content = (await request.formData()).get("content");
    if (!content) throw "no content provided";
    const note = await createNote({ content: content.toString() });
    if (!note) throw "no note created";
    return await context.render(note);
  },
};

export default function NotesPage({ data: { id } }: PageProps<Note>) {
  return (
    <Page>
      <h1>You created a note!</h1>
      <a href="/note">Back to notes</a>
      <a href={`/note/${id}`}>View your note</a>
    </Page>
  );
}
