Using dependency injection inside new-task.component.
All checks were successful
Deploying Website / build (push) Successful in 1m3s

This commit is contained in:
Maxime Delporte 2025-04-22 07:14:12 -03:00
parent b3d2a49ec9
commit 73de5fedfe
3 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import {Component, EventEmitter, Output} from '@angular/core'; import {Component, EventEmitter, inject, Input, Output} from '@angular/core';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {NewTaskData} from '../task/task.model'; import {NewTaskData} from '../task/task.model';
import {TasksService} from '../tasks.service';
@Component({ @Component({
selector: 'app-new-task', selector: 'app-new-task',
@ -11,22 +12,28 @@ import {NewTaskData} from '../task/task.model';
styleUrl: './new-task.component.css' styleUrl: './new-task.component.css'
}) })
export class NewTaskComponent { export class NewTaskComponent {
@Output() cancel = new EventEmitter<void>(); @Input({required: true}) userId!: string;
@Output() add = new EventEmitter<NewTaskData>(); @Output() close = new EventEmitter<void>();
enteredTitle = ''; enteredTitle = '';
enteredSummary = ''; enteredSummary = '';
enteredDate = ''; enteredDate = '';
tasksService = inject(TasksService);
onCancel() { onCancel() {
this.cancel.emit(); this.close.emit();
} }
onSubmit() { onSubmit() {
let taskContent: NewTaskData = { let taskData: NewTaskData = {
title: this.enteredTitle, title: this.enteredTitle,
summary: this.enteredSummary, summary: this.enteredSummary,
date: this.enteredDate, date: this.enteredDate,
}; };
this.add.emit(taskContent);
this.tasksService.addTaskFor(this.userId, taskData);
this.close.emit()
} }
} }

View File

@ -1,7 +1,7 @@
@if (isAddingTask) { @if (isAddingTask) {
<app-new-task <app-new-task
(cancel)="onCancelAddTask()" [userId]="user.id"
(add)="onAddTask($event)" (close)="onCloseAddTask()"
/> />
} }

View File

@ -29,12 +29,7 @@ export class TasksComponent {
this.isAddingTask = true; this.isAddingTask = true;
} }
onCancelAddTask() { onCloseAddTask() {
this.isAddingTask = false; this.isAddingTask = false;
} }
onAddTask(taskData: NewTaskData) {
this.tasksService.addTaskFor(this.user.id, taskData);
this.onCancelAddTask()
}
} }