homeman-deno/routes/index.tsx

57 lines
1.6 KiB
TypeScript

import { Handlers, PageProps } from '$fresh/server.ts'
import { Todo, UserWithTodos } from '@homeman/models.ts'
import { db, kv } from '@homeman/db.ts'
import Dashboard from '@homeman/islands/Dashboard.tsx'
interface Data {
users: Record<string, UserWithTodos>
todos: Record<string, Todo>
unassignedTodos: Todo[]
lastUserIdUpdated: { value: string; versionstamp: string }
lastTodoIdUpdated: { value: string; versionstamp: string }
}
export const handler: Handlers = {
async GET(req, ctx) {
const accept = req.headers.get('accept')
if (accept === 'text/event-stream') {
console.log('requested index event stream -- not implemented')
return new Response(JSON.stringify({ message: 'not implemented' }))
}
const users = Object.fromEntries(
(await db.users.findMany({ include: { assignedTodos: true } })).map(
(u) => [u.id, u],
),
)
const todos = Object.fromEntries(
(await db.todos.findMany({})).map(
(t) => [t.id, t],
),
)
const unassignedTodos = await db.todos.findMany({
where: { assigneeUserId: null },
})
const [lastUserIdUpdated, lastTodoIdUpdated] = await kv.getMany([[
'last_user_updated',
], ['last_todo_updated']], { consistency: 'eventual' })
console.log({ lastTodoIdUpdated, lastUserIdUpdated })
const props = {
users,
todos,
unassignedTodos,
lastUserIdUpdated,
lastTodoIdUpdated,
}
if (accept === 'application/json') {
return new Response(JSON.stringify(props))
}
return ctx.render(props)
},
}
export default function Home({ data }: PageProps<Data>) {
console.log('Home rendered')
return <Dashboard {...data} />
}