All files / app/form/user user.component.ts

53.19% Statements 25/47
34.88% Branches 15/43
46.15% Functions 6/13
50% Lines 20/40

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 14772x                   72x   72x   72x 72x 72x                           72x         2x       2x   2x   2x   2x                               2x 2x   2x     2x       2x               2x       8x                                                                                                 2x                            
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import {
  ReactiveFormsModule,
  UntypedFormBuilder,
  UntypedFormControl,
  UntypedFormGroup,
  Validators
} from '@angular/forms';
import { defer } from 'lodash-es';
import { v4 as uuid } from 'uuid';
import { FillWidthDirective } from '../../directive/fill-width.directive';
import { User } from '../../model/user';
import { isMailbox } from '../../mods/mailbox';
import { Store } from '../../store/store';
import { USER_REGEX } from '../../util/format';
import { JsonComponent } from '../json/json.component';
import { TagsFormComponent } from '../tags/tags.component';
 
@Component({
  selector: 'app-user-form',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss'],
  host: { 'class': 'nested-form' },
  imports: [
    ReactiveFormsModule,
    TagsFormComponent,
    FillWidthDirective,
    JsonComponent,
  ]
})
export class UserFormComponent implements OnInit {
 
  @Input()
  group!: UntypedFormGroup;
  @Input()
  showPubKey = true;
  @Input()
  fillWidth?: HTMLElement;
  @Output()
  tagChanges = new EventEmitter<string>();
  @Input()
  showClear = false;
  @Output()
  clear = new EventEmitter<void>();
  @Input()
  externalErrors: string[] = [];
 
  @ViewChild('fill')
  fill?: ElementRef;
 
  @ViewChild('notifications')
  notifications!: TagsFormComponent;
  @ViewChild('readAccess')
  readAccess!: TagsFormComponent;
  @ViewChild('writeAccess')
  writeAccess!: TagsFormComponent;
  @ViewChild('tagReadAccess')
  tagReadAccess!: TagsFormComponent;
  @ViewChild('tagWriteAccess')
  tagWriteAccess!: TagsFormComponent;
 
  id = 'user-' + uuid();
  editingExternal = false;
 
  private showedError = false;
 
  constructor(
    public store: Store,
  ) { }
 
  ngOnInit(): void {
    this.pubKey.disable();
  }
 
  get tag() {
    return this.group.get('tag') as UntypedFormControl;
  }
 
  get pubKey() {
    return this.group.get('pubKey') as UntypedFormControl;
  }
 
  get external() {
    return this.editingExternal ||= this.group.get('external')?.value;
  }
 
  get showError() {
    return this.tag.touched && this.tag.errors;
  }
 
  validate(input: HTMLInputElement) {
    if (this.showError) {
      if (this.tag.errors?.['required']) {
        input.setCustomValidity($localize`Tag must not be blank.`);
        input.reportValidity();
      }
      if (this.tag.errors?.['pattern']) {
        input.setCustomValidity($localize`
          User tags must start with the "+user/" or "_user/" prefix.
          Tags must be lower case letters and forward slashes. Must not start with a slash or contain two forward slashes in a row. Private
          tags start with an underscore.
          (i.e. "+user/alice", "_user/bob", or "+user/department/charlie")`);
        input.reportValidity();
      }
    }
  }
 
  blur(input: HTMLInputElement) {
    if (this.showError && !this.showedError) {
      this.showedError = true;
      defer(() => this.validate(input));
    } else {
      this.showedError = false;
      this.tagChanges.next(input.value)
    }
  }
 
  setUser(user: User) {
    this.notifications.setTags((user.readAccess || []).filter(isMailbox));
    this.readAccess.setTags((user.readAccess || []).filter(t => !isMailbox(t)));
    this.writeAccess.setTags([...user.writeAccess || []]);
    this.tagReadAccess.setTags([...user.tagReadAccess || []]);
    this.tagWriteAccess.setTags([...user.tagWriteAccess || []]);
    this.group.patchValue({
      ...user,
      external: user.external ? JSON.stringify(user.external, null, 2) : undefined,
    });
  }
 
}
 
export function userForm(fb: UntypedFormBuilder, locked = false) {
  return fb.group({
    tag: [{value: '', disabled: locked}, [Validators.required, Validators.pattern(USER_REGEX)]],
    name: [''],
    role: [''],
    notifications: fb.array([]),
    readAccess: fb.array([]),
    writeAccess: fb.array([]),
    tagReadAccess: fb.array([]),
    tagWriteAccess: fb.array([]),
    pubKey: ['', { disabled: true }],
    authorizedKeys: [''],
    external: [],
  });
}