| /******************************************************************************** |
| * 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 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| |
| import {Component, Input, OnInit} from "@angular/core"; |
| import {select, Store} from "@ngrx/store"; |
| import {concat, defer, of} from "rxjs"; |
| import {distinctUntilChanged, filter, map, switchMap, take, takeUntil} from "rxjs/operators"; |
| import {AUTO_SELECTED_TAGS, IAPISearchOptions} from "../../../../../core"; |
| import { |
| createStatementInformationForm, |
| fetchContactDetailsAction, |
| fetchSettingsAction, |
| getContactDetailsSelector, |
| getContactLoadingSelector, |
| getContactSearchContentSelector, |
| getContactSearchSelector, |
| getStatementSectorsSelector, |
| IStatementInformationFormValue, |
| openContactDataBaseAction, |
| startContactSearchAction, |
| statementInformationFormValueSelector, |
| statementLoadingSelector, |
| statementTypesSelector, |
| submitStatementInformationFormAction, |
| taskSelector |
| } from "../../../../../store"; |
| import {arrayJoin} from "../../../../../util"; |
| import {AbstractReactiveFormComponent} from "../../../abstract"; |
| |
| @Component({ |
| selector: "app-statement-information-form", |
| templateUrl: "./statement-information-form.component.html", |
| styleUrls: ["./statement-information-form.component.scss"] |
| }) |
| export class StatementInformationFormComponent extends AbstractReactiveFormComponent<IStatementInformationFormValue> implements OnInit { |
| |
| @Input() |
| public appForbiddenTagIds = AUTO_SELECTED_TAGS; |
| |
| @Input() |
| public appForNewStatement: boolean; |
| |
| public statementLoading$ = this.store.pipe(select(statementLoadingSelector)); |
| |
| public task$ = this.store.pipe(select(taskSelector)); |
| |
| public statementTypeOptions$ = this.store.pipe(select(statementTypesSelector)); |
| |
| public contactSearch$ = this.store.pipe(select(getContactSearchSelector)); |
| |
| public contactSearchContent$ = this.store.pipe(select(getContactSearchContentSelector)); |
| |
| public contactLoading$ = this.store.pipe(select(getContactLoadingSelector)); |
| |
| public sectors$ = this.store.pipe(select(getStatementSectorsSelector)); |
| |
| public selectedContact$ = defer(() => this.selectedContactId$).pipe( |
| switchMap((id) => this.store.pipe(select(getContactDetailsSelector, {id}))) |
| ); |
| |
| public searchText: string; |
| |
| public appFormGroup = createStatementInformationForm(); |
| |
| public selectedContactId$ = defer(() => concat(of(null), this.appFormGroup.valueChanges)).pipe( |
| map(() => this.getValue().contactId) |
| ); |
| |
| private form$ = this.store.pipe(select(statementInformationFormValueSelector)); |
| |
| private searchSize = 10; |
| |
| public constructor(public store: Store) { |
| super(); |
| } |
| |
| public ngOnInit() { |
| if (this.appForNewStatement) { |
| this.setInitialValue(); |
| this.store.dispatch(fetchSettingsAction()); |
| } else { |
| this.appFormGroup.markAllAsTouched(); |
| } |
| |
| this.updateForm(); |
| this.fetchContactDetails(); |
| this.search(""); |
| } |
| |
| public openContactDataBaseModule() { |
| this.store.dispatch(openContactDataBaseAction()); |
| } |
| |
| public search(searchText?: string) { |
| this.searchText = searchText; |
| this.changePage(0); |
| } |
| |
| public changePage(page: number) { |
| const options: IAPISearchOptions = { |
| q: this.searchText == null ? "" : this.searchText, |
| page: page == null ? 0 : page, |
| size: this.searchSize |
| }; |
| this.store.dispatch(startContactSearchAction({options})); |
| } |
| |
| public async submit(responsible?: boolean) { |
| if (this.appFormGroup.invalid) { |
| this.appFormGroup.markAllAsTouched(); |
| return; |
| } |
| |
| if (this.appForNewStatement) { |
| this.store.dispatch(submitStatementInformationFormAction({ |
| new: true, |
| value: this.getValue(), |
| responsible |
| })); |
| } else { |
| const task = await this.task$.pipe(take(1)).toPromise(); |
| this.store.dispatch(submitStatementInformationFormAction({ |
| statementId: task.statementId, |
| taskId: task.taskId, |
| value: this.getValue(), |
| responsible |
| })); |
| } |
| } |
| |
| private async setInitialValue() { |
| const statementTypeOptions = await this.statementTypeOptions$.pipe(take(1)).toPromise(); |
| const typeId = arrayJoin(statementTypeOptions)[0]?.value; |
| const today = new Date().toISOString().slice(0, 10); |
| this.patchValue({typeId, dueDate: today, receiptDate: today}); |
| } |
| |
| private fetchContactDetails() { |
| this.selectedContactId$.pipe(distinctUntilChanged(), takeUntil(this.destroy$)) |
| .subscribe((contactId) => this.store.dispatch(fetchContactDetailsAction({contactId}))); |
| } |
| |
| private updateForm() { |
| this.form$.pipe(takeUntil(this.destroy$), filter(() => !this.appForNewStatement)).subscribe((value) => { |
| this.patchValue(value); |
| }); |
| this.statementLoading$.pipe(takeUntil(this.destroy$)).subscribe((loading) => { |
| loading ? this.appFormGroup.disable() : this.appFormGroup.enable(); |
| }); |
| } |
| |
| } |