Skip to content

Commit 9dded8a

Browse files
author
Vitor Durante
committed
Merge branch 'master' of github.com:vdurante/module-zero-core-template
2 parents c6f16b6 + da9974c commit 9dded8a

9 files changed

+194
-293
lines changed

angular/src/app/app.module.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ import { RightSideBarComponent } from '@app/layout/right-sidebar.component';
3030
import { CreateTenantDialogComponent } from './tenants/create-tenant/create-tenant-dialog.component';
3131
import { EditTenantDialogComponent } from './tenants/edit-tenant/edit-tenant-dialog.component';
3232
// roles
33-
import { RolesComponent } from "@app/roles/roles.component";
34-
import { CreateRoleDialogComponent } from "@app/roles/create-role/create-role-dialog.component";
35-
import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.component";
36-
33+
import { RolesComponent } from '@app/roles/roles.component';
34+
import { CreateOrEditRoleDialogComponent } from '@app/roles/create-or-edit-role-dialog.component';
3735

3836
@NgModule({
3937
declarations: [
@@ -54,8 +52,7 @@ import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.comp
5452
RightSideBarComponent,
5553
// roles
5654
RolesComponent,
57-
CreateRoleDialogComponent,
58-
EditRoleDialogComponent
55+
CreateOrEditRoleDialogComponent
5956
],
6057
imports: [
6158
CommonModule,
@@ -78,8 +75,7 @@ import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.comp
7875

7976
// roles
8077
RolesComponent,
81-
CreateRoleDialogComponent,
82-
EditRoleDialogComponent
78+
CreateOrEditRoleDialogComponent
8379
]
8480
})
8581
export class AppModule { }

