Updated to angular 11
Now compiling with strict mode in typescript Need to error checking now
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
|
||||
|
||||
import { UserDetailComponent } from './user-detail.component';
|
||||
|
||||
@@ -6,11 +6,13 @@ describe('UserDetailComponent', () => {
|
||||
let component: UserDetailComponent;
|
||||
let fixture: ComponentFixture<UserDetailComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [UserDetailComponent],
|
||||
}).compileComponents();
|
||||
}));
|
||||
beforeEach(
|
||||
waitForAsync(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [UserDetailComponent],
|
||||
}).compileComponents();
|
||||
}),
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(UserDetailComponent);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { AbstractControl, FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
|
||||
@@ -14,9 +14,9 @@ import { UserService } from '../user.service';
|
||||
styleUrls: ['./user-detail.component.css'],
|
||||
})
|
||||
export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('nameElement', { static: true }) nameElement: ElementRef;
|
||||
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
||||
form: FormGroup;
|
||||
item: User;
|
||||
item: User = new User();
|
||||
hide: boolean;
|
||||
|
||||
constructor(
|
||||
@@ -28,10 +28,7 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
private ser: UserService,
|
||||
) {
|
||||
this.hide = true;
|
||||
this.createForm();
|
||||
}
|
||||
|
||||
createForm() {
|
||||
// Create form
|
||||
this.form = this.fb.group({
|
||||
name: '',
|
||||
password: '',
|
||||
@@ -41,16 +38,17 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.route.data.subscribe((data: { item: User }) => {
|
||||
this.route.data.subscribe((value) => {
|
||||
const data = value as { item: User };
|
||||
this.showItem(data.item);
|
||||
});
|
||||
}
|
||||
|
||||
showItem(item: User) {
|
||||
this.item = item;
|
||||
this.form.get('name').setValue(item.name);
|
||||
this.form.get('password').setValue('');
|
||||
this.form.get('lockedOut').setValue(item.lockedOut);
|
||||
(this.form.get('name') as AbstractControl).setValue(item.name);
|
||||
(this.form.get('password') as AbstractControl).setValue('');
|
||||
(this.form.get('lockedOut') as AbstractControl).setValue(item.lockedOut);
|
||||
this.form.setControl(
|
||||
'roles',
|
||||
this.fb.array(
|
||||
@@ -65,7 +63,11 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
|
||||
ngAfterViewInit() {
|
||||
setTimeout(() => {
|
||||
this.nameElement.nativeElement.focus();
|
||||
if (this.nameElement !== undefined) {
|
||||
if (this.nameElement !== undefined) {
|
||||
this.nameElement.nativeElement.focus();
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
@@ -82,7 +84,7 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.item.id).subscribe(
|
||||
this.ser.delete(this.item.id as string).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigateByUrl('/users');
|
||||
@@ -112,9 +114,11 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
this.item.password = formModel.password;
|
||||
this.item.lockedOut = formModel.lockedOut;
|
||||
const array = this.form.get('roles') as FormArray;
|
||||
this.item.roles.forEach((item, index) => {
|
||||
item.enabled = array.controls[index].value.role;
|
||||
});
|
||||
if (this.item.roles !== undefined) {
|
||||
this.item.roles.forEach((item, index) => {
|
||||
item.enabled = array.controls[index].value.role;
|
||||
});
|
||||
}
|
||||
return this.item;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user