Unborked?

This commit is contained in:
Daniel Flanagan 2024-01-21 18:11:25 -06:00
parent 4275d51faf
commit 6529cb3810
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 21 additions and 10 deletions

View file

@ -55,7 +55,7 @@ export function Routine(
<main class='flex flex-col overflow-x-scroll'> <main class='flex flex-col overflow-x-scroll'>
<ul> <ul>
{taskGroups[currentPhase.value].map(( {taskGroups[currentPhase.value].map((
{ emoji, id, doneAt, index }: TaskWithIndex, { emoji, id, description, doneAt, index }: TaskWithIndex,
) => ( ) => (
<li <li
role='button' role='button'
@ -84,7 +84,7 @@ export function Routine(
}} }}
> >
{emoji ? `${emoji.trim()} ` : ''} {emoji ? `${emoji.trim()} ` : ''}
{id.trim()} {description.trim()}
</li> </li>
))} ))}
</ul> </ul>

View file

@ -57,6 +57,14 @@ export const handler: Handlers<Task | null> = {
return Response.redirect(url, 303) return Response.redirect(url, 303)
} }
}, },
async PUT(req, _ctx) {
if (req.headers.get('content-type')?.includes('json')) {
const result = await createOrUpdate(
TaskPayload.parse(await req.json()),
)
return new Response(JSON.stringify(result))
}
},
async DELETE(req, _ctx) { async DELETE(req, _ctx) {
// task: form or query params or json // task: form or query params or json
let data let data
@ -67,7 +75,7 @@ export const handler: Handlers<Task | null> = {
} }
console.log('delete task data:', data) console.log('delete task data:', data)
const taskData = TaskModel.pick({ id: true }).parse(data) const taskData = TaskModel.pick({ id: true }).parse(data)
const result = await db.tasks.delete({ where: taskData }) const result = await kv.delete(['task', taskData.id])
await kv.set(['last_task_updated'], taskData.id) await kv.set(['last_task_updated'], taskData.id)
return new Response(JSON.stringify(result)) return new Response(JSON.stringify(result))
}, },
@ -95,9 +103,7 @@ export const handler: Handlers<Task | null> = {
continue continue
} }
const task = await db.tasks.findFirst({ const task = (await kv.get(['task', entry.value])).value
where: { id: entry.value },
})
const chunk = `data: ${ const chunk = `data: ${
JSON.stringify({ JSON.stringify({
id: entry.value, id: entry.value,
@ -133,10 +139,12 @@ export const handler: Handlers<Task | null> = {
const taskData = TaskModel.pick({ id: true }).safeParse(data) const taskData = TaskModel.pick({ id: true }).safeParse(data)
if (taskData.success) { if (taskData.success) {
return new Response( return new Response(
JSON.stringify(await db.tasks.findFirst({ where: taskData.data })), JSON.stringify((await kv.get(['task', taskData.data.id])).value),
) )
} else { } else {
return new Response(JSON.stringify(await db.tasks.findMany({}))) return new Response(
JSON.stringify(Array.fromAsync(kv.list({ prefix: ['task'] }))),
)
} }
}, },
} }

View file

@ -1,7 +1,7 @@
import { Handlers, PageProps } from '$fresh/server.ts' import { Handlers, PageProps } from '$fresh/server.ts'
import { Task } from '@homeman/models.ts' import { Task } from '@homeman/models.ts'
import { Routine } from '@homeman/islands/Routine.tsx' import { Routine } from '@homeman/islands/Routine.tsx'
import { db } from '@homeman/db.ts' import { kv } from '@homeman/db.ts'
// import { db, kv, Todo, UserWithTodos } from '@homeman/models.ts' // import { db, kv, Todo, UserWithTodos } from '@homeman/models.ts'
interface Data { interface Data {
@ -10,7 +10,10 @@ interface Data {
export const handler: Handlers = { export const handler: Handlers = {
async GET(_req, ctx) { async GET(_req, ctx) {
const tasks = await db.tasks.findMany({}) const tasks = (await Array.fromAsync(kv.list({ prefix: ['task'] }))).map(
(r) => r.value
)
console.log({ tasks })
return ctx.render({ tasks }) return ctx.render({ tasks })
}, },
} }