angular/src/app/roles/edit-role/edit-role-dialog.component.html renamed to angular/src/app/roles/create-or-edit-role-dialog.component.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<form autocomplete="off"
22
#editRoleForm="ngForm"
33
(ngSubmit)="save()">
4-
<h1 mat-dialog-title>{{ 'EditRole' | localize }}</h1>
4+
<h1 mat-dialog-title *ngIf="!role.id">{{"CreateNewRole" | localize}}</h1>
5+
<h1 mat-dialog-title *ngIf="role.id">{{"EditRole" | localize}}: {{permissionName}}</h1>
56
<mat-dialog-content>
67
<mat-form-field>
78
<input matInput
@@ -33,11 +34,9 @@ <h4>{{ 'Permissions' | localize }}</h4>
3334
<ng-template ngFor let-permission [ngForOf]="permissions">
3435
<div class="col-sm-6">
3536
<div class="checkbox-wrapper">
36-
<mat-checkbox name="permission"
37-
[value]="permission.name"
38-
[checked]="checkPermission(permission.name)"
37+
<mat-checkbox [checked]="isPermissionChecked(permission.name)"
3938
[disabled]="role.isStatic"
40-
style="padding-bottom: 5px">{{ permission.displayName }}</mat-checkbox>
39+
(change)="onPermissionChange(permission, $event)">{{ permission.displayName }}</mat-checkbox>
4140
</div>
4241
</div>
4342
</ng-template>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { Component, Injector, Inject, OnInit, Optional } from '@angular/core';
2+
import {
3+
MatDialogRef,
4+
MAT_DIALOG_DATA,
5+
MatCheckboxChange
6+
} from '@angular/material';
7+
import * as _ from 'lodash';
8+
import {
9+
RoleServiceProxy,
10+
GetRoleForEditOutput,
11+
RoleDto,
12+
ListResultDtoOfPermissionDto,
13+
PermissionDto,
14+
CreateRoleDto
15+
} from '@shared/service-proxies/service-proxies';
16+
import { AppComponentBase } from '@shared/app-component-base';
17+
import { finalize } from 'rxjs/operators';
18+
19+
@Component({
20+
templateUrl: 'create-or-edit-role-dialog.component.html',
21+
styles: [
22+
`
23+
mat-form-field {
24+
width: 100%;
25+
}
26+
mat-checkbox {
27+
padding-bottom: 5px;
28+
}
29+
`
30+
]
31+
})
32+
export class CreateOrEditRoleDialogComponent extends AppComponentBase
33+
implements OnInit {
34+
role: RoleDto = new RoleDto();
35+
permissions: PermissionDto[] = [];
36+
grantedPermissionNames: string[] = [];
37+
checkedPermissionsMap: { [key: string]: boolean } = {};
38+
defaultPermissionCheckedStatus = true;
39+
permissionName = '';
40+
saving = false;
41+
42+
constructor(
43+
injector: Injector,
44+
private _roleService: RoleServiceProxy,
45+
private _dialogRef: MatDialogRef<CreateOrEditRoleDialogComponent>,
46+
@Optional() @Inject(MAT_DIALOG_DATA) private _id: number
47+
) {
48+
super(injector);
49+
}
50+
51+
ngOnInit(): void {
52+
if (this.isCreateDialog()) {
53+
this._roleService
54+
.getAllPermissions()
55+
.subscribe((result: ListResultDtoOfPermissionDto) => {
56+
this.permissions = result.items;
57+
this.setInitialPermissionsStatus();
58+
});
59+
} else {
60+
this._roleService
61+
.getRoleForEdit(this._id)
62+
.subscribe((result: GetRoleForEditOutput) => {
63+
this.role.init(result.role);
64+
_.map(result.permissions, item => {
65+
const permission = new PermissionDto();
66+
permission.init(item);
67+
this.permissions.push(permission);
68+
});
69+
this.grantedPermissionNames = result.grantedPermissionNames;
70+
this.permissionName = result.role.name;
71+
this.setInitialPermissionsStatus();
72+
});
73+
}
74+
}
75+
76+
isCreateDialog(): boolean {
77+
return this._id <= 0;
78+
}
79+
80+
setInitialPermissionsStatus(): void {
81+
_.map(this.permissions, item => {
82+
this.checkedPermissionsMap[item.name] = this.isCreateDialog()
83+
? this.defaultPermissionCheckedStatus
84+
: _.includes(this.grantedPermissionNames, item.name);
85+
});
86+
}
87+
88+
isPermissionChecked(permissionName: string): boolean {
89+
if (this.isCreateDialog()) {
90+
return this.defaultPermissionCheckedStatus;
91+
}
92+
return _.includes(this.grantedPermissionNames, permissionName);
93+
}
94+
95+
onPermissionChange(permission: PermissionDto, $event: MatCheckboxChange) {
96+
this.checkedPermissionsMap[permission.name] = $event.checked;
97+
}
98+
99+
getCheckedPermissions(): string[] {
100+
const permissions: string[] = [];
101+
_.forEach(this.checkedPermissionsMap, function (value, key) {
102+
if (value) {
103+
permissions.push(key);
104+
}
105+
});
106+
return permissions;
107+
}
108+
109+
save(): void {
110+
this.saving = true;
111+
112+
const role = new RoleDto();
113+
role.permissions = this.getCheckedPermissions();
114+
role.init(this.role);
115+
116+
if (this.isCreateDialog()) {
117+
const createRole = new CreateRoleDto();
118+
createRole.init(role);
119+
this._roleService.create(createRole)
120+
.pipe(
121+
finalize(() => {
122+
this.saving = false;
123+
})
124+
).subscribe(() => {
125+
this.notify.info(this.l('SavedSuccessfully'));
126+
this.close(true);
127+
});
128+
} else {
129+
this._roleService.update(role)
130+
.pipe(
131+
finalize(() => {
132+
this.saving = false;
133+
})
134+
).subscribe(() => {
135+
this.notify.info(this.l('SavedSuccessfully'));
136+
this.close(true);
137+
});
138+
}
139+
}
140+
141+
close(result: any): void {
142+
this._dialogRef.close(result);
143+
}
144+
}

angular/src/app/roles/create-role/create-role-dialog.component.html

Lines changed: 0 additions & 59 deletions
This file was deleted.

angular/src/app/roles/create-role/create-role-dialog.component.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)