homeman-deno/routes/index.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-01-05 21:10:38 -06:00
import { Handlers, PageProps } from '$fresh/server.ts'
2024-01-17 15:10:28 -06:00
import { db, kv, Todo, UserWithTodos } from '@homeman/models.ts'
2024-01-11 16:23:09 -06:00
import Dashboard from '@homeman/islands/Dashboard.tsx'
2024-01-05 13:22:56 -06:00
interface Data {
2024-01-11 16:23:09 -06:00
users: Record<string, UserWithTodos>
2024-01-17 15:10:28 -06:00
todos: Record<string, Todo>
2024-01-05 21:10:38 -06:00
unassignedTodos: Todo[]
2024-01-17 15:10:28 -06:00
lastUserIdUpdated: { value: string; versionstamp: string }
lastTodoIdUpdated: { value: string; versionstamp: string }
2024-01-05 13:22:56 -06:00
}
export const handler: Handlers = {
async GET(_req, ctx) {
2024-01-11 16:23:09 -06:00
const users = Object.fromEntries(
(await db.users.findMany({ include: { assignedTodos: true } })).map(
(u) => [u.id, u],
),
)
2024-01-17 15:10:28 -06:00
const todos = Object.fromEntries(
(await db.todos.findMany({})).map(
(t) => [t.id, t],
),
)
2024-01-05 21:10:38 -06:00
const unassignedTodos = await db.todos.findMany({
where: { assigneeUserId: null },
})
2024-01-17 15:10:28 -06:00
const [lastUserIdUpdated, lastTodoIdUpdated] = await kv.getMany([[
'last_user_updated',
], ['last_todo_updated']], { consistency: 'eventual' })
console.log({ lastTodoIdUpdated, lastUserIdUpdated })
return ctx.render({
users,
todos,
unassignedTodos,
lastUserIdUpdated,
lastTodoIdUpdated,
})
2024-01-05 13:22:56 -06:00
},
}
2024-01-11 16:23:09 -06:00
export default function Home({ data }: PageProps<Data>) {
2024-01-16 16:15:10 -06:00
console.log('Home rendered')
2024-01-17 15:10:28 -06:00
return <Dashboard {...data} />
2024-01-05 13:22:56 -06:00
}