Show toast on my profile update error

This commit is contained in:
Adina Țeudan 2023-01-17 07:45:32 +02:00
parent f98109ca45
commit 36317aae75

View File

@ -1,9 +1,9 @@
import { inject, Injectable } from '@angular/core';
import { KeycloakService } from 'keycloak-angular';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { BehaviorSubject, firstValueFrom, Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BASE_HREF, List, mapEach, RequiredParam, Validate } from '../../utils';
import { QueryParam } from '../../services';
import { QueryParam, Toaster } from '../../services';
import { CacheApiService } from '../../caching';
import { EntitiesService } from '../../listing';
import { IIqserUser } from '../types/user.response';
@ -14,11 +14,8 @@ import { IProfileUpdateRequest } from '../types/profile-update.request';
import { KeycloakProfile } from 'keycloak-js';
import { IqserUser } from '../iqser-user.model';
import { IqserPermissionsService, IqserRolesService } from '../../permissions';
interface IToken {
sub: string;
resource_access: { account: { roles: List }; [key: string]: { roles: List } };
}
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
@Injectable()
export abstract class IqserUserService<
@ -32,6 +29,7 @@ export abstract class IqserUserService<
protected abstract readonly _entityClass: new (entityInterface: Interface | KeycloakProfile, ...args: unknown[]) => Class;
protected readonly _currentUser$ = new BehaviorSubject<Class | undefined>(undefined);
protected readonly _baseHref = inject(BASE_HREF);
protected readonly _toaster = inject(Toaster);
protected readonly _keycloakService = inject(KeycloakService);
protected readonly _cacheApiService = inject(CacheApiService);
protected readonly _permissionsService = inject(IqserPermissionsService, { optional: true });
@ -126,7 +124,17 @@ export abstract class IqserUserService<
@Validate()
updateMyProfile<T = IMyProfileUpdateRequest>(@RequiredParam() body: T) {
return this._post<unknown>(body, `${this._defaultModelPath}/my-profile`);
const showToast = (error: HttpErrorResponse) => {
if (error.status === HttpStatusCode.BadRequest) {
const { message } = error.error;
this._toaster.error(_('update-profile.errors.bad-request'), { params: { message } });
} else {
this._toaster.error(_('update-profile.errors.generic'));
}
return throwError(() => error);
};
return this._post<unknown>(body, `${this._defaultModelPath}/my-profile`).pipe(catchError(showToast));
}
@Validate()