feat: add topics to tasks

This commit is contained in:
2025-01-08 17:20:16 +01:00
parent f2dfba27ee
commit d9349e08e0
4 changed files with 69 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
import { db, tasks } from "$lib/db/schema" import { db } from "$lib/db"
export const load = async () => { export const load = async () => {
return { return {
tasks: await db.select().from(tasks) tasks: await db.query.tasks.findMany()
} }
} }

View File

@@ -1,12 +1,26 @@
import { db } from "$lib/db" import { db } from "$lib/db"
import { tasksToTopics } from "$lib/db/schema"
export const load = async () => { export const load = async () => {
return { return {
task: (await db.query.tasks.findFirst({ task: (await db.query.tasks.findFirst({
with: { with: {
tasksToTopics: true tasksToTopics: {
with: {
topic: true
}
}
} }
}) })
) ),
topics: await db.query.topics.findMany()
}
}
export const actions = {
addTopic: async ({ request, params }) => {
const data = await request.formData()
const task = await db.insert(tasksToTopics).values({ taskId: params.id, topicId: data.get('topic-id') }).returning()
} }
} }

View File

@@ -1,14 +1,56 @@
<script lang="ts"> <script lang="ts">
let { data } = $props(); let { data } = $props();
let addTopicsDropdownShown = $state(false);
</script> </script>
<h1 class="my-4 text-2xl font-bold text-center">{data.task?.name}</h1> <h1 class="my-4 text-2xl font-bold text-center">{data.task?.name}</h1>
<h2>Topics ({data.task?.tasksToTopics.length})</h2> <div class="my-4">
<div class="flex justify-center gap-4 items-center relative">
<h2 class="text-xl font-semibold">topics ({data.task?.tasksToTopics.length})</h2>
<div class="relative">
<button
class="px-2 py-1 rounded bg-green-500 text-white font-medium shadow"
onclick={() => (addTopicsDropdownShown = !addTopicsDropdownShown)}
>
+
</button>
{#if addTopicsDropdownShown}
<form
method="post"
action="?/addTopic"
class="absolute mt-2 rounded-xl bg-gray-100 p-2 shadow-md items-center flex flex-col"
>
<select name="topic-id" class="rounded p-2 border border-gray-300 my-2" required>
<option value="" disabled selected>select a topic</option>
{#each data.topics as topic}
{#if !data.task?.tasksToTopics.some((t) => t.topicId === topic.id)}
<option value={topic.id}>{topic.name}</option>
{/if}
{/each}
</select>
<button
type="submit"
class="ml-2 px-2 py-1 rounded bg-blue-500 text-white font-medium shadow"
>
Add
</button>
</form>
{/if}
</div>
</div>
<div class="flex justify-center flex-wrap gap-2 my-1">
{#if data.task?.tasksToTopics} {#if data.task?.tasksToTopics}
{#each data.task?.tasksToTopics as topic} {#each data.task?.tasksToTopics as topic}
<div class="rounded px-4 py-2 bg-gray-300"> <div
{topic.topicId} class="bg-blue-500 text-white rounded-full px-2 py-1 shadow font-medium hover:bg-blue-600 transition cursor-pointer"
title="Click for more details about {topic.topic.name}"
>
{topic.topic.name}
</div> </div>
{/each} {/each}
{/if} {/if}
</div>
</div>

View File

@@ -1,4 +1,5 @@
import { db, tasks } from "$lib/db/schema" import { db } from "$lib/db"
import { tasks } from "$lib/db/schema"
import { redirect } from "@sveltejs/kit" import { redirect } from "@sveltejs/kit"
export const actions = { export const actions = {