From b6d0b42a1c23a66da4b6fadebf89fa29c3b2dcad Mon Sep 17 00:00:00 2001
From: "Zickermann, Jan" <jan.zickermann@dataport.de>
Date: Thu, 28 Mar 2024 11:12:59 +0100
Subject: [PATCH] OZG-4993 OZG-5300 Save local state on resource service save

---
 .../postfach-form/postfach.formservice.ts     |  4 ++--
 .../src/lib/postfach/postfach.service.ts      |  2 +-
 .../src/lib/resource/resource.service.spec.ts | 19 ++++++++++++++-----
 .../src/lib/resource/resource.service.ts      | 17 +++++++----------
 4 files changed, 24 insertions(+), 18 deletions(-)

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 8eb84a9e47..1a8f122192 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
@@ -7,7 +7,7 @@ import {
 } from '@alfa-client/tech-shared';
 import { Injectable } from '@angular/core';
 import { FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
-import { map, Observable, switchMap } from 'rxjs';
+import { map, Observable } from 'rxjs';
 import { PostfachService } from '../../postfach.service';
 import { Postfach, PostfachResource } from '../../postfach.model';
 import { isNil } from 'lodash-es';
@@ -48,7 +48,7 @@ export class PostfachFormService extends AbstractFormService {
     if (this.shouldSkipAbsender(value)) {
       delete value.absender;
     }
-    return this.postfachService.save(value).pipe(switchMap(() => this.postfachService.get()));
+    return this.postfachService.save(value);
   }
 
   private shouldSkipAbsender(postfach: Postfach): boolean {
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 4fa62e3680..b35600877d 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
@@ -4,7 +4,7 @@ import { Postfach, PostfachResource, PostfachSettingsItem } from './postfach.mod
 import { ResourceService } from 'libs/tech-shared/src/lib/resource/resource.service';
 import { SettingsService } from '../admin-settings.service';
 import { ResourceServiceConfig } from 'libs/tech-shared/src/lib/resource/resource.model';
-import { Observable, of, tap } from 'rxjs';
+import { Observable } from 'rxjs';
 import { HttpError, StateResource } from '@alfa-client/tech-shared';
 import { SettingName } from '../admin-settings.model';
 import { PostfachLinkRel } from './postfach.linkrel';
diff --git a/alfa-client/libs/tech-shared/src/lib/resource/resource.service.spec.ts b/alfa-client/libs/tech-shared/src/lib/resource/resource.service.spec.ts
index 3311716c86..08d197454b 100644
--- a/alfa-client/libs/tech-shared/src/lib/resource/resource.service.spec.ts
+++ b/alfa-client/libs/tech-shared/src/lib/resource/resource.service.spec.ts
@@ -1,15 +1,16 @@
 import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
-import { Observable, lastValueFrom, of, throwError } from 'rxjs';
+import { lastValueFrom, Observable, of, throwError } from 'rxjs';
 import { ResourceService } from './resource.service';
 import { ResourceRepository } from './resource.repository';
 import { LinkRelationName, ResourceServiceConfig, SaveResourceData } from './resource.model';
-import { Resource, getUrl } from '@ngxp/rest';
+import { getUrl, Resource } from '@ngxp/rest';
 import { createDummyResource } from 'libs/tech-shared/test/resource';
+import * as ResourceUtil from './resource.util';
 import {
-  StateResource,
   createEmptyStateResource,
   createErrorStateResource,
   createStateResource,
+  StateResource,
 } from './resource.util';
 import { fakeAsync, tick } from '@angular/core/testing';
 import { singleCold, singleHot } from './marbles';
@@ -18,8 +19,6 @@ import { createProblemDetail } from 'libs/tech-shared/test/error';
 import { HttpError, ProblemDetail } from '../tech.model';
 import { cold } from 'jest-marbles';
 
-import * as ResourceUtil from './resource.util';
-
 describe('ResourceService', () => {
   let service: ResourceService<Resource, Resource>;
   let config: ResourceServiceConfig<Resource>;
@@ -443,6 +442,16 @@ describe('ResourceService', () => {
 
       expect(service.handleError).toHaveBeenCalledWith(errorResponse);
     });
+
+    it('should update state resource subject', fakeAsync(() => {
+      service.stateResource.next(createStateResource(resourceWithEditLinkRel));
+      repository.save.mockReturnValue(of(loadedResource));
+
+      service.save(dummyToSave).subscribe();
+      tick();
+
+      expect(service.stateResource.value).toEqual(createStateResource(loadedResource));
+    }));
   });
 
   describe('handleError', () => {
diff --git a/alfa-client/libs/tech-shared/src/lib/resource/resource.service.ts b/alfa-client/libs/tech-shared/src/lib/resource/resource.service.ts
index c14a661395..a5a705e9d9 100644
--- a/alfa-client/libs/tech-shared/src/lib/resource/resource.service.ts
+++ b/alfa-client/libs/tech-shared/src/lib/resource/resource.service.ts
@@ -1,11 +1,10 @@
 import {
   BehaviorSubject,
-  Observable,
   catchError,
   combineLatest,
   filter,
   map,
-  mergeMap,
+  Observable,
   of,
   startWith,
   tap,
@@ -13,21 +12,20 @@ import {
 } from 'rxjs';
 import { ResourceServiceConfig } from './resource.model';
 import {
-  StateResource,
   createEmptyStateResource,
   createErrorStateResource,
   createStateResource,
   isLoadingRequired,
+  StateResource,
   throwErrorOn,
 } from './resource.util';
-import { Resource, getUrl, hasLink } from '@ngxp/rest';
+import { getUrl, hasLink, Resource } from '@ngxp/rest';
 import { ResourceRepository } from './resource.repository';
 import { isEqual, isNull } from 'lodash-es';
 import { isNotNull } from '../tech.util';
 import { HttpErrorResponse } from '@angular/common/http';
 import { isUnprocessableEntity } from '../http.util';
 import { HttpError, ProblemDetail } from '../tech.model';
-import { isDevMode } from '@angular/core';
 
 /**
  * B = Type of baseresource
@@ -146,11 +144,10 @@ export class ResourceService<B extends Resource, T extends Resource> {
 
   public save(toSave: unknown): Observable<StateResource<T | HttpError>> {
     this.verifyEditLinkRel();
-    return this.stateResource.asObservable().pipe(
-      mergeMap((selectedResource: StateResource<T>) =>
-        this.doSave(selectedResource.resource, toSave),
-      ),
-      map((loadedResource: T) => createStateResource(loadedResource)),
+    const previousResource: T = this.stateResource.value.resource;
+    return this.doSave(previousResource, toSave).pipe(
+      tap((loadedResource: T) => this.stateResource.next(createStateResource(loadedResource))),
+      map(() => this.stateResource.value),
       catchError((errorResponse: HttpErrorResponse) => this.handleError(errorResponse)),
     );
   }
-- 
GitLab