added user service & interface

This commit is contained in:
Chneemann 2024-03-26 08:25:08 +01:00
parent d06e3f48bc
commit b95a0fb328
3 changed files with 42 additions and 2 deletions

View file

@ -29,8 +29,8 @@ export class TaskComponent {
// Subtasks
completedSubtasks() {
const subtasks = this.task.subtasksDone;
return subtasks.filter((subtask: boolean) => subtask === true).length;
return this.task.subtasksDone.filter((subtask: boolean) => subtask === true)
.length;
}
completedSubtasksPercent(): number {

View file

@ -0,0 +1,4 @@
export interface User {
id?: string;
name: string;
}

View file

@ -0,0 +1,36 @@
import { Injectable, inject } from '@angular/core';
import { Firestore, collection, onSnapshot } from '@angular/fire/firestore';
import { User } from '../interfaces/user.interface';
@Injectable({
providedIn: 'root',
})
export class UserService {
firestore: Firestore = inject(Firestore);
allUsers: User[] = [];
unsubUser;
constructor() {
this.unsubUser = this.subUserList();
}
subUserList() {
return onSnapshot(this.getUserRef(), (list) => {
this.allUsers = [];
list.forEach((element) => {
const taskData = { ...(element.data() as User), id: element.id };
this.allUsers.push(taskData);
});
});
}
getUserRef() {
return collection(this.firestore, 'users');
}
ngOnDestroy() {
this.unsubUser;
}
}