diff --git a/src/app/tasks/new-task/new-task.component.ts b/src/app/tasks/new-task/new-task.component.ts index 10bb5f1..cce685c 100644 --- a/src/app/tasks/new-task/new-task.component.ts +++ b/src/app/tasks/new-task/new-task.component.ts @@ -1,6 +1,6 @@ import {Component, EventEmitter, Output} from '@angular/core'; import {FormsModule} from '@angular/forms'; -import {NewTaskData} from '../../task/task.model'; +import {NewTaskData} from '../task/task.model'; @Component({ selector: 'app-new-task', diff --git a/src/app/task/task.component.css b/src/app/tasks/task/task.component.css similarity index 100% rename from src/app/task/task.component.css rename to src/app/tasks/task/task.component.css diff --git a/src/app/task/task.component.html b/src/app/tasks/task/task.component.html similarity index 100% rename from src/app/task/task.component.html rename to src/app/tasks/task/task.component.html diff --git a/src/app/task/task.component.ts b/src/app/tasks/task/task.component.ts similarity index 82% rename from src/app/task/task.component.ts rename to src/app/tasks/task/task.component.ts index a666861..a207e10 100644 --- a/src/app/task/task.component.ts +++ b/src/app/tasks/task/task.component.ts @@ -1,6 +1,6 @@ import {Component, EventEmitter, Input, Output} from '@angular/core'; -import { Task } from './task.model'; -import {CardComponent} from '../shared/card/card.component'; +import {Task} from './task.model'; +import {CardComponent} from '../../shared/card/card.component'; import {DatePipe} from '@angular/common'; @Component({ diff --git a/src/app/task/task.model.ts b/src/app/tasks/task/task.model.ts similarity index 100% rename from src/app/task/task.model.ts rename to src/app/tasks/task/task.model.ts diff --git a/src/app/tasks/tasks.component.ts b/src/app/tasks/tasks.component.ts index 4ed849f..721acef 100644 --- a/src/app/tasks/tasks.component.ts +++ b/src/app/tasks/tasks.component.ts @@ -1,8 +1,9 @@ -import {Component, EventEmitter, Input, Output} from '@angular/core'; -import { type User} from '../user/user.model'; -import { type NewTaskData, type Task } from '../task/task.model'; -import {TaskComponent} from '../task/task.component'; +import {Component, Input} from '@angular/core'; +import {type User} from '../user/user.model'; +import {type NewTaskData, type Task} from './task/task.model'; +import {TaskComponent} from './task/task.component'; import {NewTaskComponent} from './new-task/new-task.component'; +import {TasksService} from './tasks.service'; @Component({ selector: 'app-tasks', @@ -12,39 +13,16 @@ import {NewTaskComponent} from './new-task/new-task.component'; }) export class TasksComponent { @Input({ required: true }) user!: User; - isAddingTask = false; - tasks: Task[] = [ - { - id: 't1', - userId: 'u1', - title: 'Master Angular', - summary: 'Learn all the basic and advanced features of Angular & how to apply them.', - dueDate: '2025-12-31', - }, - { - id: 't2', - userId: 'u3', - title: 'Build first prototype', - summary: 'Build a first prototype of the online shop website', - dueDate: '2024-05-31', - }, - { - id: 't3', - userId: 'u3', - title: 'Prepare issue template', - summary: 'Prepare and describe an issue template which will help with project management', - dueDate: '2024-06-15', - }, - ]; + constructor(private tasksService: TasksService) {} get selectedUserTasks() { - return this.tasks.filter((task) => task.userId === this.user.id); + return this.tasksService.getUserTasks(this.user.id); } onCompleteTask(taskId: string) { - this.tasks = this.tasks.filter((task) => task.id !== taskId); + this.tasksService.removeTask(taskId); } onStartAddTask() { @@ -56,17 +34,7 @@ export class TasksComponent { } onAddTask(taskData: NewTaskData) { - let nextTaskId = this.tasks.length + 1; - let taskId = 't' + (nextTaskId).toString(); - - this.tasks.push({ - id: taskId, - userId: this.user.id, - title: taskData.title, - summary: taskData.summary, - dueDate: taskData.date, - }); - + this.tasksService.addTaskFor(this.user.id, taskData); this.onCancelAddTask() } } diff --git a/src/app/tasks/tasks.service.ts b/src/app/tasks/tasks.service.ts new file mode 100644 index 0000000..3bf10b2 --- /dev/null +++ b/src/app/tasks/tasks.service.ts @@ -0,0 +1,28 @@ +import type {NewTaskData, Task} from './task/task.model'; +import {Injectable} from '@angular/core'; + +@Injectable({providedIn: 'root'}) +export class TasksService { + private tasks: Task[] = []; + + getUserTasks(userId: string) { + return this.tasks.filter((task) => task.userId === userId); + } + + addTaskFor(userId: string, taskData: NewTaskData) { + let nextTaskId = this.tasks.length + 1; + let taskId = 't' + (nextTaskId).toString(); + + this.tasks.push({ + id: taskId, + userId: userId, + title: taskData.title, + summary: taskData.summary, + dueDate: taskData.date, + }); + } + + removeTask(taskId: string) { + this.tasks = this.tasks.filter((task) => task.id !== taskId); + } +}