From fa127b785aa8641d8ca61db3d870d56504f1b232 Mon Sep 17 00:00:00 2001 From: NazarMykhalkevych Date: Thu, 3 Jul 2025 16:44:37 +0300 Subject: [PATCH 1/2] feat(registries): add shared state-errors handler --- src/app/core/handlers/state-error.handler.ts | 14 +++++++ .../registries/store/registries.state.ts | 37 +++++++------------ 2 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 src/app/core/handlers/state-error.handler.ts diff --git a/src/app/core/handlers/state-error.handler.ts b/src/app/core/handlers/state-error.handler.ts new file mode 100644 index 000000000..dc050f432 --- /dev/null +++ b/src/app/core/handlers/state-error.handler.ts @@ -0,0 +1,14 @@ +import { StateContext } from '@ngxs/store'; + +import { throwError } from 'rxjs'; + +export function handleSectionError(ctx: StateContext, section: keyof T, error: Error) { + ctx.patchState({ + [section]: { + ...ctx.getState()[section], + isLoading: false, + error: error.message, + }, + } as Partial); + return throwError(() => error); +} diff --git a/src/app/features/registries/store/registries.state.ts b/src/app/features/registries/store/registries.state.ts index 9cf274cad..b868fae0d 100644 --- a/src/app/features/registries/store/registries.state.ts +++ b/src/app/features/registries/store/registries.state.ts @@ -1,10 +1,10 @@ import { Action, State, StateContext } from '@ngxs/store'; -import { throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators'; import { inject, Injectable } from '@angular/core'; +import { handleSectionError } from '@osf/core/handlers/state-error.handler'; import { ResourceTab } from '@osf/shared/enums'; import { SearchService } from '@osf/shared/services'; import { getResourceTypes } from '@osf/shared/utils'; @@ -115,7 +115,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'registries', error)) + catchError((error) => handleSectionError(ctx, 'registries', error)) ); } @@ -203,7 +203,7 @@ export class RegistriesState { error: error.message, }, }); - return this.handleError(ctx, 'draftRegistration', error); + return handleSectionError(ctx, 'draftRegistration', error); }) ); } @@ -229,7 +229,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'draftRegistration', error)) + catchError((error) => handleSectionError(ctx, 'draftRegistration', error)) ); } @@ -253,7 +253,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'draftRegistration', error)) + catchError((error) => handleSectionError(ctx, 'draftRegistration', error)) ); } @@ -273,7 +273,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'pagesSchema', error)) + catchError((error) => handleSectionError(ctx, 'pagesSchema', error)) ); } @@ -295,7 +295,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'contributorsList', error)) + catchError((error) => handleSectionError(ctx, 'contributorsList', error)) ); } @@ -319,7 +319,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'contributorsList', error)) + catchError((error) => handleSectionError(ctx, 'contributorsList', error)) ); } @@ -345,7 +345,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'contributorsList', error)) + catchError((error) => handleSectionError(ctx, 'contributorsList', error)) ); } @@ -367,7 +367,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'contributorsList', error)) + catchError((error) => handleSectionError(ctx, 'contributorsList', error)) ); } @@ -390,7 +390,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'licenses', error)) + catchError((error) => handleSectionError(ctx, 'licenses', error)) ); } @@ -414,7 +414,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'registrationSubjects', error)) + catchError((error) => handleSectionError(ctx, 'registrationSubjects', error)) ); } @@ -440,18 +440,7 @@ export class RegistriesState { }, }); }), - catchError((error) => this.handleError(ctx, 'registrationSubjects', error)) + catchError((error) => handleSectionError(ctx, 'registrationSubjects', error)) ); } - - private handleError(ctx: StateContext, section: keyof RegistriesStateModel, error: Error) { - ctx.patchState({ - [section]: { - ...ctx.getState()[section], - isLoading: false, - error: error.message, - }, - }); - return throwError(() => error); - } } From 92fd53bedd3c8c2977b401a7d0c8e9186c00d207 Mon Sep 17 00:00:00 2001 From: NazarMykhalkevych Date: Thu, 3 Jul 2025 18:05:24 +0300 Subject: [PATCH 2/2] feat(registries): add shared state-errors handler --- src/app/core/handlers/index.ts | 1 + src/app/features/registries/store/registries.state.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/core/handlers/index.ts b/src/app/core/handlers/index.ts index 400695f14..70ca4d249 100644 --- a/src/app/core/handlers/index.ts +++ b/src/app/core/handlers/index.ts @@ -1 +1,2 @@ export { GlobalErrorHandler } from './global-error.handler'; +export { handleSectionError } from './state-error.handler'; diff --git a/src/app/features/registries/store/registries.state.ts b/src/app/features/registries/store/registries.state.ts index b868fae0d..6f01d6cf9 100644 --- a/src/app/features/registries/store/registries.state.ts +++ b/src/app/features/registries/store/registries.state.ts @@ -4,7 +4,7 @@ import { catchError, tap } from 'rxjs/operators'; import { inject, Injectable } from '@angular/core'; -import { handleSectionError } from '@osf/core/handlers/state-error.handler'; +import { handleSectionError } from '@osf/core/handlers'; import { ResourceTab } from '@osf/shared/enums'; import { SearchService } from '@osf/shared/services'; import { getResourceTypes } from '@osf/shared/utils';