|
1 | | -import { Component, EventEmitter, Input, OnInit, Output, inject } from '@angular/core'; |
2 | | -import { Paged } from 'src/app/common/models/common'; |
3 | | -import { SecurityGroupModel } from 'src/app/common/models/security'; |
4 | | -import { UserRegisterModel } from 'src/app/common/models/user'; |
5 | | -import { AdminService } from 'src/app/common/services/users'; |
| 1 | +import {Component, EventEmitter, Input, OnInit, Output, inject} from '@angular/core'; |
| 2 | +import {FormBuilder, FormGroup, Validators} from '@angular/forms'; |
| 3 | +import {Paged} from 'src/app/common/models/common'; |
| 4 | +import {SecurityGroupModel} from 'src/app/common/models/security'; |
| 5 | +import {UserRegisterModel} from 'src/app/common/models/user'; |
| 6 | +import {AdminService} from 'src/app/common/services/users'; |
6 | 7 | import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog'; |
7 | 8 |
|
8 | 9 | @Component({ |
9 | | - selector: 'app-user-modal', |
10 | | - templateUrl: './user-modal.component.html', |
11 | | - styleUrls: ['./user-modal.component.scss'], |
12 | | - standalone: false |
| 10 | + selector: 'app-user-modal', |
| 11 | + templateUrl: './user-modal.component.html', |
| 12 | + styleUrls: ['./user-modal.component.scss'], |
| 13 | + standalone: false |
13 | 14 | }) |
14 | 15 | export class UserModalComponent implements OnInit { |
| 16 | + private fb = inject(FormBuilder); |
15 | 17 | private adminService = inject(AdminService); |
16 | 18 | dialogRef = inject<MatDialogRef<UserModalComponent>>(MatDialogRef); |
17 | | - |
18 | | - userModel: UserRegisterModel = new UserRegisterModel(); |
| 19 | + userForm: FormGroup; |
19 | 20 | public availableGroups: Paged<SecurityGroupModel> = new Paged<SecurityGroupModel>(); |
20 | 21 | edit: boolean = false; |
| 22 | + selectedId?: number; |
| 23 | + isDeviceUser = false; |
21 | 24 |
|
22 | 25 | constructor() { |
23 | | - const data = inject<{ |
24 | | - availableGroups: Paged<SecurityGroupModel>; |
25 | | - selectedId?: number; |
26 | | -}>(MAT_DIALOG_DATA); |
27 | | - |
| 26 | + const data = inject<{ availableGroups: Paged<SecurityGroupModel>; selectedId?: number }>(MAT_DIALOG_DATA); |
28 | 27 | this.availableGroups = data.availableGroups; |
29 | | - if(data.selectedId) { |
| 28 | + this.selectedId = data.selectedId; |
| 29 | + |
| 30 | + this.userForm = this.fb.group({ |
| 31 | + firstName: ['', Validators.required], |
| 32 | + lastName: ['', Validators.required], |
| 33 | + email: ['', [Validators.required, Validators.email]], |
| 34 | + password: [''], |
| 35 | + role: ['user', Validators.required], |
| 36 | + groupId: [null, Validators.required] |
| 37 | + }); |
| 38 | + |
| 39 | + if (data.selectedId) { |
30 | 40 | this.edit = true; |
31 | | - this.getUserInfo(data.selectedId); |
| 41 | + this.loadUser(data.selectedId); |
32 | 42 | } |
33 | 43 | } |
34 | 44 |
|
35 | | - ngOnInit() {} |
| 45 | + ngOnInit() { |
| 46 | + if (this.edit) { |
| 47 | + this.userForm.get('password')?.clearValidators(); |
| 48 | + this.userForm.get('password')?.updateValueAndValidity(); |
| 49 | + } else { |
| 50 | + this.userForm.get('password')?.setValidators(Validators.required); |
| 51 | + this.userForm.get('password')?.updateValueAndValidity(); |
| 52 | + } |
| 53 | + this.userForm.get('password')?.updateValueAndValidity(); |
36 | 54 |
|
37 | | - createUser() { |
38 | | - this.adminService.createUser(this.userModel).subscribe((data) => { |
39 | | - if (data && data.success) { |
40 | | - this.hide(true); |
| 55 | + this.userForm.get('role')?.valueChanges.subscribe((role) => { |
| 56 | + const groupControl = this.userForm.get('groupId'); |
| 57 | + if (role === 'admin') { |
| 58 | + groupControl?.clearValidators(); |
| 59 | + groupControl?.setValue(null); |
| 60 | + } else { |
| 61 | + groupControl?.setValidators(Validators.required); |
41 | 62 | } |
| 63 | + groupControl?.updateValueAndValidity(); |
42 | 64 | }); |
| 65 | + |
43 | 66 | } |
44 | 67 |
|
45 | | - getUserInfo(selectedId: number) { |
| 68 | + loadUser(selectedId: number) { |
46 | 69 | this.adminService.getUser(selectedId).subscribe((data) => { |
47 | 70 | if (data && data.model) { |
48 | | - this.userModel = data.model; |
| 71 | + const user = data.model; |
| 72 | + this.userForm.patchValue({ |
| 73 | + firstName: user.firstName, |
| 74 | + lastName: user.lastName, |
| 75 | + email: user.email, |
| 76 | + role: user.role, |
| 77 | + groupId: user.groupId |
| 78 | + }); |
| 79 | + |
| 80 | + if (user.isDeviceUser) { |
| 81 | + this.isDeviceUser = true; |
| 82 | + } |
| 83 | + |
| 84 | + if (user.role === 'admin') { |
| 85 | + const groupControl = this.userForm.get('groupId'); |
| 86 | + groupControl?.clearValidators(); |
| 87 | + groupControl?.updateValueAndValidity(); |
| 88 | + } |
49 | 89 | } |
50 | 90 | }); |
51 | 91 | } |
52 | 92 |
|
53 | | - updateUser() { |
54 | | - this.adminService.updateUser(this.userModel).subscribe((data) => { |
55 | | - if (data && data.success) { |
56 | | - this.hide(true); |
57 | | - } |
58 | | - }); |
| 93 | + submit() { |
| 94 | + if (this.userForm.invalid) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const formValue = this.userForm.value; |
| 99 | + const model = new UserRegisterModel(); |
| 100 | + Object.assign(model, formValue); |
| 101 | + |
| 102 | + if (this.edit && this.selectedId) { |
| 103 | + model.id = this.selectedId; |
| 104 | + this.adminService.updateUser(model).subscribe((data) => { |
| 105 | + if (data && data.success) { |
| 106 | + this.hide(true); |
| 107 | + } |
| 108 | + }); |
| 109 | + } else { |
| 110 | + this.adminService.createUser(model).subscribe((data) => { |
| 111 | + if (data && data.success) { |
| 112 | + this.hide(true); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
59 | 116 | } |
60 | 117 |
|
61 | 118 | hide(result = false) { |
62 | | - this.dialogRef.close({result: result, edit: this.edit}); |
| 119 | + this.dialogRef.close({result, edit: this.edit}); |
63 | 120 | } |
64 | 121 | } |
0 commit comments