From 78bd64f100d0aee5dce9bbc1f5f71dbb62dca956 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 11 Jan 2024 18:57:29 -0600 Subject: [PATCH] Done works --- fresh.gen.ts | 2 ++ islands/Dashboard.tsx | 29 +++++++++++++++++++---------- islands/TodoList.tsx | 28 ++++++++++++++++++++++------ routes/api/todo.ts | 15 +++++++++++---- routes/api/todo/done.ts | 14 ++++++++++++++ routes/index.tsx | 2 +- 6 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 routes/api/todo/done.ts diff --git a/fresh.gen.ts b/fresh.gen.ts index bc618a5..02ed8ab 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -7,6 +7,7 @@ import * as $_app from './routes/_app.tsx' import * as $admin from './routes/admin.tsx' import * as $api_joke from './routes/api/joke.ts' import * as $api_todo from './routes/api/todo.ts' +import * as $api_todo_done from './routes/api/todo/done.ts' import * as $api_user from './routes/api/user.ts' import * as $greet_name_ from './routes/greet/[name].tsx' import * as $index from './routes/index.tsx' @@ -24,6 +25,7 @@ const manifest = { './routes/admin.tsx': $admin, './routes/api/joke.ts': $api_joke, './routes/api/todo.ts': $api_todo, + './routes/api/todo/done.ts': $api_todo_done, './routes/api/user.ts': $api_user, './routes/greet/[name].tsx': $greet_name_, './routes/index.tsx': $index, diff --git a/islands/Dashboard.tsx b/islands/Dashboard.tsx index eb0dd49..64e5001 100644 --- a/islands/Dashboard.tsx +++ b/islands/Dashboard.tsx @@ -21,12 +21,13 @@ const unassignedUserPlaceholder: User = { color: '888888', } -interface UserSelectButtonProps extends JSX.HTMLAttributes { +interface UserSelectButtonProps extends JSX.HTMLAttributes { user: User } function UserSelectButton( - { user: { id, avatarUrl, color }, ...props }: UserSelectButtonProps, + { user: { id, name, avatarUrl, color }, tabindex, ...props }: + UserSelectButtonProps, ) { const eid = `assigneeUserId_${id}` return ( @@ -34,17 +35,18 @@ function UserSelectButton( @@ -91,13 +93,20 @@ export default function Dashboard({ users, unassignedTodos }: Props) { Assignee
  • - +
  • {Object.values(users).map( - (user, i) => { + (user) => { return (
  • - +
  • ) }, diff --git a/islands/TodoList.tsx b/islands/TodoList.tsx index 8a1f448..875e92f 100644 --- a/islands/TodoList.tsx +++ b/islands/TodoList.tsx @@ -13,19 +13,35 @@ export function TodoList( Props, ) { const todoItem = ( - { className, description, hideDone }: Pick & { - className?: string - hideDone?: boolean - }, + { id, doneAt, className, description, hideDone }: + & Pick + & { + className?: string + hideDone?: boolean + }, ) => (
  • + {JSON.stringify(doneAt)} {description} - {hideDone ? '' : } + {(hideDone || doneAt != null) ? '' : ( + + )}
  • ) return ( diff --git a/routes/api/todo.ts b/routes/api/todo.ts index 1991ff8..c8e52ef 100644 --- a/routes/api/todo.ts +++ b/routes/api/todo.ts @@ -8,7 +8,11 @@ type TodoPayload = z.infer async function createOrUpdate(todo: TodoPayload) { if (!todo.id) { - const newTodo: Todo = { ...todo, id: ulid(), createdAt: new Date() } + const newTodo: Todo = { + ...todo, + id: ulid(), + createdAt: new Date(), + } return await db.todos.create({ data: newTodo }) } else { return await db.todos.update({ where: { id: todo.id }, data: todo }) @@ -18,7 +22,9 @@ async function createOrUpdate(todo: TodoPayload) { export const handler: Handlers = { async POST(req, _ctx) { if (req.headers.get('content-type')?.includes('json')) { - const result = await createOrUpdate(TodoPayload.parse(await req.json())) + const result = await createOrUpdate( + TodoPayload.parse(await req.json()), + ) return new Response(JSON.stringify(result)) } else { const form = await req.formData() @@ -29,7 +35,8 @@ export const handler: Handlers = { const todo = TodoPayload.parse({ id: id, - emoji: form.get('emoji')?.toString(), + emoji: form.get('emoji')?.toString() || null, + doneAt: form.get('doneAt')?.toString() || null, description: form.get('description')?.toString(), assigneeUserId: form.get('assigneeUserId')?.toString(), }) @@ -41,7 +48,7 @@ export const handler: Handlers = { await createOrUpdate(todo) const url = new URL(req.url) - url.pathname = '/admin' + url.pathname = '/' return Response.redirect(url, 303) } }, diff --git a/routes/api/todo/done.ts b/routes/api/todo/done.ts new file mode 100644 index 0000000..f0c200e --- /dev/null +++ b/routes/api/todo/done.ts @@ -0,0 +1,14 @@ +import { Handlers } from '$fresh/server.ts' +import { db, TodoModel } from '@homeman/models.ts' + +const Model = TodoModel.pick({ id: true }) + +export const handler: Handlers = { + async POST(req, _ctx) { + const { id } = Model.parse(await req.json()) + const todo = await db.todos.findFirst({ where: { id } }) + todo.doneAt = new Date() + const newTodo = await db.todos.update({ where: { id }, data: todo }) + return new Response(JSON.stringify(newTodo)) + }, +} diff --git a/routes/index.tsx b/routes/index.tsx index 13c1710..c69798b 100644 --- a/routes/index.tsx +++ b/routes/index.tsx @@ -1,5 +1,5 @@ import { Handlers, PageProps } from '$fresh/server.ts' -import { db, Todo, User, UserWithTodos } from '@homeman/models.ts' +import { db, Todo, UserWithTodos } from '@homeman/models.ts' import Dashboard from '@homeman/islands/Dashboard.tsx' interface Data {