| /******************************************************************************** |
| * 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, OnChanges, OnDestroy, OnInit, SimpleChanges} from "@angular/core"; |
| import {select, Store} from "@ngrx/store"; |
| import {BehaviorSubject, combineLatest} from "rxjs"; |
| import {delay, filter, switchMap, take, takeUntil} from "rxjs/operators"; |
| import {AUTO_SELECTED_TAGS, IAPIAttachmentModel} from "../../../../core/api/attachments"; |
| import {IAPIEmailAttachmentModel, IAPIEmailModel} from "../../../../core/api/mail"; |
| import { |
| clearAttachmentCacheAction, |
| createAttachmentForm, |
| fetchAttachmentTagsAction, |
| getAllStatementAttachments, |
| getAttachmentControlValueSelector, |
| getFilteredAttachmentTagsSelector, |
| getStatementAttachmentCacheSelector, |
| IAttachmentFormValue, |
| queryParamsIdSelector, |
| startAttachmentDownloadAction |
| } from "../../../../store"; |
| import {downloadEmailAttachmentAction} from "../../../../store/mail/actions"; |
| import {getSelectedEmailSelector, getStatementMailSelector} from "../../../../store/mail/selectors"; |
| import {arrayJoin} from "../../../../util/store"; |
| import {AbstractReactiveFormComponent} from "../../abstract"; |
| |
| @Component({ |
| selector: "app-attachments-form-group", |
| templateUrl: "./attachments-form-group.component.html", |
| styleUrls: ["./attachments-form-group.component.scss"] |
| }) |
| export class AttachmentsFormGroupComponent |
| extends AbstractReactiveFormComponent<IAttachmentFormValue> |
| implements OnInit, OnChanges, OnDestroy { |
| |
| @Input() |
| public appAutoTagIds: string[]; |
| |
| @Input() |
| public appForbiddenTagIds: string[]; |
| |
| @Input() |
| public appRestrictedTagIds: string[]; |
| |
| @Input() |
| public appWithoutTagControl: boolean; |
| |
| @Input() |
| public appCollapsed: boolean; |
| |
| @Input() |
| public appFormGroup = createAttachmentForm(); |
| |
| @Input() |
| public appForNewStatement: boolean; |
| |
| @Input() |
| public appMailId: string; |
| |
| @Input() |
| public appDisabled: boolean; |
| |
| public mail: IAPIEmailModel; |
| |
| public statementId$ = this.store.pipe(select(queryParamsIdSelector)); |
| |
| public selectedMail$ = this.store.pipe(select(getSelectedEmailSelector)); |
| |
| public statementMail$ = this.store.pipe(select(getStatementMailSelector)); |
| |
| public attachments$ = this.store.pipe(select(getAttachmentControlValueSelector, {})); |
| |
| public allAttachments$ = this.store.pipe(select(getAllStatementAttachments)); |
| |
| public fileCache$ = this.store.pipe(select(getStatementAttachmentCacheSelector)); |
| |
| public tagList$ = this.store.pipe(select(getFilteredAttachmentTagsSelector, {without: AUTO_SELECTED_TAGS})); |
| |
| private attachmentProps$ = new BehaviorSubject<{ restrictedTagIds: string[], forbiddenTagIds: string[] }>({ |
| restrictedTagIds: [], |
| forbiddenTagIds: [] |
| }); |
| |
| public constructor(public store: Store) { |
| super(); |
| } |
| |
| public ngOnInit() { |
| this.attachments$ = this.attachmentProps$.pipe( |
| switchMap((props) => this.store.pipe( |
| select(getAttachmentControlValueSelector, props) |
| )) |
| ); |
| |
| this.attachments$.pipe(delay(0), takeUntil(this.destroy$)) |
| .subscribe((values) => this.setValueForArray(values, "edit")); |
| |
| combineLatest([this.selectedMail$, this.statementMail$, this.allAttachments$]).pipe( |
| filter(([_, m, a]) => (_ != null || m != null) && a != null), |
| delay(0), |
| takeUntil(this.destroy$) |
| ).subscribe(async ([selectedMail, statementMail, attachments]) => { |
| this.setMailTextAttachmentValue(attachments, this.appForNewStatement); |
| this.mail = selectedMail ? selectedMail : statementMail; |
| this.setMailAttachmentValues(attachments, arrayJoin(this.mail?.attachments)); |
| }); |
| |
| |
| this.fileCache$.pipe(delay(0), takeUntil(this.destroy$)) |
| .subscribe((values) => this.setValueForArray(values, "add")); |
| this.store.dispatch(fetchAttachmentTagsAction()); |
| } |
| |
| public ngOnChanges(changes: SimpleChanges) { |
| const keys: Array<keyof AttachmentsFormGroupComponent> = ["appRestrictedTagIds", "appForbiddenTagIds"]; |
| if (keys.some((_) => changes[_] != null)) { |
| this.attachmentProps$.next({ |
| restrictedTagIds: this.appRestrictedTagIds, |
| forbiddenTagIds: this.appForbiddenTagIds |
| }); |
| } |
| } |
| |
| public ngOnDestroy() { |
| this.statementId$.pipe(take(1)) |
| .subscribe((statementId) => this.store.dispatch(clearAttachmentCacheAction({statementId}))); |
| super.ngOnDestroy(); |
| } |
| |
| public async downloadAttachment(attachmentId: number) { |
| const statementId = await this.statementId$.pipe(take(1)).toPromise(); |
| this.store.dispatch(startAttachmentDownloadAction({statementId, attachmentId})); |
| } |
| |
| public async downloadEmailAttachment(attachmentId: string | number) { |
| if (typeof (attachmentId) === "number") { |
| this.downloadAttachment(attachmentId); |
| } else { |
| this.store.dispatch(downloadEmailAttachmentAction({mailId: this.appMailId, name: attachmentId})); |
| } |
| } |
| |
| public setMailTextAttachmentValue(attachments: IAPIAttachmentModel[], isNewStatement: boolean) { |
| const mailTextAttachmentId = attachments.find((_) => |
| _.name === "mailText.txt" && _.tagIds && _.tagIds.length === 2 |
| && _.tagIds[0] === "email" && _.tagIds[1] === "email-text")?.id; |
| |
| this.appFormGroup.patchValue({mailTextAttachmentId, transferMailText: mailTextAttachmentId != null || isNewStatement}); |
| } |
| |
| public setMailAttachmentValues(attachments: IAPIAttachmentModel[], emailAttachments: IAPIEmailAttachmentModel[]) { |
| const mergedAttachmentLists = emailAttachments.map((emailAttachment) => { |
| const emailAttachmentFromAttachmentsArray = attachments.find((attachment) => |
| attachment.name === emailAttachment.name && attachment.tagIds.find((_) => _ === "email") != null); |
| |
| const isSelected = emailAttachmentFromAttachmentsArray != null || this.appForNewStatement; |
| |
| const tagIds = emailAttachmentFromAttachmentsArray?.tagIds; |
| |
| return { |
| ...(emailAttachmentFromAttachmentsArray ? emailAttachmentFromAttachmentsArray : emailAttachment), |
| isSelected, |
| tagIds: arrayJoin(tagIds) |
| }; |
| }); |
| this.setValueForArray(mergedAttachmentLists, "email"); |
| } |
| |
| } |