From 181d424ba1ff590d1e1695cd44efb443c7ca41dd Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Fri, 7 Jun 2024 13:54:31 +0200
Subject: [PATCH] OZG-5012 adjust abstract formservice Type; add missing file

---
 .../postfach-form/postfach.formservice.ts     | 14 ++-----
 .../src/lib/postfach/postfach.service.ts      |  4 +-
 .../lib/ngrx/api-state-service.executer.ts    | 41 +++++++++++++++++++
 .../src/lib/resource/resource.loader.ts       |  2 +-
 .../src/lib/service/formservice.abstract.ts   |  6 +--
 5 files changed, 50 insertions(+), 17 deletions(-)
 create mode 100644 alfa-client/libs/tech-shared/src/lib/ngrx/api-state-service.executer.ts

diff --git a/alfa-client/libs/admin-settings/src/lib/postfach/postfach-container/postfach-form/postfach.formservice.ts b/alfa-client/libs/admin-settings/src/lib/postfach/postfach-container/postfach-form/postfach.formservice.ts
index c6d801bdd9..b77eab038c 100644
--- a/alfa-client/libs/admin-settings/src/lib/postfach/postfach-container/postfach-form/postfach.formservice.ts
+++ b/alfa-client/libs/admin-settings/src/lib/postfach/postfach-container/postfach-form/postfach.formservice.ts
@@ -1,10 +1,4 @@
-import {
-  AbstractFormService,
-  EMPTY_STRING,
-  HttpError,
-  ProblemDetail,
-  StateResource,
-} from '@alfa-client/tech-shared';
+import { AbstractFormService, EMPTY_STRING, StateResource } from '@alfa-client/tech-shared';
 import { Injectable } from '@angular/core';
 import { FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
 import { isNil } from 'lodash-es';
@@ -43,7 +37,7 @@ export class PostfachFormService extends AbstractFormService {
     });
   }
 
-  protected doSubmit(): Observable<StateResource<PostfachResource | HttpError>> {
+  protected doSubmit(): Observable<StateResource<PostfachResource>> {
     const value: Postfach = this.getFormValue();
     if (this.shouldSkipAbsender(value)) {
       delete value.absender;
@@ -68,9 +62,7 @@ export class PostfachFormService extends AbstractFormService {
 
   public submitWithProgress(): Observable<boolean> {
     return this.submit().pipe(
-      map(
-        (stateResource: StateResource<PostfachResource | ProblemDetail>) => stateResource.loading,
-      ),
+      map((stateResource: StateResource<PostfachResource>) => stateResource.loading),
     );
   }
 }
diff --git a/alfa-client/libs/admin-settings/src/lib/postfach/postfach.service.ts b/alfa-client/libs/admin-settings/src/lib/postfach/postfach.service.ts
index 24eb2f3b5b..f2de8ffd4b 100644
--- a/alfa-client/libs/admin-settings/src/lib/postfach/postfach.service.ts
+++ b/alfa-client/libs/admin-settings/src/lib/postfach/postfach.service.ts
@@ -1,4 +1,4 @@
-import { HttpError, StateResource } from '@alfa-client/tech-shared';
+import { StateResource } from '@alfa-client/tech-shared';
 import { Injectable } from '@angular/core';
 import { Observable } from 'rxjs';
 import { SettingName } from '../admin-settings.model';
@@ -13,7 +13,7 @@ export class PostfachService {
     return this.postfachResourceService.get();
   }
 
-  public save(postfach: Postfach): Observable<StateResource<PostfachResource | HttpError>> {
+  public save(postfach: Postfach): Observable<StateResource<PostfachResource>> {
     return this.postfachResourceService.save(this.buildPostfachSettingItem(postfach));
   }
 
diff --git a/alfa-client/libs/tech-shared/src/lib/ngrx/api-state-service.executer.ts b/alfa-client/libs/tech-shared/src/lib/ngrx/api-state-service.executer.ts
new file mode 100644
index 0000000000..6a479a6642
--- /dev/null
+++ b/alfa-client/libs/tech-shared/src/lib/ngrx/api-state-service.executer.ts
@@ -0,0 +1,41 @@
+import { Resource } from '@ngxp/rest';
+import { Observable, map, withLatestFrom } from 'rxjs';
+import { StateResource } from '../resource/resource.util';
+import { ApiSingleResourceStateService } from './state.service';
+
+export class ApiStateServiceExecuter<B extends Resource, T extends Resource> {
+  private stateService: ApiSingleResourceStateService<B, T>;
+  private baseResource: Observable<StateResource<T>>;
+  shouldExecute: boolean;
+
+  public static init<B extends Resource, T extends Resource>(
+    stateService: ApiSingleResourceStateService<B, T>,
+  ): ApiStateServiceExecuter<B, T> {
+    return new ApiStateServiceExecuter<B, T>(stateService);
+  }
+
+  constructor(stateService: ApiSingleResourceStateService<B, T>) {
+    this.stateService = stateService;
+
+    this.shouldExecute = true;
+  }
+
+  public withBaseResource(baseResource: Observable<StateResource<T>>): this {
+    this.baseResource = baseResource;
+    return this;
+  }
+
+  public execute(runnable: (resource: T) => void): Observable<StateResource<T>> {
+    return this.baseResource.pipe(
+      withLatestFrom(this.stateService.selectResource()),
+      map(([baseStateResource, stateResource]) => {
+        if (this.shouldExecute && !baseStateResource.loading) {
+          runnable(stateResource.resource);
+          this.shouldExecute = false;
+          return { ...baseStateResource, loading: true };
+        }
+        return baseStateResource;
+      }),
+    );
+  }
+}
diff --git a/alfa-client/libs/tech-shared/src/lib/resource/resource.loader.ts b/alfa-client/libs/tech-shared/src/lib/resource/resource.loader.ts
index 5c38968831..f65fc2011c 100644
--- a/alfa-client/libs/tech-shared/src/lib/resource/resource.loader.ts
+++ b/alfa-client/libs/tech-shared/src/lib/resource/resource.loader.ts
@@ -18,7 +18,7 @@ export class ResourceLoader<B extends Resource, T extends Resource> {
 
   constructor(
     private config: ServiceConfig<B>,
-    private resourceStateService: ResourceStateService<T>,
+    private resourceStateService: ResourceStateService<B, T>,
   ) {}
 
   public get(): Observable<StateResource<T>> {
diff --git a/alfa-client/libs/tech-shared/src/lib/service/formservice.abstract.ts b/alfa-client/libs/tech-shared/src/lib/service/formservice.abstract.ts
index a02d2bab0a..6d18542c1d 100644
--- a/alfa-client/libs/tech-shared/src/lib/service/formservice.abstract.ts
+++ b/alfa-client/libs/tech-shared/src/lib/service/formservice.abstract.ts
@@ -48,13 +48,13 @@ export abstract class AbstractFormService {
 
   protected abstract initForm(): UntypedFormGroup;
 
-  public submit(): Observable<StateResource<Resource | HttpError>> {
+  public submit(): Observable<StateResource<Resource>> {
     return this.doSubmit().pipe(map((result) => this.handleResponse(result)));
   }
 
-  protected abstract doSubmit(): Observable<StateResource<Resource | HttpError>>;
+  protected abstract doSubmit(): Observable<StateResource<Resource>>;
 
-  handleResponse(result: StateResource<Resource | HttpError>): StateResource<Resource | HttpError> {
+  handleResponse(result: StateResource<Resource>): StateResource<Resource> {
     if (result.loading) return result;
     if (hasStateResourceError(result)) {
       this.handleError(result.error);
-- 
GitLab