feat: created and integrated UserService for global access to user data
This commit is contained in:
parent
f24596eb8e
commit
7b85ad5493
4 changed files with 91 additions and 24 deletions
|
|
@ -99,11 +99,12 @@
|
||||||
<!-- Greeting -->
|
<!-- Greeting -->
|
||||||
<div class="right-container">
|
<div class="right-container">
|
||||||
<p>{{ greeting }},</p>
|
<p>{{ greeting }},</p>
|
||||||
<!-- TODO -->
|
@if (currentUser) {
|
||||||
<span id="welcome-user"
|
<span id="welcome-user">
|
||||||
>{{ "firstName" }}<br />
|
{{ currentUser["firstName"] }}<br />
|
||||||
{{ "lastName" }}</span
|
{{ currentUser["lastName"] }}
|
||||||
>
|
</span>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
} @else {
|
} @else {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ import { TaskService } from '../../services/task.service';
|
||||||
import { LoadingSpinnerComponent } from '../../shared/components/loading-spinner/loading-spinner.component';
|
import { LoadingSpinnerComponent } from '../../shared/components/loading-spinner/loading-spinner.component';
|
||||||
import { finalize } from 'rxjs';
|
import { finalize } from 'rxjs';
|
||||||
|
|
||||||
|
import { UserService } from '../../services/user.service';
|
||||||
|
import { User } from '../../interfaces/user.interface';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-summary',
|
selector: 'app-summary',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
|
|
@ -18,11 +21,13 @@ export class SummaryComponent {
|
||||||
nextUrgendTask: number[] = [];
|
nextUrgendTask: number[] = [];
|
||||||
|
|
||||||
allTasks: Task[] = [];
|
allTasks: Task[] = [];
|
||||||
|
currentUser: User | null = null;
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public firebaseService: FirebaseService,
|
public firebaseService: FirebaseService,
|
||||||
private translateService: TranslateService,
|
private translateService: TranslateService,
|
||||||
|
private userService: UserService,
|
||||||
private taskService: TaskService
|
private taskService: TaskService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
@ -31,6 +36,7 @@ export class SummaryComponent {
|
||||||
*/
|
*/
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.loadAllTasks();
|
this.loadAllTasks();
|
||||||
|
this.loadCurrentUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -52,6 +58,12 @@ export class SummaryComponent {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadCurrentUser(): void {
|
||||||
|
this.userService.getCurrentUser().subscribe((userData) => {
|
||||||
|
this.currentUser = userData;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the total number of tasks.
|
* Gets the total number of tasks.
|
||||||
* @returns The total count of tasks.
|
* @returns The total count of tasks.
|
||||||
|
|
|
||||||
66
src/app/services/user.service.ts
Normal file
66
src/app/services/user.service.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { BehaviorSubject, Observable, of } from 'rxjs';
|
||||||
|
import { catchError, switchMap, tap } from 'rxjs/operators';
|
||||||
|
import { ApiService } from './api.service';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
import { User } from '../interfaces/user.interface';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class UserService {
|
||||||
|
private currentUserCache: User | null = null;
|
||||||
|
private currentUserSubject: BehaviorSubject<User | null> =
|
||||||
|
new BehaviorSubject<User | null>(null);
|
||||||
|
private loading = false;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private authService: AuthService,
|
||||||
|
private apiService: ApiService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the current user from the cache if available, or loads it from the API.
|
||||||
|
*
|
||||||
|
* @returns An Observable resolving to the current user, or null if not available.
|
||||||
|
*/
|
||||||
|
getCurrentUser(): Observable<User | null> {
|
||||||
|
if (this.currentUserCache) {
|
||||||
|
this.currentUserSubject.next(this.currentUserCache);
|
||||||
|
return this.currentUserSubject.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.loadCurrentUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the current user data by fetching the user ID from the AuthService and
|
||||||
|
* retrieving the user details from the ApiService.
|
||||||
|
*
|
||||||
|
* @returns an Observable resolving to the current user, or null if none exists
|
||||||
|
*/
|
||||||
|
loadCurrentUser(): Observable<User | null> {
|
||||||
|
if (this.loading) {
|
||||||
|
return this.currentUserSubject.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.loading = true;
|
||||||
|
|
||||||
|
return this.authService.getCurrentUserId().pipe(
|
||||||
|
switchMap((userId) => {
|
||||||
|
if (!userId) return of(null);
|
||||||
|
|
||||||
|
return this.apiService.getUserById(userId).pipe(
|
||||||
|
catchError(() => of(null)),
|
||||||
|
tap((userData) => {
|
||||||
|
if (userData) {
|
||||||
|
this.currentUserCache = userData;
|
||||||
|
this.currentUserSubject.next(userData);
|
||||||
|
}
|
||||||
|
this.loading = false;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,9 +4,8 @@ import { NavbarComponent } from './navbar/navbar.component';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { FirebaseService } from '../../../services/firebase.service';
|
import { FirebaseService } from '../../../services/firebase.service';
|
||||||
import { AuthService } from '../../../services/auth.service';
|
import { UserService } from '../../../services/user.service';
|
||||||
import { ApiService } from '../../../services/api.service';
|
import { User } from '../../../interfaces/user.interface';
|
||||||
import { catchError, of, switchMap } from 'rxjs';
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-header',
|
selector: 'app-header',
|
||||||
|
|
@ -18,12 +17,11 @@ import { catchError, of, switchMap } from 'rxjs';
|
||||||
export class HeaderComponent {
|
export class HeaderComponent {
|
||||||
navbarVisible: boolean = false;
|
navbarVisible: boolean = false;
|
||||||
navbarLanguageVisible: boolean = false;
|
navbarLanguageVisible: boolean = false;
|
||||||
currentUser: any = null;
|
currentUser: User | null = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public firebaseService: FirebaseService,
|
public firebaseService: FirebaseService,
|
||||||
private authService: AuthService,
|
private userService: UserService
|
||||||
private apiService: ApiService
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -37,18 +35,8 @@ export class HeaderComponent {
|
||||||
* Loads the current user data and sets it to the `currentUser` property.
|
* Loads the current user data and sets it to the `currentUser` property.
|
||||||
*/
|
*/
|
||||||
loadCurrentUser(): void {
|
loadCurrentUser(): void {
|
||||||
this.authService
|
this.userService.getCurrentUser().subscribe((userData) => {
|
||||||
.getCurrentUserId()
|
this.currentUser = userData;
|
||||||
.pipe(
|
|
||||||
switchMap((userId) => {
|
|
||||||
if (!userId) return of(null);
|
|
||||||
return this.apiService
|
|
||||||
.getUserById(userId)
|
|
||||||
.pipe(catchError(() => of(null)));
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.subscribe((userData) => {
|
|
||||||
this.currentUser = userData || {};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue