homeman-deno/routes/index.tsx

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-01-05 21:10:38 -06:00
import { Handlers, PageProps } from '$fresh/server.ts'
import { Todo, UserWithTodos } from '@homeman/models.ts'
import { db, kv } from '@homeman/db.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 = {
2024-01-21 09:25:17 -06:00
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' }))
}
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 })
2024-01-21 09:25:17 -06:00
const props = {
2024-01-17 15:10:28 -06:00
users,
todos,
unassignedTodos,
lastUserIdUpdated,
lastTodoIdUpdated,
2024-01-21 09:25:17 -06:00
}
if (accept === 'application/json') {
return new Response(JSON.stringify(props))
}
return ctx.render(props)
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
}