refactor(deposit, login-success, user.service): convert async to observables

This commit is contained in:
Jan K9f 2025-02-27 15:39:05 +01:00
parent 925a9d540d
commit 6bd2928166
3 changed files with 26 additions and 19 deletions

View file

@ -10,7 +10,7 @@ import {
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { loadStripe, Stripe } from '@stripe/stripe-js'; import { loadStripe, Stripe } from '@stripe/stripe-js';
import { DepositService } from '../../service/deposit.service'; import { DepositService } from '../../service/deposit.service';
import { debounceTime } from 'rxjs'; import { debounceTime, from } from 'rxjs';
import { environment } from '../../../environments/environment'; import { environment } from '../../../environments/environment';
import { NgIf } from '@angular/common'; import { NgIf } from '@angular/common';
@ -29,7 +29,7 @@ export class DepositComponent implements OnInit {
private stripe: Stripe | null = null; private stripe: Stripe | null = null;
private service: DepositService = inject(DepositService); private service: DepositService = inject(DepositService);
async ngOnInit() { ngOnInit() {
this.form = new FormGroup({ this.form = new FormGroup({
amount: new FormControl(50, [Validators.min(50)]), amount: new FormControl(50, [Validators.min(50)]),
}); });
@ -40,7 +40,9 @@ export class DepositComponent implements OnInit {
} }
}); });
this.stripe = await loadStripe(environment.STRIPE_KEY); from(loadStripe(environment.STRIPE_KEY)).subscribe(stripe => {
this.stripe = stripe;
});
} }
submit() { submit() {

View file

@ -2,6 +2,7 @@ import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/cor
import { UserService } from '../../service/user.service'; import { UserService } from '../../service/user.service';
import { KeycloakService } from 'keycloak-angular'; import { KeycloakService } from 'keycloak-angular';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { from, switchMap } from 'rxjs';
@Component({ @Component({
selector: 'app-login-success', selector: 'app-login-success',
@ -16,11 +17,14 @@ export default class LoginSuccessComponent implements OnInit {
private keycloakService: KeycloakService = inject(KeycloakService); private keycloakService: KeycloakService = inject(KeycloakService);
private router: Router = inject(Router); private router: Router = inject(Router);
async ngOnInit() { ngOnInit() {
const userProfile = await this.keycloakService.loadUserProfile(); from(this.keycloakService.loadUserProfile()).pipe(
const user = await this.userService.getOrCreateUser(userProfile); switchMap(userProfile => this.userService.getOrCreateUser(userProfile))
sessionStorage.setItem('user', JSON.stringify(user)); ).subscribe(user => {
if (user) {
this.router.navigate(['home']); sessionStorage.setItem('user', JSON.stringify(user));
}
this.router.navigate(['home']);
});
} }
} }

View file

@ -1,7 +1,7 @@
import { inject, Injectable } from '@angular/core'; import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { KeycloakProfile } from 'keycloak-js'; import { KeycloakProfile } from 'keycloak-js';
import { catchError, EMPTY, Observable } from 'rxjs'; import { catchError, EMPTY, Observable, of, switchMap } from 'rxjs';
import { User } from '../model/User'; import { User } from '../model/User';
@Injectable({ @Injectable({
@ -21,18 +21,19 @@ export class UserService {
}); });
} }
public async getOrCreateUser(userProfile: KeycloakProfile) { public getOrCreateUser(userProfile: KeycloakProfile): Observable<User | undefined> {
if (userProfile.id == null) { if (userProfile.id == null) {
return; return of(undefined);
} }
return await this.getUser(userProfile.id)
.toPromise() return this.getUser(userProfile.id).pipe(
.then(async (user) => { switchMap(user => {
if (user) { if (user) {
return user; return of(user);
} }
return await this.createUser(userProfile.id ?? '', userProfile.username ?? '').toPromise(); return this.createUser(userProfile.id ?? '', userProfile.username ?? '');
}); })
);
} }
} }