From 6039509006df7f5fa603ea0f58e2262a78cdc65f Mon Sep 17 00:00:00 2001 From: CoGomu Date: Sun, 15 Jun 2025 21:04:22 +0200 Subject: [PATCH] add button for deleting task delete task from db --- backend/src/main.ts | 16 ++++++++------ web/components/ui/Sidebar.vue | 41 ++++++++++++++++++++++++++++++++--- web/pages/index.vue | 24 ++++++++++++++++++-- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/backend/src/main.ts b/backend/src/main.ts index f28b17d..3356dbe 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -16,9 +16,9 @@ app.get('/', (req, res) => { }); app.get('/tasks', async(req, res) => { - const tasks = await db.select({ title: task.title }).from(task) - const titles = tasks.map(t => t.title) - res.send(titles); + const tasks = await db.select().from(task) + console.log(tasks) + res.send(tasks); }); app.get('/events', async(req, res) => { @@ -38,17 +38,19 @@ app.get('/user/:id', (req, res) => { }); -app.get('/task/:id', (req, res) => { +app.get('/task/:id', async(req, res) => { - const id = req.params['id']; + const id = parseInt(req.params['id']); if (id == null) { res.status(400).send({error: 'Needs an id'}); return; } - const task = {id: id, name: 'Homework'} //TODO - res.json(task); + const returnedTask = await db.select().from(task).where(eq(task.id, id)) + // + console.log(returnedTask) + res.json(returnedTask); }); app.get('/event/:id', (req, res) => { diff --git a/web/components/ui/Sidebar.vue b/web/components/ui/Sidebar.vue index dd4fad2..d8f7ee7 100644 --- a/web/components/ui/Sidebar.vue +++ b/web/components/ui/Sidebar.vue @@ -19,6 +19,7 @@ watch(currentTheme, () => { const emits = defineEmits<{ (e: 'createTask', name: string): void + (e: 'deleteTask', id: number): void }>() const dropDownItems = computed(() => [ @@ -73,17 +74,37 @@ const selectedDate = computed({ } }) +type Task = { + id: number + userid: string + title: string + description: string + done: number + estimated_time: string + due_date: string + created_at: string + updated_at: string +} + defineProps<{ - todos: string[] + todos: Task[] }>() -function addTodo(){ + +function addTodo() { const name = prompt("Todo name:") console.log(name) if (name !== null) { emits('createTask', name) } } +function deleteTodo(todo: Task) { + console.log(todo.id) + emits('deleteTask', todo.id) +} +function editTodo() { + +} @@ -98,7 +119,21 @@ function addTodo(){
Todos
- {{ todo }} + +
+ + {{ todo.title }} + +
+ + + + + + +
+
+
diff --git a/web/pages/index.vue b/web/pages/index.vue index 63bde31..5f1b1f1 100644 --- a/web/pages/index.vue +++ b/web/pages/index.vue @@ -19,7 +19,20 @@ onMounted(() => { events.value = eventsResponse.value?.map(Event.fromSerializable) ?? [] }) -const { data: tasks } = await useAsyncData( +type Task = { + id: number + userid: string + title: string + description: string + done: number + estimated_time: string + due_date: string + created_at: string + updated_at: string +} + + +const { data: tasks, refresh } = await useAsyncData( 'tasks', () => { return axios.get("/tasks").then(result => { @@ -43,13 +56,20 @@ async function postTask(name: string) { estimated_time: (new Date()).toISOString(), //TODO due_date: (new Date()).toISOString(), }) + await refresh() +} + +async function deleteTask(id: number) { + console.log('deleting Task') + await axios.delete(`/task/${id}`) + await refresh() }