| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { BaseSandbox } from '@shared/sandbox/base.sandbox'; |
| import { Injectable, OnDestroy } from '@angular/core'; |
| import * as externalPersonActions from '@app/shared/store/actions/persons/external-person.action'; |
| import { FormGroupState, MarkAsTouchedAction, SetValueAction, ResetAction, MarkAsUntouchedAction } from 'ngrx-forms'; |
| import { ExternalPerson } from '@app/shared/models'; |
| import { Observable, Subscription } from 'rxjs'; |
| import * as store from '@shared/store'; |
| import { Store, ActionsSubject } from '@ngrx/store'; |
| import * as externalPersonDetailsFormReducer from '@shared/store/reducers/persons/external-person/external-person-details-form.reducer'; |
| import { ofType } from '@ngrx/effects'; |
| import { UtilService } from '@app/shared/utility'; |
| import { takeUntil } from 'rxjs/operators'; |
| import { Router } from '@angular/router'; |
| |
| @Injectable() |
| export class ExternalPersonDetailsSandBox extends BaseSandbox implements OnDestroy { |
| |
| public formState$: Observable<FormGroupState<ExternalPerson>> = this.appState$.select(store.getExternalPersonDetails); |
| |
| public currentFormState: FormGroupState<ExternalPerson>; |
| private subscriptions: Array<Subscription> = []; |
| |
| public salutations$ = this.appState$.select(store.getSalutationsData); |
| |
| /** |
| * ExternalPerson Sandbox constructor |
| */ |
| constructor( |
| public appState$: Store<store.State>, |
| public utilService: UtilService, |
| protected actionsSubject: ActionsSubject, |
| protected router: Router, |
| ) { |
| super(appState$); |
| this.registerEvents(); |
| } |
| |
| /** |
| * Loads external person from the server |
| */ |
| public loadExternalPerson(id: string): void { |
| this.appState$.dispatch( |
| externalPersonActions.loadExternalPersonDetail({ payload: id }) |
| ); |
| } |
| |
| /** |
| * Persists an external person |
| */ |
| public persistExternalPerson(newExternalPerson: ExternalPerson): void { |
| this.appState$.dispatch( |
| externalPersonActions.persistExternalPersonDetail({ |
| payload: newExternalPerson |
| }) |
| ); |
| this.actionsSubject |
| .pipe( |
| ofType(externalPersonActions.persistExternalPersonDetailSuccess), |
| takeUntil(this._endSubscriptions$) |
| ) |
| .subscribe(() => { |
| this.router.navigateByUrl(`/overview`); |
| }); |
| this.subscriptions.push( |
| this.actionsSubject |
| .pipe(ofType(externalPersonActions.persistExternalPersonDetailSuccess)) |
| .subscribe(() => { |
| this.clear(); |
| }) |
| ); |
| } |
| |
| /** |
| * Clear |
| */ |
| clear(): void { |
| this.appState$.dispatch( |
| new SetValueAction( |
| externalPersonDetailsFormReducer.FORM_ID, |
| externalPersonDetailsFormReducer.INITIAL_STATE.value |
| ) |
| ); |
| this.appState$.dispatch(new ResetAction(externalPersonDetailsFormReducer.FORM_ID)); |
| } |
| |
| /** |
| * Clean up before leaving |
| */ |
| public ngOnDestroy() { |
| this.unregisterEvents(); |
| } |
| |
| /** |
| * Unsubscribes from events |
| */ |
| private unregisterEvents() { |
| this.subscriptions.forEach(sub => sub.unsubscribe()); |
| } |
| |
| public updatingState() { |
| this.appState$.dispatch(new MarkAsTouchedAction(externalPersonDetailsFormReducer.FORM_ID)); |
| } |
| |
| /** |
| * Subscribes to events |
| */ |
| private registerEvents(): void { |
| |
| // subscribes to formState |
| this.subscriptions.push( |
| this.formState$.subscribe( |
| (formState: FormGroupState<ExternalPerson>) => |
| (this.currentFormState = formState) |
| ) |
| ); |
| } |
| } |