From 471e86d223ace3287a9b2a6318f19f01e1d60864 Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Mon, 23 Jun 2025 13:04:18 +0200 Subject: [PATCH] feat(edit): enhance edit functionality with signals and pipes --- frontend/src/app/edit/edit.component.ts | 28 +++++++++++++++------ frontend/src/app/service/todo.service.ts | 18 ++++--------- frontend/src/app/table/table.component.html | 8 +++++- frontend/src/app/table/table.component.ts | 4 +-- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/edit/edit.component.ts b/frontend/src/app/edit/edit.component.ts index 3ef96c7..b3b656e 100644 --- a/frontend/src/app/edit/edit.component.ts +++ b/frontend/src/app/edit/edit.component.ts @@ -1,8 +1,8 @@ -import {Component, inject, input, InputSignal, OnInit} from '@angular/core'; +import {Component, computed, effect, inject, input, InputSignal, OnInit, Signal, WritableSignal} from '@angular/core'; import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms"; import {GetTask} from "../dto/models"; import {TodoService} from "../service/todo.service"; -import {assert} from "@angular/compiler-cli/linker"; +import {DatePipe} from "@angular/common"; @Component({ selector: 'app-edit', @@ -10,6 +10,7 @@ import {assert} from "@angular/compiler-cli/linker"; ReactiveFormsModule ], templateUrl: './edit.component.html', + standalone: true, styleUrl: './edit.component.css' }) export default class EditComponent implements OnInit{ @@ -17,15 +18,28 @@ export default class EditComponent implements OnInit{ private todoService: TodoService = inject(TodoService); public form!: FormGroup; public todo: InputSignal = input(null); + public kys = effect(() => { + console.log(this.todo()) + }); - ngOnInit() { - this.form = this.fb.group({ - title: [this.todo()?.title, Validators.required], - dueDate: [this.todo()?.dueDate, Validators.required], + constructor() { + effect(() => { + this.form = this.fb.group({ + title: [this.todo()?.title, Validators.required], + dueDate: [new DatePipe('en-US').transform(this.todo()?.dueDate, 'yyyy-MM-dd'), Validators.required], + }) }); } + ngOnInit() { + } + edit() { - this.todoService.edit(this.form.value, this.todo()?.id ?? 0) + const edited = { + ...this.todo(), + ...this.form.value, + } + + this.todoService.edit(edited, this.todo()?.id ?? 0).subscribe(() => this.todoService.todos.reload()) } } diff --git a/frontend/src/app/service/todo.service.ts b/frontend/src/app/service/todo.service.ts index 85a9fc9..3847975 100644 --- a/frontend/src/app/service/todo.service.ts +++ b/frontend/src/app/service/todo.service.ts @@ -23,29 +23,21 @@ export class TodoService { return this.http.get('http://localhost:2000/api/tasks'); } - public create(todo: {title: string, dueDate: Date}) { + public create(todo: { title: string, dueDate: Date }) { return this.http.post('http://localhost:2000/api/tasks', todo); } - public markAsDone(todo: GetTask) { - const putTask: PutTask = { - title: todo.title, - dueDate: todo.dueDate, - done: true, - } + public toggleDone(todo: GetTask) { + todo.done = !todo.done; - return this.http.put(`http://localhost:2000/api/tasks/${todo.id}`, putTask); + return this.edit(todo, todo.id); } public delete(id: number) { return this.http.delete(`http://localhost:2000/api/tasks/${id}`); } - edit(edited: { title: string; dueDate: Date }, id: number) { + edit(edited: GetTask, id: number) { return this.http.put(`http://localhost:2000/api/tasks/${id}`, edited); } - - getOne(id: number) { - return this.http.get(`http://localhost:2000/api/tasks/${id}`); - } } diff --git a/frontend/src/app/table/table.component.html b/frontend/src/app/table/table.component.html index 61ec60a..f1e8e15 100644 --- a/frontend/src/app/table/table.component.html +++ b/frontend/src/app/table/table.component.html @@ -17,7 +17,13 @@ {{ todo.dueDate | date }} - + diff --git a/frontend/src/app/table/table.component.ts b/frontend/src/app/table/table.component.ts index 68029a4..9a630e4 100644 --- a/frontend/src/app/table/table.component.ts +++ b/frontend/src/app/table/table.component.ts @@ -19,8 +19,8 @@ export class TableComponent { todos = this.todoService.todos; editingTodo: WritableSignal = signal(null); - markAsDone(todo: GetTask) { - this.todoService.markAsDone(todo).subscribe(() => this.todos.reload()); + toggleDone(todo: GetTask) { + this.todoService.toggleDone(todo).subscribe(() => this.todos.reload()); } delete(id: number) {