All files / app/component/todo todo.component.ts

34.24% Statements 25/73
20.75% Branches 11/53
14.28% Functions 3/21
31.03% Lines 18/58

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 16067x                                     67x                           67x         1x   1x       1x   1x   1x 1x 1x 1x 1x             1x 1x 1x 1x   1x                                                           2x                                                                                                                                  
import { CdkDrag, CdkDragDrop, CdkDropList } from '@angular/cdk/drag-drop';
import {
  Component,
  EventEmitter,
  HostBinding,
  HostListener,
  Input,
  NgZone,
  OnChanges,
  Output,
  SimpleChanges
} from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { catchError, Observable, of, Subscription, switchMap, throwError, timer } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Ref } from '../../model/ref';
import { ActionService } from '../../service/action.service';
import { ConfigService } from '../../service/config.service';
import { Store } from '../../store/store';
import { TodoItemComponent } from './item/item.component';
 
@Component({
  selector: 'app-todo',
  templateUrl: './todo.component.html',
  styleUrls: ['./todo.component.scss'],
  host: { 'class': 'todo-list' },
  imports: [
    CdkDropList,
    CdkDrag,
    ReactiveFormsModule,
    TodoItemComponent,
  ]
})
export class TodoComponent implements OnChanges {
 
  @Input()
  ref?: Ref;
  @Input()
  text? = '';
  @Input()
  origin = '';
  @Input()
  tags?: string[];
  @Output()
  comment = new EventEmitter<string>();
  @Output()
  copied = new EventEmitter<string>();
 
  lines: string[] = [];
  addText = '';
  pushText: string[] = [];
  pressToUnlock = false;
  serverErrors: string[] = [];
 
  private watch?: Subscription;
  private pushing?: Subscription;
  private comment$!: (comment: string) => Observable<string>;
 
  constructor(
    public config: ConfigService,
    private store: Store,
    private actions: ActionService,
    private zone: NgZone,
  ) {
    Iif (config.mobile) {
      this.pressToUnlock = true;
    }
  }
 
  init() {
    this.lines = (this.ref?.comment || this.text || '').split('\n')?.filter(l => !!l) || [];
    if (!this.watch && this.ref) {
      const watch = this.actions.watch(this.ref);
      this.comment$ = watch.comment$;
      this.watch = watch.ref$.subscribe(update => {
        this.ref!.comment = update.comment;
        this.init();
      });
    }
  }
 
  ngOnChanges(changes: SimpleChanges) {
    if (changes.ref || changes.text) {
      this.init();
    }
  }
 
  @HostListener('touchstart', ['$event'])
  touchstart(e: TouchEvent) {
    this.zone.run(() => this.pressToUnlock = true);
  }
 
  @HostBinding('class.empty')
  get empty() {
    return !this.lines.length;
  }
 
  get local() {
    return this.ref?.origin === this.store.account.origin;
  }
 
  drop(event: CdkDragDrop<string, string, string>) {
    if (event.previousContainer.data === event.container.data) {
      this.lines.splice(event.previousIndex, 1);
    } else {
      // TODO: Delete from prev
    }
    this.lines.splice(event.currentIndex, 0, event.item.data);
    this.save$(this.lines.join('\n'))?.subscribe();
  }
 
  update(line: {index: number, text: string, checked: boolean}) {
    if (!line.text) {
      this.lines.splice(line.index, 1);
    } else {
      this.lines[line.index] = `- [${line.checked ? 'X' : ' '}] ${line.text}`;
    }
    this.save$(this.lines.join('\n'))?.subscribe();
  }
 
  save$(comment: string) {
    this.comment.emit(comment);
    if (!this.ref) return of();
    return this.comment$(comment).pipe(
      tap(() => {
        if (!this.local) {
          this.copied.emit(this.store.account.origin);
          this.store.eventBus.refresh(this.ref);
        }
      }),
    );
  }
 
  add(cancel?: Event) {
    cancel?.preventDefault();
    this.addText = this.addText.trim();
    if (!this.addText) return;
    this.pushText.push(`- [ ] ${this.addText}`);
    this.addText = '';
    if (!this.pushing) this.pushing = this.push$().subscribe();
  }
 
  push$(): Observable<string> {
    const lines = [...this.pushText];
    return this.save$([...this.lines, ...lines].join('\n')).pipe(
      catchError((err: any) => {
        if (err.conflict) {
          return timer(100).pipe(switchMap(() => this.push$()));
        }
        return throwError(() => err);
      }),
      tap(() => {
        this.pushText = this.pushText.slice(lines.length);
        delete this.pushing;
        if (this.pushText.length) this.pushing = this.push$().subscribe();
      }),
    );
  }
}