From 36317aae757fa25229c0df83c78ef9900fd17800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Tue, 17 Jan 2023 07:45:32 +0200 Subject: [PATCH] Show toast on my profile update error --- src/lib/users/services/iqser-user.service.ts | 26 +++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/lib/users/services/iqser-user.service.ts b/src/lib/users/services/iqser-user.service.ts index 651c10a..35624c0 100644 --- a/src/lib/users/services/iqser-user.service.ts +++ b/src/lib/users/services/iqser-user.service.ts @@ -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(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(@RequiredParam() body: T) { - return this._post(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(body, `${this._defaultModelPath}/my-profile`).pipe(catchError(showToast)); } @Validate()