add-user-dialog.component.ts
4.2 KB
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
///
/// Copyright © 2016-2020 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { FormGroup } from '@angular/forms';
import { UserComponent } from '@modules/home/pages/user/user.component';
import { Authority } from '@shared/models/authority.enum';
import { ActivationMethod, activationMethodTranslations, User } from '@shared/models/user.model';
import { CustomerId } from '@shared/models/id/customer-id';
import { UserService } from '@core/http/user.service';
import { Observable } from 'rxjs';
import {
ActivationLinkDialogComponent,
ActivationLinkDialogData
} from '@modules/home/pages/user/activation-link-dialog.component';
import { TenantId } from '@app/shared/models/id/tenant-id';
import { DialogComponent } from '@shared/components/dialog.component';
import { Router } from '@angular/router';
export interface AddUserDialogData {
tenantId: string;
customerId: string;
authority: Authority;
}
@Component({
selector: 'tb-add-user-dialog',
templateUrl: './add-user-dialog.component.html',
styleUrls: ['./add-user-dialog.component.scss']
})
export class AddUserDialogComponent extends DialogComponent<AddUserDialogComponent, User> implements OnInit {
detailsForm: FormGroup;
user: User;
activationMethods = Object.keys(ActivationMethod);
activationMethodEnum = ActivationMethod;
activationMethodTranslations = activationMethodTranslations;
activationMethod = ActivationMethod.DISPLAY_ACTIVATION_LINK;
@ViewChild(UserComponent, {static: true}) userComponent: UserComponent;
constructor(protected store: Store<AppState>,
protected router: Router,
@Inject(MAT_DIALOG_DATA) public data: AddUserDialogData,
public dialogRef: MatDialogRef<AddUserDialogComponent, User>,
private userService: UserService,
private dialog: MatDialog) {
super(store, router, dialogRef);
}
ngOnInit(): void {
this.user = {} as User;
this.userComponent.isEdit = true;
this.userComponent.entity = this.user;
this.detailsForm = this.userComponent.entityForm;
}
cancel(): void {
this.dialogRef.close(null);
}
add(): void {
if (this.detailsForm.valid) {
this.user = {...this.user, ...this.userComponent.entityForm.value};
this.user.authority = this.data.authority;
this.user.tenantId = new TenantId(this.data.tenantId);
this.user.customerId = new CustomerId(this.data.customerId);
const sendActivationEmail = this.activationMethod === ActivationMethod.SEND_ACTIVATION_MAIL;
this.userService.saveUser(this.user, sendActivationEmail).subscribe(
(user) => {
if (this.activationMethod === ActivationMethod.DISPLAY_ACTIVATION_LINK) {
this.userService.getActivationLink(user.id.id).subscribe(
(activationLink) => {
this.displayActivationLink(activationLink).subscribe(
() => {
this.dialogRef.close(user);
}
);
}
);
} else {
this.dialogRef.close(user);
}
}
);
}
}
displayActivationLink(activationLink: string): Observable<void> {
return this.dialog.open<ActivationLinkDialogComponent, ActivationLinkDialogData,
void>(ActivationLinkDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
activationLink
}
}).afterClosed();
}
}