Skip to content
Snippets Groups Projects
Commit 181d424b authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5012 adjust abstract formservice Type; add missing file

parent 49e60c7b
No related branches found
No related tags found
No related merge requests found
import { import { AbstractFormService, EMPTY_STRING, StateResource } from '@alfa-client/tech-shared';
AbstractFormService,
EMPTY_STRING,
HttpError,
ProblemDetail,
StateResource,
} from '@alfa-client/tech-shared';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { FormControl, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
...@@ -43,7 +37,7 @@ export class PostfachFormService extends AbstractFormService { ...@@ -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(); const value: Postfach = this.getFormValue();
if (this.shouldSkipAbsender(value)) { if (this.shouldSkipAbsender(value)) {
delete value.absender; delete value.absender;
...@@ -68,9 +62,7 @@ export class PostfachFormService extends AbstractFormService { ...@@ -68,9 +62,7 @@ export class PostfachFormService extends AbstractFormService {
public submitWithProgress(): Observable<boolean> { public submitWithProgress(): Observable<boolean> {
return this.submit().pipe( return this.submit().pipe(
map( map((stateResource: StateResource<PostfachResource>) => stateResource.loading),
(stateResource: StateResource<PostfachResource | ProblemDetail>) => stateResource.loading,
),
); );
} }
} }
import { HttpError, StateResource } from '@alfa-client/tech-shared'; import { StateResource } from '@alfa-client/tech-shared';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { SettingName } from '../admin-settings.model'; import { SettingName } from '../admin-settings.model';
...@@ -13,7 +13,7 @@ export class PostfachService { ...@@ -13,7 +13,7 @@ export class PostfachService {
return this.postfachResourceService.get(); 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)); return this.postfachResourceService.save(this.buildPostfachSettingItem(postfach));
} }
......
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;
}),
);
}
}
...@@ -18,7 +18,7 @@ export class ResourceLoader<B extends Resource, T extends Resource> { ...@@ -18,7 +18,7 @@ export class ResourceLoader<B extends Resource, T extends Resource> {
constructor( constructor(
private config: ServiceConfig<B>, private config: ServiceConfig<B>,
private resourceStateService: ResourceStateService<T>, private resourceStateService: ResourceStateService<B, T>,
) {} ) {}
public get(): Observable<StateResource<T>> { public get(): Observable<StateResource<T>> {
......
...@@ -48,13 +48,13 @@ export abstract class AbstractFormService { ...@@ -48,13 +48,13 @@ export abstract class AbstractFormService {
protected abstract initForm(): UntypedFormGroup; protected abstract initForm(): UntypedFormGroup;
public submit(): Observable<StateResource<Resource | HttpError>> { public submit(): Observable<StateResource<Resource>> {
return this.doSubmit().pipe(map((result) => this.handleResponse(result))); 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 (result.loading) return result;
if (hasStateResourceError(result)) { if (hasStateResourceError(result)) {
this.handleError(result.error); this.handleError(result.error);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment