185 lines
5.1 KiB
TypeScript
185 lines
5.1 KiB
TypeScript
import { Component, inject, signal, WritableSignal } from '@angular/core';
|
|
import { RouterOutlet } from '@angular/router';
|
|
import { TaskService } from '../../../services/task.service';
|
|
import { Task } from '../../../model/Task';
|
|
import { GroupService } from '../../../services/group.service';
|
|
import { Group } from '../../../model/Group';
|
|
import { ProjectService } from '../../../services/project.service';
|
|
import { Project } from '../../../model/Project';
|
|
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
|
import { Observable } from 'rxjs';
|
|
|
|
@Component({
|
|
imports: [RouterOutlet, ReactiveFormsModule],
|
|
selector: 'app-root',
|
|
styleUrl: './home.component.css',
|
|
templateUrl: './home.component.html',
|
|
})
|
|
export class Home {
|
|
//#region Attributs
|
|
private taskService: TaskService = inject(TaskService);
|
|
private groupService: GroupService = inject(GroupService);
|
|
private projectService: ProjectService = inject(ProjectService);
|
|
|
|
tasks: WritableSignal<Task[]> = signal<Task[]>([]);
|
|
groups: WritableSignal<Group[]> = signal<Group[]>([]);
|
|
projects: WritableSignal<Project[]> = signal<Project[]>([]);
|
|
|
|
newTaskForm = new FormGroup({
|
|
name: new FormControl('', Validators.required),
|
|
dueDate: new FormControl(''),
|
|
priority: new FormControl(''),
|
|
status: new FormControl(''),
|
|
});
|
|
|
|
showTaskForm: boolean = false;
|
|
|
|
newGroupForm = new FormGroup({
|
|
name: new FormControl('', Validators.required),
|
|
});
|
|
|
|
showGroupForm: boolean = false;
|
|
|
|
newProjectForm = new FormGroup({
|
|
name: new FormControl('', Validators.required),
|
|
});
|
|
|
|
showProjectForm: boolean = false;
|
|
|
|
//#endregion
|
|
|
|
public ngOnInit(): void {
|
|
this.taskService.getTasks().subscribe({
|
|
next: (response: Task[]) => {
|
|
this.tasks.set(response);
|
|
},
|
|
error: (error) => {
|
|
console.error(`Récupération des tâches impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
|
|
this.groupService.getGroups().subscribe({
|
|
next: (response: Group[]) => {
|
|
this.groups.set(response);
|
|
},
|
|
error: (error) => {
|
|
console.error(`Récupération des groupes impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
|
|
this.projectService.getProjects().subscribe({
|
|
next: (response: Project[]) => {
|
|
this.projects.set(response);
|
|
},
|
|
error: (error) => {
|
|
console.error(`Récupération des projets impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
public toggleTaskForm(): void {
|
|
this.showTaskForm = !this.showTaskForm;
|
|
}
|
|
|
|
/**
|
|
* submitTask
|
|
*/
|
|
public submitTask(): void {
|
|
let response: Observable<Task> = this.taskService.addTask(this.newTaskForm.controls.name.value!,
|
|
this.newTaskForm.controls.dueDate.value,
|
|
this.newTaskForm.controls.priority.value,
|
|
this.newTaskForm.controls.status.value);
|
|
|
|
response.subscribe({
|
|
next: (response) => {
|
|
this.tasks.update(values => {
|
|
return [...values, response];
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error(`L'ajout de tâche est impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* deleteTask
|
|
*/
|
|
public deleteTask(id_task: number) {
|
|
let response: Observable<void> = this.taskService.deleteTask(id_task);
|
|
|
|
response.subscribe({
|
|
next: () => {
|
|
this.tasks.update((tasks) => {
|
|
return tasks.filter(((task) => task.id_task != id_task));
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error(`La suppression de tâche est impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
public toggleGroupForm(): void {
|
|
this.showGroupForm = !this.showGroupForm;
|
|
}
|
|
|
|
/**
|
|
* submitGroup
|
|
*/
|
|
public submitGroup() {
|
|
let response: Observable<Group> = this.groupService.addGroup(this.newGroupForm.controls.name.value!);
|
|
|
|
response.subscribe({
|
|
next: (response) => {
|
|
this.groups.update(values => {
|
|
return [...values, response];
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error(`L'ajout d'un groupe est impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* deleteGroup
|
|
*/
|
|
public deleteGroup(id_group:number) {
|
|
let response:Observable<void> = this.groupService.deleteGroup(id_group);
|
|
|
|
response.subscribe({
|
|
next: () => {
|
|
this.groups.update((groups) => {
|
|
return groups.filter(((group) => group.id_group != id_group));
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error(`La suppression du groupe est impossible\nErreur: ${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
public toggleProjectForm(): void {
|
|
this.showProjectForm = !this.showProjectForm;
|
|
}
|
|
|
|
/**
|
|
* submitProject
|
|
*/
|
|
public submitProject() {
|
|
let response: Observable<Project> = this.projectService.addProject(this.newProjectForm.controls.name.value!);
|
|
|
|
response.subscribe({
|
|
next: (response) => {
|
|
this.projects.update(values => {
|
|
return [...values, response];
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error(`L'ajout d'un projet est impossible\nErreur:${error.error.message}`);
|
|
}
|
|
});
|
|
}
|
|
}
|