blob: fac63abae965e45c13d54a6bd893d59b4ea5db05 [file] [log] [blame]
/********************************************************************************
* 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 { Injectable } from '@angular/core';
import { Store, ActionsSubject } from '@ngrx/store';
import { FormGroupState, NgrxValueConverter, SetValueAction, ResetAction } from 'ngrx-forms';
import * as store from '@shared/store';
import * as gridFailuresDetailFormReducer from '@shared/store/reducers/grid-failures/grid-failure-details-form.reducer';
import { Observable } from 'rxjs';
import { ILoadGridFailuresSuccess, ILoadGridFailureSuccess } from '@shared/store/actions/grid-failures.action';
import * as gridFailureActions from '@shared/store/actions/grid-failures.action';
import { takeUntil, take } from 'rxjs/operators';
import { ofType } from '@ngrx/effects';
import { Router } from '@angular/router';
import { GridFailure } from '@shared/models/grid-failure.model';
import { dateValueConverter } from '@shared/utility';
import { Moment } from 'moment';
import { BaseSandbox } from '@shared/sandbox/base.sandbox';
import { UtilService } from '@shared/utility/utility.service';
import * as fromGridFailuresDetailFormReducer from '@shared/store/reducers/grid-failures/grid-failure-details-form.reducer';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { SafetyQueryDialogComponent } from '@shared/components/dialogs/safety-query-dialog/safety-query-dialog.component';
import { navigateHome } from '@shared/utility';
import { DatePickerResetEnum } from '@shared/constants/enums';
@Injectable()
export class GridFailureSandbox extends BaseSandbox {
public gridFailureList$: Observable<GridFailure[]> = this.appState$.select(store.getGridFailuresData);
public gridFailureListLoading$: Observable<boolean> = this.appState$.select(store.getGridFailuresLoading);
public gridFailureDetailsFormState$: Observable<FormGroupState<GridFailure>> = this.appState$.select(store.getGridFailuresDetails);
public currentFormState: FormGroupState<GridFailure>;
public datePickerResetEnum = DatePickerResetEnum;
constructor(
protected appState$: Store<store.State>,
private _actionsSubject: ActionsSubject,
private _router: Router,
private _utilService: UtilService,
private _modalService: NgbModal
) {
super(appState$);
}
public dateValueConverter: NgrxValueConverter<Date | null | Moment, string | null> = dateValueConverter;
public loadGridFailures(): Observable<ILoadGridFailuresSuccess> {
this.appState$.dispatch(gridFailureActions.loadGridFailures());
return this._actionsSubject.pipe(ofType(gridFailureActions.loadGridFailuresSuccess), take(1));
}
public loadGridFailure(gridFailureId: string): Observable<ILoadGridFailureSuccess> {
this.appState$.dispatch(gridFailureActions.loadGridFailureDetail({ payload: gridFailureId }));
return this._actionsSubject.pipe(ofType(gridFailureActions.loadGridFailureDetailSuccess), take(1));
}
public createGridFailure() {
this.appState$.dispatch(new SetValueAction(fromGridFailuresDetailFormReducer.FORM_ID, fromGridFailuresDetailFormReducer.INITIAL_STATE.value));
this.appState$.dispatch(new ResetAction(fromGridFailuresDetailFormReducer.FORM_ID));
this._router.navigateByUrl('/grid-failures/new');
}
public clearGridFailurePropertyData(property: string): void {
switch (property) {
case this.datePickerResetEnum.FailureBegin:
this.appState$.dispatch(new SetValueAction(gridFailuresDetailFormReducer.INITIAL_STATE.controls.failureBegin.id, null));
break;
case this.datePickerResetEnum.FailureEndPlanned:
this.appState$.dispatch(new SetValueAction(gridFailuresDetailFormReducer.INITIAL_STATE.controls.failureEndPlanned.id, null));
break;
case this.datePickerResetEnum.FailureEndResupplied:
this.appState$.dispatch(new SetValueAction(gridFailuresDetailFormReducer.INITIAL_STATE.controls.failureEndResupplied.id, null));
break;
default:
break;
}
}
public cancel(): void {
if (!this.currentFormState.isPristine) {
const modalRef = this._modalService.open(SafetyQueryDialogComponent);
modalRef.componentInstance.title = 'ConfirmDialog.Action.edit';
modalRef.componentInstance.body = 'ConfirmDialog.Content';
modalRef.result.then(
() => {
this.clear();
},
() => {}
);
} else {
this.clear();
}
}
clear(): void {
this.appState$.dispatch(new SetValueAction(fromGridFailuresDetailFormReducer.FORM_ID, fromGridFailuresDetailFormReducer.INITIAL_STATE.value));
this.appState$.dispatch(new ResetAction(fromGridFailuresDetailFormReducer.FORM_ID));
navigateHome(this._router);
}
public saveGridFailure(): void {
if (this.currentFormState.isValid) {
this.appState$.dispatch(
gridFailureActions.saveGridFailure({
payload: new GridFailure(this.currentFormState.value),
})
);
this._actionsSubject.pipe(ofType(gridFailureActions.saveGridFailureSuccess), take(1), takeUntil(this._endSubscriptions$)).subscribe(() => {
this.clear();
});
} else {
this._utilService.displayNotification('MandatoryFieldError', 'alert');
}
}
public registerEvents(): void {
this.gridFailureDetailsFormState$
.pipe(takeUntil(this._endSubscriptions$))
.subscribe((formState: FormGroupState<GridFailure>) => (this.currentFormState = formState));
}
}