update task service & board

This commit is contained in:
Chneemann 2024-03-30 08:10:02 +01:00
parent 459cd43ce5
commit 66e72a7023
7 changed files with 76 additions and 67 deletions

View file

@ -32,7 +32,7 @@
><img src="./../../../assets/img/board/add.svg" alt="add" /> ><img src="./../../../assets/img/board/add.svg" alt="add" />
</div> </div>
<div id="todo" class="details"> <div id="todo" class="details">
@for(task of todoTasks; track task) { @for(task of getTaskStatus("todo"); track task) {
<app-task [task]="task"></app-task> <app-task [task]="task"></app-task>
} @empty { } @empty {
<app-task-empty>No tasks</app-task-empty> <app-task-empty>No tasks</app-task-empty>
@ -49,7 +49,7 @@
><img src="./../../../assets/img/board/add.svg" alt="add" /> ><img src="./../../../assets/img/board/add.svg" alt="add" />
</div> </div>
<div id="inprogress" class="details"> <div id="inprogress" class="details">
@for(task of inprogressTasks; track task) { @for(task of getTaskStatus("inprogress"); track task) {
<app-task [task]="task"></app-task> <app-task [task]="task"></app-task>
} @empty { } @empty {
<app-task-empty>No tasks</app-task-empty> <app-task-empty>No tasks</app-task-empty>
@ -66,7 +66,7 @@
><img src="./../../../assets/img/board/add.svg" alt="add" /> ><img src="./../../../assets/img/board/add.svg" alt="add" />
</div> </div>
<div id="awaitfeedback" class="details"> <div id="awaitfeedback" class="details">
@for(task of awaitfeedbackTasks; track task) { @for(task of getTaskStatus("awaitfeedback"); track task) {
<app-task [task]="task"></app-task> <app-task [task]="task"></app-task>
} @empty { } @empty {
<app-task-empty>No tasks</app-task-empty> <app-task-empty>No tasks</app-task-empty>
@ -83,7 +83,7 @@
><img src="./../../../assets/img/board/add.svg" alt="add" /> ><img src="./../../../assets/img/board/add.svg" alt="add" />
</div> </div>
<div id="done" class="details"> <div id="done" class="details">
@for(task of doneTasks; track task) { @for(task of getTaskStatus("done"); track task) {
<app-task [task]="task"></app-task> <app-task [task]="task"></app-task>
} @empty { } @empty {
<app-task-empty>No tasks</app-task-empty> <app-task-empty>No tasks</app-task-empty>

View file

@ -15,19 +15,12 @@ import { TaskEmptyComponent } from './task/task-empty/task-empty.component';
}) })
export class BoardComponent { export class BoardComponent {
@ViewChild('searchField') searchField!: ElementRef; @ViewChild('searchField') searchField!: ElementRef;
todoTasks: Task[] = [];
inprogressTasks: Task[] = [];
awaitfeedbackTasks: Task[] = [];
doneTasks: Task[] = [];
constructor( constructor(
public dragDropService: DragDropService, public dragDropService: DragDropService,
private taskService: TaskService private taskService: TaskService
) { ) {}
this.taskService.subTaskList().subscribe(() => {
this.loadAllTasks(); search!: string;
});
}
ngOnInit() { ngOnInit() {
this.dragDropService.itemDropped.subscribe(({ id, status }) => { this.dragDropService.itemDropped.subscribe(({ id, status }) => {
@ -35,22 +28,20 @@ export class BoardComponent {
}); });
} }
getTask(status: string): Task[] { getTaskStatus(status: string) {
const search = this.searchField.nativeElement.value.toLowerCase(); if (this.searchField) {
if (this.taskService.filteredTasks.length >= 0 && search !== '') { this.search = this.searchField.nativeElement.value.toLowerCase();
return this.taskService.filteredTasks.filter( if (this.search.length > 0) {
(task) => task.status === status return this.taskService
); .getFiltertTasks()
} else { .filter((task) => task.status === status);
return this.taskService.allTasks.filter((task) => task.status === status); } else {
return this.taskService
.getAllTasks()
.filter((task) => task.status === status);
}
} }
} return;
loadAllTasks() {
this.todoTasks = this.getTask('todo');
this.inprogressTasks = this.getTask('inprogress');
this.awaitfeedbackTasks = this.getTask('awaitfeedback');
this.doneTasks = this.getTask('done');
} }
handleItemDropped(id: string, status: string): void { handleItemDropped(id: string, status: string): void {
@ -68,13 +59,13 @@ export class BoardComponent {
} }
searchTask(): void { searchTask(): void {
const search = this.searchField.nativeElement.value.toLowerCase(); this.taskService.filteredTasks = this.taskService
this.taskService.filteredTasks = this.taskService.allTasks.filter( .getAllTasks()
(task) => .filter(
task.title.toLowerCase().includes(search) || (task) =>
task.description.toLowerCase().includes(search) || task.title.toLowerCase().includes(this.search) ||
task.category.toLowerCase().includes(search) task.description.toLowerCase().includes(this.search) ||
); task.category.toLowerCase().includes(this.search)
this.loadAllTasks(); );
} }
} }

View file

@ -46,7 +46,7 @@ export class TaskComponent {
userBadged(id: number) { userBadged(id: number) {
const userId = String(id); const userId = String(id);
const user = this.userService.allUsers.find((user) => user.id === userId); const user = this.userService.getUsers().find((user) => user.id === userId);
if (user) { if (user) {
if (user.firstName === 'Guest') { if (user.firstName === 'Guest') {
return user.firstName.charAt(0); return user.firstName.charAt(0);

View file

@ -43,8 +43,7 @@ export class ContactsComponent {
} }
checkUsersFirstLetter() { checkUsersFirstLetter() {
let usersFirstLetter = []; let usersFirstLetter = Array.from(
usersFirstLetter = Array.from(
new Set( new Set(
this.loadAllUserWithoutGuest().map((user) => this.loadAllUserWithoutGuest().map((user) =>
user.firstName[0].toUpperCase() user.firstName[0].toUpperCase()

View file

@ -19,11 +19,9 @@ export class SummaryComponent {
urgentTasksDate: String = ''; urgentTasksDate: String = '';
constructor(private taskService: TaskService) { constructor(private taskService: TaskService) {
this.taskService.subTaskList().subscribe(() => { this.updateTasksCount();
this.updateTasksCount(); this.updateAllTasksCount();
this.updateAllTasksCount(); this.updateUrgentTasksCount();
this.updateUrgentTasksCount();
});
} }
updateAllTasksCount() { updateAllTasksCount() {

View file

@ -1,4 +1,4 @@
import { EventEmitter, Injectable, inject } from '@angular/core'; import { EventEmitter, Injectable, OnDestroy, inject } from '@angular/core';
import { import {
Firestore, Firestore,
collection, collection,
@ -7,36 +7,53 @@ import {
updateDoc, updateDoc,
} from '@angular/fire/firestore'; } from '@angular/fire/firestore';
import { Task } from '../interfaces/task.interface'; import { Task } from '../interfaces/task.interface';
import { Observable } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class TaskService { export class TaskService implements OnDestroy {
firestore: Firestore = inject(Firestore); firestore: Firestore = inject(Firestore);
allTasks: Task[] = []; allTasks: Task[] = [];
filteredTasks: Task[] = []; filteredTasks: Task[] = [];
constructor() {} unsubTask;
constructor() {
this.unsubTask = this.subTaskList();
}
subTaskList() { subTaskList() {
return new Observable<void>((observer) => { return onSnapshot(collection(this.firestore, 'tasks'), (list) => {
const unsubscribe = onSnapshot( this.allTasks = [];
collection(this.firestore, 'tasks'), list.forEach((element) => {
(list) => { this.allTasks.push(this.setUserObject(element.data(), element.id));
this.allTasks = []; });
list.forEach((element) => {
const taskData = { ...(element.data() as Task), id: element.id };
this.allTasks.push(taskData);
});
observer.next();
}
);
return () => unsubscribe();
}); });
} }
setUserObject(obj: any, id: string): Task {
return {
id: id,
title: obj.title,
description: obj.description,
category: obj.category,
status: obj.status,
priority: obj.priority,
subtasksTitle: obj.subtasksTitle,
subtasksDone: obj.subtasksDone,
assigned: obj.assigned,
timestamp: obj.timestamp,
};
}
getAllTasks(): Task[] {
return this.allTasks;
}
getFiltertTasks(): Task[] {
return this.filteredTasks;
}
async updateTask(taskId: any, index: number) { async updateTask(taskId: any, index: number) {
await updateDoc( await updateDoc(
doc(collection(this.firestore, 'tasks'), taskId), doc(collection(this.firestore, 'tasks'), taskId),
@ -54,4 +71,8 @@ export class TaskService {
status: task.status, status: task.status,
}; };
} }
ngOnDestroy() {
this.unsubTask();
}
} }

View file

@ -17,10 +17,6 @@ export class UserService implements OnDestroy {
this.unsubUser = this.subUserList(); this.unsubUser = this.subUserList();
} }
getUsers(): User[] {
return this.allUsers;
}
subUserList() { subUserList() {
return onSnapshot(collection(this.firestore, 'users'), (list) => { return onSnapshot(collection(this.firestore, 'users'), (list) => {
this.allUsers = []; this.allUsers = [];
@ -42,6 +38,10 @@ export class UserService implements OnDestroy {
}; };
} }
getUsers(): User[] {
return this.allUsers;
}
getUserDetails(userId: string, query: keyof User) { getUserDetails(userId: string, query: keyof User) {
const filteredUsers = this.getUsers().filter((user) => user.id === userId); const filteredUsers = this.getUsers().filter((user) => user.id === userId);
return filteredUsers.map((user) => user[query]); return filteredUsers.map((user) => user[query]);