| .. | .. |
|---|
| 9 | 9 | import { TdMediaService } from '@covalent/core'; |
|---|
| 10 | 10 | import { FormBase, IComboOption } from './base'; |
|---|
| 11 | 11 | import { ActivatedRoute, Router } from '@angular/router'; |
|---|
| 12 | +import { OrganizationsService } from "../resources/organizations"; |
|---|
| 12 | 13 | |
|---|
| 13 | 14 | var user_example = { |
|---|
| 14 | 15 | username: 'rym', |
|---|
| .. | .. |
|---|
| 27 | 28 | templateUrl: 'src/app/forms/user.form.html' |
|---|
| 28 | 29 | }) |
|---|
| 29 | 30 | export class UserFormComponent extends FormBase { |
|---|
| 30 | | - |
|---|
| 31 | + allOrganizations: IComboOption[]; |
|---|
| 32 | + orgNames: string[] = []; |
|---|
| 33 | + allRoles: any[] = [{"id":1, "code": "advance", "label":"Advance"}, {"id":2, "code": "admin","label":"Admin"}]; |
|---|
| 34 | + user_orgs: string[] = []; |
|---|
| 35 | + user_roles: any = {}; |
|---|
| 31 | 36 | constructor(private http: Http, |
|---|
| 32 | 37 | private users: UsersService, |
|---|
| 33 | | - private applications: ApplicationsService, |
|---|
| 38 | + private organizations: OrganizationsService, |
|---|
| 34 | 39 | router: Router, |
|---|
| 35 | 40 | toaster: ToastsManager, |
|---|
| 36 | 41 | route: ActivatedRoute, |
|---|
| .. | .. |
|---|
| 39 | 44 | super($L, router, route, toaster, users, $L.get('user'), dialogs); |
|---|
| 40 | 45 | } |
|---|
| 41 | 46 | |
|---|
| 42 | | - |
|---|
| 47 | + save() : void { |
|---|
| 48 | + this.data.organizations_ids = []; |
|---|
| 49 | + this.data.roles = []; |
|---|
| 50 | + this.user_orgs.forEach(orgName => { |
|---|
| 51 | + var selectedOrg = this.allOrganizations.find(org => org.label === orgName); |
|---|
| 52 | + this.data.organizations_ids.push(selectedOrg.id); |
|---|
| 53 | + }); |
|---|
| 54 | + this.user_roles.advance && this.data.roles.push(1); |
|---|
| 55 | + this.user_roles.admin && this.data.roles.push(2); |
|---|
| 56 | + super.save('username'); |
|---|
| 57 | + } |
|---|
| 58 | + |
|---|
| 59 | + canBeDeleted() { |
|---|
| 60 | + return this.data && this.data.username !== 'admin' && this.data.username !== '_client'; |
|---|
| 61 | + } |
|---|
| 62 | + |
|---|
| 63 | + loadCombos() : void { |
|---|
| 64 | + this.organizations.get() |
|---|
| 65 | + .map(list => list.map((org : any) => <IComboOption>{id: org.id, label: org.name})) |
|---|
| 66 | + .subscribe( |
|---|
| 67 | + data => { |
|---|
| 68 | + this.allOrganizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)); |
|---|
| 69 | + this.orgNames = this.allOrganizations.map(org => org.label); |
|---|
| 70 | + this._loadOrgs(); |
|---|
| 71 | + }, |
|---|
| 72 | + err => console.error('Error loading organizations') |
|---|
| 73 | + ); |
|---|
| 74 | + } |
|---|
| 75 | + |
|---|
| 43 | 76 | goBack(): void { |
|---|
| 44 | 77 | this.router.navigate([`users`]); |
|---|
| 45 | 78 | } |
|---|
| 46 | | - |
|---|
| 79 | + _loadOrgs() { |
|---|
| 80 | + if (this.data && this.data.organizations_ids && this.allOrganizations && this.allOrganizations.length > 0) { |
|---|
| 81 | + this.data.organizations_ids.forEach((orgId : number) => { |
|---|
| 82 | + var selectedOrg = this.allOrganizations.find(org => org.id === orgId); |
|---|
| 83 | + this.user_orgs.push(selectedOrg.label); |
|---|
| 84 | + }); |
|---|
| 85 | + } |
|---|
| 86 | + } |
|---|
| 47 | 87 | init() : void { |
|---|
| 88 | + this.loadCombos(); |
|---|
| 89 | + this.user_orgs = []; |
|---|
| 90 | + this.user_roles = {}; |
|---|
| 48 | 91 | super.setFirstFocus(); |
|---|
| 49 | 92 | super.reset(); |
|---|
| 50 | 93 | super.prepareInitialData('username', { |
|---|
| 51 | | - metadata: [] |
|---|
| 94 | + organizations_ids: [], |
|---|
| 95 | + roles: [] |
|---|
| 96 | + }, (data) => { |
|---|
| 97 | + this._loadOrgs(); |
|---|
| 98 | + data.roles.forEach((roleId : number) => { |
|---|
| 99 | + var selectedRole = this.allRoles.find(r => r.id === roleId); |
|---|
| 100 | + this.user_roles[selectedRole.code] = true; |
|---|
| 101 | + }); |
|---|
| 102 | + |
|---|
| 52 | 103 | }); |
|---|
| 53 | 104 | } |
|---|
| 54 | 105 | |
|---|