All files / app/component/comment/comment-edit comment-edit.component.ts

61.29% Statements 38/62
63.63% Branches 14/22
42.1% Functions 8/19
63.46% Lines 33/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148  67x       67x           67x 67x   67x 67x               30x       67x 4x   4x                       4x 4x 4x     4x 4x 4x 4x   4x                   4x       4x 4x       21x                     3x             3x 3x 1x           3x 3x 1x           3x 1x         3x             3x                                                          
import { HttpErrorResponse } from '@angular/common/http';
import { AfterViewInit, Component, forwardRef, Input, OnDestroy, ViewChild } from '@angular/core';
import { FormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { uniq, without } from 'lodash-es';
import { catchError, forkJoin, map, of, Subject, Subscription, switchMap, takeUntil, throwError } from 'rxjs';
import { EditorComponent } from '../../../form/editor/editor.component';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { Ref } from '../../../model/ref';
import { RefService } from '../../../service/api/ref.service';
import { TaggingService } from '../../../service/api/tagging.service';
import { Store } from '../../../store/store';
import { getIfNew, getMailboxes } from '../../../util/editor';
import { printError } from '../../../util/http';
import { OpPatch } from '../../../util/json-patch';
import { getVisibilityTags } from '../../../util/tag';
import { LoadingComponent } from '../../loading/loading.component';
 
@Component({
  selector: 'app-comment-edit',
  templateUrl: './comment-edit.component.html',
  styleUrls: ['./comment-edit.component.scss'],
  host: { 'class': 'comment-edit' },
  imports: [
    forwardRef(() => EditorComponent),
    LoadingComponent,
  ]
})
export class CommentEditComponent implements AfterViewInit, HasChanges, OnDestroy {
  private destroy$ = new Subject<void>();
 
  serverError: string[] = [];
 
  @Input()
  ref!: Ref;
  @Input()
  commentEdited$!: Subject<Ref>;
 
  @ViewChild('editor')
  editor?: EditorComponent;
 
  editing?: Subscription;
  commentForm: UntypedFormGroup;
  editorTags: string[] = [];
  sources: string[] = [];
  completedUploads: Ref[] = [];
 
  constructor(
    private store: Store,
    private refs: RefService,
    private ts: TaggingService,
    private fb: FormBuilder,
  ) {
    this.commentForm = fb.group({
      comment: [''],
    });
  }
 
  saveChanges() {
    return !this.commentForm.dirty;
  }
 
  ngAfterViewInit() {
    this.comment.setValue(this.ref.comment);
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  get comment() {
    return this.commentForm.get('comment') as UntypedFormControl;
  }
 
  get newTags() {
    return getIfNew(uniq([
      ...this.editorTags,
      ...getMailboxes(this.comment.value, this.store.account.origin),
    ]), this.ref.tags);
  }
 
  get allTags() {
    return uniq([
      ...this.editorTags,
      ...getMailboxes(this.comment.value, this.store.account.origin),
    ]);
  }
 
  save() {
    const patches: OpPatch[] = [];
    if (this.comment.dirty) {
      patches.push({
        op: 'add',
        path: '/comment',
        value: this.comment.value,
      });
    }
    const finalTags = this.allTags;
    for (const t of without(finalTags, ...this.ref.tags || [])) {
      patches.push({
        op: 'add',
        path: '/tags/-',
        value: t,
      });
    }
    for (const t of without(this.ref.tags || [], ...finalTags)) {
      patches.push({
        op: 'remove',
        path: '/tags/' + this.ref.tags!.indexOf(t),
      });
    }
    for (const s of this.sources) {
      patches.push({
        op: 'add',
        path: '/sources/-',
        value: s,
      });
    }
    this.editing = this.refs.patch(this.ref.url, this.ref.origin!, this.ref!.modifiedString!, patches).pipe(
      switchMap(() => this.refs.get(this.ref.url, this.ref.origin!).pipe(takeUntil(this.destroy$))),
      switchMap(res => {
        const finalVisibilityTags = getVisibilityTags(finalTags);
        if (!finalVisibilityTags.length) return of(res);
        const taggingOps = this.completedUploads
          .map(upload => this.ts.patch(finalVisibilityTags, upload.url, upload.origin));
        if (!taggingOps.length) return of(res);
        return forkJoin(taggingOps).pipe(map(() => res));
      }),
      catchError((res: HttpErrorResponse) => {
        delete this.editing;
        this.serverError = printError(res);
        return throwError(() => res);
      }),
    ).subscribe(res => {
      delete this.editing;
      this.ref = res;
      this.completedUploads = [];
 
      this.commentEdited$.next(res);
    });
  }
 
  cancel() {
    this.editing?.unsubscribe();
    this.commentEdited$.next(this.ref);
  }
}