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

55.88% Statements 38/68
47.05% Branches 16/34
43.47% Functions 10/23
58.33% Lines 35/60

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167      76x         76x             76x 76x   76x 76x                 179x       76x 5x   5x                       5x 5x 5x     5x 5x 5x 5x   5x                   5x         26x                     4x                                         4x 4x 1x           4x 4x 1x           4x 11x 11x 1x 4x 3x         4x             4x                                                          
import {
  HttpErrorResponse
} from '@angular/common/http';
import { DestroyRef, inject, AfterViewInit, Component, forwardRef, Input, ViewChild, ChangeDetectionStrategy } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { uniq, without } from 'lodash-es';
import { catchError, forkJoin, map, of, Subject, Subscription, switchMap, throwError } from 'rxjs';
import { EditorComponent } from '../../../form/editor/editor.component';
import { LinksFormComponent } from '../../../form/links/links.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' },
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [
    forwardRef(() => EditorComponent),
    LoadingComponent,
  ]
})
export class CommentEditComponent implements AfterViewInit, HasChanges {
  private destroyRef = inject(DestroyRef);
 
  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);
  }
 
 
  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),
    ]);
  }
 
  get top() {
    return this.ref.sources?.[1] || this.ref.sources?.[0] || this.ref.url;
  }
 
  addSource(value = '') {
    if ((this.ref.sources?.length || 0) < 1) {
      this.sources.push(this.top);
    }
    if ((this.ref.sources?.length || 0) < 2) {
      this.sources.push(this.top);
    }
    this.sources.push(value);
  }
 
  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,
      });
    }
    const removeIndices = (this.ref.tags || [])
      .map((t, i) => finalTags.includes(t) ? -1 : i)
      .filter(i => i >= 0)
      .sort((a, b) => b - a);
    for (const i of removeIndices) {
      patches.push({
        op: 'remove',
        path: '/tags/' + i,
      });
    }
    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(takeUntilDestroyed(this.destroyRef))),
      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);
  }
}