| /******************************************************************************** |
| * 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 } from '@angular/core'; |
| import * as companyActions from '@shared/store/actions/company/company.action'; |
| import { FormGroupState, SetValueAction, ResetAction } from 'ngrx-forms'; |
| import { Company, CommunicationsData, CommunicationType } from '@shared/models'; |
| import { Observable } from 'rxjs'; |
| import * as store from '@shared/store'; |
| import { Store, ActionsSubject } from '@ngrx/store'; |
| import * as companyDetailsFormReducer from '@shared/store/reducers/company/company-details-form.reducer'; |
| import { ofType } from '@ngrx/effects'; |
| import { UtilService } from '@shared/utility'; |
| import { takeUntil, take, map } from 'rxjs/operators'; |
| import { Router } from '@angular/router'; |
| import { SafetyQueryDialogComponent } from '@shared/components/dialogs/safety-query-dialog/safety-query-dialog.component'; |
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| import * as companyCommunicationsDataDetailsFormReducer from '@shared/store/reducers/company/communications-data-details-form.reducer'; |
| import * as communicationTypesActions from '@shared/store/actions/admin/communication-types.action'; |
| import { MarkAsTouchedAction } from 'ngrx-forms'; |
| |
| @Injectable() |
| export class CompanyDetailsSandBox extends BaseSandbox { |
| public formState$: Observable<FormGroupState<Company>> = this.appState$.select(store.getCompanyDetails); |
| public currentFormState: FormGroupState<Company>; |
| |
| public communicationsDataDetailsFormState$: Observable<FormGroupState<CommunicationsData>> = this.appState$.select(store.getCompanyCommunicationsDataDetails); |
| public communicationsDataDetailsCurrentFormState: FormGroupState<CommunicationsData>; |
| |
| public communicationsDataList$: Observable<Array<CommunicationsData>> = this.appState$.select(store.getCompanyCommunicationsDataData); |
| public communicationsDataListLoading$: Observable<boolean> = this.appState$.select(store.getCompanyCommunicationsDataLoading); |
| |
| public isCommunicationsDataDetailViewVisible: boolean = false; |
| |
| private _companyContactId: string; |
| |
| private _communicationTypes: Array<CommunicationType> = new Array<CommunicationType>(); |
| |
| /** |
| * Company Sandbox constructor |
| */ |
| constructor( |
| protected appState$: Store<store.State>, |
| protected actionsSubject: ActionsSubject, |
| public utilService: UtilService, |
| protected router: Router, |
| protected modalService: NgbModal |
| ) { |
| super(appState$); |
| } |
| |
| /** |
| * Loads company from the server |
| */ |
| public loadCompany(id: string): void { |
| this._companyContactId = id; |
| this.appState$.dispatch( |
| companyActions.loadCompanyDetail({ payload: id }) |
| ); |
| } |
| |
| /** |
| * Persists a company |
| */ |
| public persistCompany(): void { |
| if (this.currentFormState.isValid) { |
| const newCompany = new Company(this.currentFormState.value); |
| |
| this.appState$.dispatch( |
| companyActions.persistCompanyDetail({ |
| payload: newCompany, |
| }) |
| ); |
| this.actionsSubject |
| .pipe( |
| ofType(companyActions.persistCompanyDetailSuccess), |
| take(1), |
| takeUntil(this._endSubscriptions$) |
| ) |
| .subscribe(() => { |
| this.clearCompany(); |
| this.router.navigateByUrl(`/overview`); |
| }); |
| } else { |
| this.utilService.displayNotification('MandatoryFieldsNotFilled', 'error'); |
| } |
| } |
| |
| public clearCompany(): void { |
| this.appState$.dispatch(new SetValueAction(companyDetailsFormReducer.FORM_ID, companyDetailsFormReducer.INITIAL_STATE.value)); |
| this.appState$.dispatch(new ResetAction(companyDetailsFormReducer.FORM_ID)); |
| |
| this.appState$.dispatch(new MarkAsTouchedAction(companyDetailsFormReducer.FORM_ID)); |
| } |
| |
| /** |
| * Subscribes to events |
| */ |
| public registerEvents(): void { |
| // subscribes to formState |
| this.formState$.pipe(takeUntil(this._endSubscriptions$)).subscribe((formState: FormGroupState<Company>) => (this.currentFormState = formState)); |
| } |
| |
| public loadCommunicationsData(companyId: string): void { |
| this.actionsSubject |
| .pipe( |
| ofType(communicationTypesActions.loadCommunicationTypesSuccess), |
| map((action: communicationTypesActions.ILoadCommunicationTypesSuccess) => action.payload), |
| take(1), |
| takeUntil(this._endSubscriptions$) |
| ) |
| .subscribe((payload: Array<CommunicationType>) => { |
| this._communicationTypes = payload; |
| this.appState$.dispatch(companyActions.loadCompanyDetailCommunicationsData({ payload: companyId })); |
| }); |
| |
| this.actionsSubject |
| .pipe( |
| ofType(companyActions.loadCompanyDetailCommunicationsDataSuccess), |
| map((action: companyActions.ILoadCompanyCommunicationsDataSuccess) => action.payload), |
| take(1), |
| takeUntil(this._endSubscriptions$) |
| ) |
| .subscribe((communicationsData: Array<CommunicationsData>) => { |
| if (this._communicationTypes) { |
| for (let i = 0; i < this._communicationTypes.length; i++) { |
| const ct = this._communicationTypes[i]; |
| const existingCommunicationsData: CommunicationsData = communicationsData.find(cd => cd.communicationTypeId == ct.id); |
| ct.isDisabled = existingCommunicationsData ? true : false; |
| } |
| } |
| }); |
| } |
| |
| public loadCommunicationsDataDetails(communicationsDataId: string): void { |
| this.appState$.dispatch( |
| companyActions.loadCompanyDetailCommunicationsDataDetails({ payload_contactId: this._companyContactId, payload_communicationsId: communicationsDataId }) |
| ); |
| } |
| |
| public persistCommunicationsData(): void { |
| if (this.communicationsDataDetailsCurrentFormState.isValid) { |
| const newCommunicationsData = new CommunicationsData(this.communicationsDataDetailsCurrentFormState.value); |
| newCommunicationsData.contactId = newCommunicationsData.contactId !== null ? newCommunicationsData.contactId : this._companyContactId; |
| |
| this.appState$.dispatch( |
| companyActions.persistCommunicationsDataDetail({payload: newCommunicationsData}) |
| ); |
| this.actionsSubject |
| .pipe( |
| ofType(companyActions.persistCommunicationsDataDetailSuccess), |
| take(1), |
| takeUntil(this._endSubscriptions$) |
| ) |
| .subscribe(() => { |
| this.closeCommunicationsDataDetail(); |
| }); |
| } else { |
| this.utilService.displayNotification( |
| 'MandatoryFieldsNotFilled', |
| 'error' |
| ); |
| } |
| } |
| |
| public deleteCommunicationsData(communicationsData: CommunicationsData): void { |
| const modalRef = this.modalService.open(SafetyQueryDialogComponent); |
| modalRef.componentInstance.title = 'ConfirmDialog.Action.delete'; |
| modalRef.componentInstance.body = 'ConfirmDialog.Deletion'; |
| modalRef.result.then(() => { |
| this.appState$.dispatch(companyActions.deleteCommunicationsData({ payload: communicationsData })); |
| this.closeCommunicationsDataDetail(); |
| }, () => { |
| }); |
| } |
| |
| public closeCommunicationsDataDetail(): void { |
| this.loadCommunicationsData(this._companyContactId); |
| this.clearCommunicationsData(); |
| this.isCommunicationsDataDetailViewVisible = false; |
| } |
| |
| public clearCommunicationsData(): void { |
| this.appState$.dispatch( |
| new SetValueAction( |
| companyCommunicationsDataDetailsFormReducer.FORM_ID, |
| companyCommunicationsDataDetailsFormReducer.INITIAL_STATE.value |
| ) |
| ); |
| this.appState$.dispatch(new ResetAction(companyCommunicationsDataDetailsFormReducer.FORM_ID)); |
| } |
| |
| /** |
| * Subscribes to communicationsData events |
| */ |
| public registerCommunicationsDataEvents(): void { |
| // subscribes to formState |
| this.communicationsDataDetailsFormState$.pipe(takeUntil(this._endSubscriptions$)).subscribe( |
| (formState: FormGroupState<CommunicationsData>) => |
| (this.communicationsDataDetailsCurrentFormState = formState) |
| ); |
| } |
| } |