migrate angular 21 tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {EnvironmentInjector, Injectable, inject, runInInjectionContext} from '@angular/core';
|
||||
import {
|
||||
addDoc,
|
||||
collection,
|
||||
@@ -25,7 +25,8 @@ type DocumentPredicate<T> = string | DbDocument<T>;
|
||||
export class DbCollection<T> {
|
||||
public constructor(
|
||||
private readonly fs: Firestore,
|
||||
private readonly path: string
|
||||
private readonly path: string,
|
||||
private readonly environmentInjector: EnvironmentInjector
|
||||
) {}
|
||||
|
||||
public add(data: Partial<T>): Promise<DocumentReference<T>> {
|
||||
@@ -33,7 +34,7 @@ export class DbCollection<T> {
|
||||
}
|
||||
|
||||
public valueChanges(options?: {idField?: string}): Observable<T[]> {
|
||||
return collectionData(this.ref, options as {idField?: never}) as Observable<T[]>;
|
||||
return runInInjectionContext(this.environmentInjector, () => collectionData(this.ref, options as {idField?: never}) as Observable<T[]>);
|
||||
}
|
||||
|
||||
private get ref(): CollectionReference<DocumentData> {
|
||||
@@ -44,7 +45,8 @@ export class DbCollection<T> {
|
||||
export class DbDocument<T> {
|
||||
public constructor(
|
||||
private readonly fs: Firestore,
|
||||
private readonly path: string
|
||||
private readonly path: string,
|
||||
private readonly environmentInjector: EnvironmentInjector
|
||||
) {}
|
||||
|
||||
public set(data: Partial<T>): Promise<void> {
|
||||
@@ -60,11 +62,14 @@ export class DbDocument<T> {
|
||||
}
|
||||
|
||||
public collection<U>(subPath: string): DbCollection<U> {
|
||||
return new DbCollection<U>(this.fs, `${this.path}/${subPath}`);
|
||||
return new DbCollection<U>(this.fs, `${this.path}/${subPath}`, this.environmentInjector);
|
||||
}
|
||||
|
||||
public valueChanges(options?: {idField?: string}): Observable<(NonNullable<T> & {id?: string}) | undefined> {
|
||||
return docData(this.ref as DocumentReference<T>, options as {idField?: never}) as Observable<(NonNullable<T> & {id?: string}) | undefined>;
|
||||
return runInInjectionContext(
|
||||
this.environmentInjector,
|
||||
() => docData(this.ref as DocumentReference<T>, options as {idField?: never}) as Observable<(NonNullable<T> & {id?: string}) | undefined>
|
||||
);
|
||||
}
|
||||
|
||||
private get ref(): DocumentReference<DocumentData> {
|
||||
@@ -77,13 +82,14 @@ export class DbDocument<T> {
|
||||
})
|
||||
export class DbService {
|
||||
private fs = inject(Firestore);
|
||||
private environmentInjector = inject(EnvironmentInjector);
|
||||
|
||||
public col<T>(ref: CollectionPredicate<T>): DbCollection<T> {
|
||||
return typeof ref === 'string' ? new DbCollection<T>(this.fs, ref) : ref;
|
||||
return typeof ref === 'string' ? new DbCollection<T>(this.fs, ref, this.environmentInjector) : ref;
|
||||
}
|
||||
|
||||
public doc<T>(ref: DocumentPredicate<T>): DbDocument<T> {
|
||||
return typeof ref === 'string' ? new DbDocument<T>(this.fs, ref) : ref;
|
||||
return typeof ref === 'string' ? new DbDocument<T>(this.fs, ref, this.environmentInjector) : ref;
|
||||
}
|
||||
|
||||
public doc$<T>(ref: DocumentPredicate<T>): Observable<(NonNullable<T> & {id?: string}) | null> {
|
||||
@@ -98,6 +104,6 @@ export class DbService {
|
||||
}
|
||||
|
||||
const q = query(collection(this.fs, ref), ...queryConstraints);
|
||||
return collectionData(q, {idField: 'id'}) as Observable<T[]>;
|
||||
return runInInjectionContext(this.environmentInjector, () => collectionData(q, {idField: 'id'}) as Observable<T[]>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {EnvironmentInjector, Injectable, inject, runInInjectionContext} from '@angular/core';
|
||||
import {Auth, authState, createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, signOut} from '@angular/fire/auth';
|
||||
import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs';
|
||||
import {filter, map, shareReplay, switchMap, take, tap} from 'rxjs/operators';
|
||||
@@ -25,6 +25,7 @@ export class UserService {
|
||||
private router = inject(Router);
|
||||
private showDataService = inject(ShowDataService);
|
||||
private showSongDataService = inject(ShowSongDataService);
|
||||
private environmentInjector = inject(EnvironmentInjector);
|
||||
|
||||
public users$ = this.db.col$<User>('users').pipe(shareReplay({bufferSize: 1, refCount: true}));
|
||||
private iUserId$ = new BehaviorSubject<string | null>(null);
|
||||
@@ -32,7 +33,7 @@ export class UserService {
|
||||
private userByIdCache = new Map<string, Observable<User | null>>();
|
||||
|
||||
public constructor() {
|
||||
authState(this.auth)
|
||||
this.authState$()
|
||||
.pipe(
|
||||
filter(auth => !!auth),
|
||||
map(auth => auth?.uid ?? ''),
|
||||
@@ -65,7 +66,7 @@ export class UserService {
|
||||
};
|
||||
|
||||
public async login(user: string, password: string): Promise<string | null> {
|
||||
const aUser = await signInWithEmailAndPassword(this.auth, user, password);
|
||||
const aUser = await this.runInFirebaseContext(() => signInWithEmailAndPassword(this.auth, user, password));
|
||||
if (!aUser.user) return null;
|
||||
const dUser = await this.readUser(aUser.user.uid);
|
||||
if (!dUser) return null;
|
||||
@@ -76,12 +77,12 @@ export class UserService {
|
||||
return aUser.user.uid;
|
||||
}
|
||||
|
||||
public loggedIn$: () => Observable<boolean> = () => authState(this.auth).pipe(map(_ => !!_));
|
||||
public loggedIn$: () => Observable<boolean> = () => this.authState$().pipe(map(_ => !!_));
|
||||
|
||||
public list$: () => Observable<User[]> = (): Observable<User[]> => this.users$;
|
||||
|
||||
public async logout(): Promise<void> {
|
||||
await signOut(this.auth);
|
||||
await this.runInFirebaseContext(() => signOut(this.auth));
|
||||
this.iUser$.next(null);
|
||||
this.iUserId$.next(null);
|
||||
}
|
||||
@@ -92,11 +93,11 @@ export class UserService {
|
||||
|
||||
public async changePassword(user: string): Promise<void> {
|
||||
const url = environment.url;
|
||||
await sendPasswordResetEmail(this.auth, user, {url});
|
||||
await this.runInFirebaseContext(() => sendPasswordResetEmail(this.auth, user, {url}));
|
||||
}
|
||||
|
||||
public async createNewUser(user: string, name: string, password: string): Promise<void> {
|
||||
const aUser = await createUserWithEmailAndPassword(this.auth, user, password);
|
||||
const aUser = await this.runInFirebaseContext(() => createUserWithEmailAndPassword(this.auth, user, password));
|
||||
if (!aUser.user) return;
|
||||
const userId = aUser.user.uid;
|
||||
await this.db.doc('users/' + userId).set({name, chordMode: 'onlyFirst', songUsage: {}});
|
||||
@@ -176,6 +177,8 @@ export class UserService {
|
||||
return role.split(';').includes('admin');
|
||||
}
|
||||
|
||||
private authState$ = () => runInInjectionContext(this.environmentInjector, () => authState(this.auth));
|
||||
private runInFirebaseContext = <T>(factory: () => T): T => runInInjectionContext(this.environmentInjector, factory);
|
||||
private readUser$ = (uid: string) => this.db.doc$<User>('users/' + uid);
|
||||
private readUser: (uid: string) => Promise<User | null> = (uid: string) => firstValueFrom(this.readUser$(uid));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user