114 lines
3.1 KiB
TypeScript
114 lines
3.1 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { Role } from '../role';
|
|
import { RoleService } from '../role.service';
|
|
|
|
@Component({
|
|
selector: 'app-role-detail',
|
|
templateUrl: './role-detail.component.html',
|
|
styleUrls: ['./role-detail.component.css'],
|
|
})
|
|
export class RoleDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: FormGroup<{
|
|
name: FormControl<string | null>;
|
|
permissions: FormArray<
|
|
FormGroup<{
|
|
permission: FormControl<boolean>;
|
|
}>
|
|
>;
|
|
}>;
|
|
|
|
item: Role = new Role();
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toaster: ToasterService,
|
|
private dialog: MatDialog,
|
|
private ser: RoleService,
|
|
) {
|
|
this.form = new FormGroup({
|
|
name: new FormControl<string | null>(null),
|
|
permissions: new FormArray<FormGroup<{ permission: FormControl<boolean> }>>([]),
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: Role };
|
|
this.item = data.item;
|
|
|
|
this.form.controls.name.setValue(this.item.name);
|
|
this.form.controls.permissions.reset();
|
|
this.item.permissions.forEach((x) =>
|
|
this.form.controls.permissions.push(
|
|
new FormGroup({
|
|
permission: new FormControl<boolean>(x.enabled, { nonNullable: true }),
|
|
}),
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/roles');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id as string).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/roles');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Role?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): Role {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name!;
|
|
const array = this.form.controls.permissions;
|
|
this.item.permissions.forEach((item, index) => {
|
|
item.enabled = array.controls[index].value.permission!;
|
|
});
|
|
return this.item;
|
|
}
|
|
}
|