2022-09-27 15:49:41 -05:00
|
|
|
import { Handlers, PageProps } from "$fresh/server.ts";
|
2022-10-07 23:22:35 -05:00
|
|
|
import { listNotes } from "@/db/mod.ts";
|
2022-10-05 17:02:21 -05:00
|
|
|
import { Page } from "@/components/Page.tsx";
|
2022-10-07 23:22:35 -05:00
|
|
|
import { type Note } from "@/types.ts";
|
2022-09-27 15:49:41 -05:00
|
|
|
|
2022-09-30 15:14:57 -05:00
|
|
|
export const handler: Handlers<Note[]> = {
|
2022-10-05 17:02:21 -05:00
|
|
|
async GET(_request, context) {
|
2022-10-07 23:22:35 -05:00
|
|
|
const result = await listNotes();
|
|
|
|
if (!result) throw "unable to fetch from database";
|
|
|
|
return await context.render(result.rows);
|
2022-09-27 15:49:41 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-09-30 15:14:57 -05:00
|
|
|
export default function NotesPage({ data: notes }: PageProps<Note[]>) {
|
2022-09-27 15:49:41 -05:00
|
|
|
return (
|
2022-09-30 15:14:57 -05:00
|
|
|
<Page>
|
2022-09-29 20:22:30 -05:00
|
|
|
<h1>List of Notes</h1>
|
|
|
|
<p>Create a note:</p>
|
2022-09-30 15:14:57 -05:00
|
|
|
<form class="flex flex-col" action="./note/create" method="POST">
|
2022-10-01 14:03:15 -05:00
|
|
|
<textarea rows={6} class="" name="content">
|
2022-09-30 15:14:57 -05:00
|
|
|
</textarea>
|
2022-10-01 14:03:15 -05:00
|
|
|
<input class="mt-2" type="submit" value="Post" />
|
2022-09-29 20:22:30 -05:00
|
|
|
</form>
|
2022-10-07 23:22:35 -05:00
|
|
|
{notes.map(({ id, content, createdAt }) => (
|
2022-09-30 15:14:57 -05:00
|
|
|
<div class="my-4" key={id}>
|
|
|
|
<span>
|
|
|
|
<a href={`/note/${id}`} class="text-blue-500">Note {id}</a>{" "}
|
2022-10-07 23:22:35 -05:00
|
|
|
created at {createdAt.toLocaleString()}
|
2022-09-30 15:14:57 -05:00
|
|
|
</span>
|
|
|
|
<blockquote class="mt-2 pl-4 border-l(solid 4)">
|
|
|
|
<pre>{content}</pre>
|
|
|
|
</blockquote>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Page>
|
2022-09-27 15:49:41 -05:00
|
|
|
);
|
|
|
|
}
|