From ecf7389f29e33c0473f1195749b38126fd85c4ba Mon Sep 17 00:00:00 2001 From: Tomasz Date: Mon, 26 Jun 2023 15:19:41 +0200 Subject: [PATCH] changes to edit just single task and refactor --- src/app/Task.ts | 10 ++++---- .../components/add-task/add-task.component.ts | 2 ++ .../edit-task-form.component.html | 2 +- .../edit-task-form.component.ts | 14 ++++++++--- .../task-item/task-item.component.html | 4 ++-- .../task-item/task-item.component.ts | 20 ++++++---------- src/app/components/tasks/tasks.component.ts | 5 ++-- src/app/services/ui.service.ts | 23 +++++++++++++++---- 8 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/app/Task.ts b/src/app/Task.ts index aef922f..919553a 100644 --- a/src/app/Task.ts +++ b/src/app/Task.ts @@ -1,6 +1,6 @@ export interface Task { - id?: number; - text: string; - day:string; - reminder:boolean; -} \ No newline at end of file + id: number; + text: string; + day: string; + reminder: boolean; +} diff --git a/src/app/components/add-task/add-task.component.ts b/src/app/components/add-task/add-task.component.ts index 021c4cd..4afa367 100644 --- a/src/app/components/add-task/add-task.component.ts +++ b/src/app/components/add-task/add-task.component.ts @@ -10,6 +10,7 @@ import { Subscription } from 'rxjs'; }) export class AddTaskComponent implements OnInit { @Output() onAddTask: EventEmitter = new EventEmitter(); + id: number; text: string; day: string; reminder: boolean = false; @@ -29,6 +30,7 @@ export class AddTaskComponent implements OnInit { return; } const newTask = { + id: this.id++, text: this.text, day: this.day, reminder: this.reminder, diff --git a/src/app/components/edit-task-form/edit-task-form.component.html b/src/app/components/edit-task-form/edit-task-form.component.html index e757798..f2501c0 100644 --- a/src/app/components/edit-task-form/edit-task-form.component.html +++ b/src/app/components/edit-task-form/edit-task-form.component.html @@ -1,4 +1,4 @@ -
+
diff --git a/src/app/components/edit-task-form/edit-task-form.component.ts b/src/app/components/edit-task-form/edit-task-form.component.ts index 2f348ed..8437677 100644 --- a/src/app/components/edit-task-form/edit-task-form.component.ts +++ b/src/app/components/edit-task-form/edit-task-form.component.ts @@ -15,23 +15,31 @@ export class EditTaskFormComponent implements OnInit { day: string; reminder: boolean; showEditTask: boolean; + selectedTask: Task | null; subscriptionEdit: Subscription; + constructor(private uiService: UiService) { - this.subscriptionEdit = this.uiService - .onToggleEdit() - .subscribe((value) => (this.showEditTask = value)); + this.subscriptionEdit = this.uiService.onToggleEdit$.subscribe( + (selectedTask) => { + this.selectedTask = selectedTask; + this.showEditTask = this.selectedTask !== null; + } + ); } + ngOnInit(): void {} onSubmit() { if (!this.text) { return; } const editedTask = { + ...this.task, text: (this.task.text = this.text), day: this.day, reminder: this.reminder, }; this.onEditTask.emit(editedTask); this.text = ''; + this.showEditTask = false; } } diff --git a/src/app/components/task-item/task-item.component.html b/src/app/components/task-item/task-item.component.html index b1bd583..1aaa33c 100644 --- a/src/app/components/task-item/task-item.component.html +++ b/src/app/components/task-item/task-item.component.html @@ -1,7 +1,7 @@

{{task.text}} -
@@ -14,5 +14,5 @@

{{task.day}}

- +

\ No newline at end of file diff --git a/src/app/components/task-item/task-item.component.ts b/src/app/components/task-item/task-item.component.ts index eb468c7..187d1e8 100644 --- a/src/app/components/task-item/task-item.component.ts +++ b/src/app/components/task-item/task-item.component.ts @@ -2,9 +2,7 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { Task } from 'src/app/Task'; import { faTimes } from '@fortawesome/free-solid-svg-icons'; import { faPenToSquare } from '@fortawesome/free-solid-svg-icons'; -import { NgModel } from '@angular/forms'; import { UiService } from 'src/app/services/ui.service'; -import { Router } from '@angular/router'; import { Subscription } from 'rxjs'; @Component({ selector: 'app-task-item', @@ -22,21 +20,17 @@ export class TaskItemComponent implements OnInit { faPenToSquare = faPenToSquare; showEditTask: boolean; subscription: Subscription; - constructor(private uiService: UiService, private router: Router) { - this.subscription = this.uiService - .onToggleEdit() - .subscribe((value) => (this.showEditTask = value)); //* value to jest przeciwieństwo showEditTask które ustawia się w toggleAddTask w ui.service. Tu sie tez subskrybuje observabla z ui.service - } //! tu też łapiemy value + constructor(private uiService: UiService) {} ngOnInit(): void {} - //to definicja metody używanej w komponencie task-item - onDelete(task: any) { - this.onDeleteTask.emit(task); //to leci do tasks.component.html + + onDelete(task: Task) { + this.onDeleteTask.emit(task); } - onToggle(task: any) { + onToggle(task: Task) { this.onToggleReminder.emit(task); //to leci do tasks.component.html -> trzeba isc do komponentu rodzic "tasks" i tak samo jak onDeleteTask dodajemy onToggleReminder tam } - onToggleEdit(task: any) { - this.uiService.toggleEditTask(); //*wywołujemy metodę napisaną w ui.service.ts + onToggleEditForm(task: Task) { + this.uiService.toggleEdit(task); } } diff --git a/src/app/components/tasks/tasks.component.ts b/src/app/components/tasks/tasks.component.ts index ec53ced..22094a8 100644 --- a/src/app/components/tasks/tasks.component.ts +++ b/src/app/components/tasks/tasks.component.ts @@ -18,13 +18,12 @@ export class TasksComponent implements OnInit { //osobny obiekt, nie array więc bez [] this.taskService.deleteTask(task).subscribe( //to jest jak then - () => (this.tasks = this.tasks.filter((t) => t.id !== task.id)) // !filtruje taski które NIE są o id które usuwamy + () => (this.tasks = this.tasks.filter((t) => t.id !== task.id)) ); } toggleReminder(task: Task) { - //!tutaj to jest to co po '=' w task.component.html (onToggleReminder)="toggleReminder(task)" task.reminder = !task.reminder; - this.taskService.updateTaskReminder(task).subscribe(); //!wykonanie metody z service która obsługuje aktualizowanie w bazie rekordów + this.taskService.updateTaskReminder(task).subscribe(); } addTask(task: Task) { this.taskService.addTask(task).subscribe((task) => this.tasks.push(task)); diff --git a/src/app/services/ui.service.ts b/src/app/services/ui.service.ts index 70431fc..d9cd8c7 100644 --- a/src/app/services/ui.service.ts +++ b/src/app/services/ui.service.ts @@ -1,13 +1,16 @@ import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs'; +import { Task } from 'src/app/Task'; @Injectable({ providedIn: 'root', }) export class UiService { private showAddTask: boolean = false; //! ustawienie domyślne, oddziałuje na zmienną prywatną o tej samej nazwie w header.component private subjectAdd = new Subject(); - private subjectEdit = new Subject(); + private subjectEdit = new Subject(); private showEditTask: boolean = false; + private selectedTask: Task | null; + onToggleEdit$ = this.subjectEdit.asObservable(); constructor() {} @@ -18,15 +21,27 @@ export class UiService { } toggleEditTask(): void { this.showEditTask = !this.showEditTask; - this.subjectEdit.next(this.showEditTask); + this.subjectEdit.next(this.selectedTask); } + toggleEdit(task: Task): void { + if (this.selectedTask === task) { + this.selectedTask = null; + } else { + this.selectedTask = task; + } + this.subjectEdit.next(this.selectedTask); + } onToggleAdd(): Observable { //! żeby zrobić coś kiedy się uruchomi toggleAddTask po kliknieciu Add, trzeba zasubskrybować onToggle -> to się dzieje w header.component return this.subjectAdd.asObservable(); } - onToggleEdit(): Observable { - //! żeby zrobić coś kiedy się uruchomi toggleAddTask po kliknieciu Add, trzeba zasubskrybować onToggle -> to się dzieje w header.component + onToggleEdit(task: Task): Observable { + if (this.selectedTask === task) { + this.showEditTask = true; + } else { + this.showEditTask = false; + } return this.subjectEdit.asObservable(); } }