All files / app/page/settings/me me.component.ts

56.25% Statements 27/48
68.75% Branches 11/16
18.18% Functions 2/11
56.41% Lines 22/39

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    1x             1x 1x 1x 1x             1x 1x                 3x         1x   1x         1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x                                                                             1x    
import { Location } from '@angular/common';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, isDevMode, ViewChild } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, UntypedFormGroup } from '@angular/forms';
import { cloneDeep, defer } from 'lodash-es';
import { runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { catchError, Subscription, switchMap, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { LoadingComponent } from '../../../component/loading/loading.component';
import { UserTagSelectorComponent } from '../../../component/user-tag-selector/user-tag-selector.component';
import { LimitWidthDirective } from '../../../directive/limit-width.directive';
import { extForm, ExtFormComponent } from '../../../form/ext/ext.component';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { AccountService } from '../../../service/account.service';
import { AdminService } from '../../../service/admin.service';
import { ExtService } from '../../../service/api/ext.service';
import { ConfigService } from '../../../service/config.service';
import { Store } from '../../../store/store';
import { scrollToFirstInvalid } from '../../../util/form';
import { printError } from '../../../util/http';
 
@Component({
  selector: 'app-settings-me-page',
  templateUrl: './me.component.html',
  styleUrls: ['./me.component.scss'],
  host: { 'class': 'full-page-form' },
  imports: [MobxAngularModule, ReactiveFormsModule, LimitWidthDirective, UserTagSelectorComponent, ExtFormComponent, LoadingComponent]
})
export class SettingsMePage implements HasChanges {
 
  @ViewChild('form')
  form?: ExtFormComponent;
 
  submitted = false;
  editForm!: UntypedFormGroup;
  serverError: string[] = [];
 
  editing?: Subscription;
 
  constructor(
    public config: ConfigService,
    public store: Store,
    private exts: ExtService,
    private accounts: AccountService,
    private admin: AdminService,
    private fb: FormBuilder,
    private location: Location,
  ) {
    const ext = cloneDeep(store.account.ext!);
    this.editForm = extForm(fb, ext, this.admin, true);
    this.editForm.patchValue(ext);
    Iif (ext) defer(() => this.form!.setValue(ext));
  }
 
  saveChanges() {
    return !this.editForm?.dirty;
  }
 
  save() {
    this.serverError = [];
    this.submitted = true;
    this.editForm.markAllAsTouched();
    if (!this.editForm.valid) {
      scrollToFirstInvalid();
      return;
    }
    const ext = this.store.account.ext!;
    this.editing = this.exts.update({
      ...ext,
      ...this.editForm.value,
      tag: ext.tag, // Need to fetch because control is disabled
      config: {
        ...ext.config,
        ...this.editForm.value.config,
      },
    }).pipe(
      tap(() => runInAction(() => this.accounts.clearCache())),
      switchMap(() => this.accounts.initExt$),
      catchError((res: HttpErrorResponse) => {
        delete this.editing;
        this.serverError = printError(res);
        return throwError(() => res);
      }),
    ).subscribe(() => {
      delete this.editing;
      this.editForm.markAsPristine();
      this.location.back();
    });
  }
 
  protected readonly isDevMode = isDevMode;
}