diff --git a/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.css b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.css new file mode 100644 index 0000000..605ecf3 --- /dev/null +++ b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.css @@ -0,0 +1,15 @@ +.user-form .form-field { + display: block; + margin-bottom: 10px; + } + + .checkbox-group label { + display: block; + margin-bottom: 8px; + } + + .error-message { + color: red; + margin-top: 10px; + } + \ No newline at end of file diff --git a/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.html b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.html new file mode 100644 index 0000000..15aaf85 --- /dev/null +++ b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.html @@ -0,0 +1,23 @@ +

Editar Usuario

+
+
+ + Nombre de usuario + + + + Contraseña + + + + Repite la contraseña + + +
Las contraseñas no coinciden
+
Ha habido un error, inténtelo de nuevo
+
+
+
+ + +
diff --git a/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.spec.ts b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.spec.ts new file mode 100644 index 0000000..0135615 --- /dev/null +++ b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ChangePasswordModalComponent } from './change-password-modal.component'; + +describe('ChangePasswordModalComponent', () => { + let component: ChangePasswordModalComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ChangePasswordModalComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ChangePasswordModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.ts b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.ts new file mode 100644 index 0000000..24928d7 --- /dev/null +++ b/ogWebconsole/src/app/components/pages/admin/users/users/change-password-modal/change-password-modal.component.ts @@ -0,0 +1,71 @@ +import { Component, EventEmitter, Inject, Output } from '@angular/core'; +import { FormGroup, FormBuilder, Validators } from '@angular/forms'; +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; +import { EditUserModalComponent } from '../edit-user-modal/edit-user-modal.component'; +import { UserService } from '../users.service'; + +@Component({ + selector: 'app-change-password-modal', + templateUrl: './change-password-modal.component.html', + styleUrls: ['./change-password-modal.component.css'] +}) +export class ChangePasswordModalComponent { + @Output() userEdited = new EventEmitter(); + userForm: FormGroup; + passwordMismatch: boolean = false; + updateError: boolean = false; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private fb: FormBuilder, + private userService: UserService + ) { + this.userForm = this.fb.group({ + username: [this.data.user, Validators.required], + password: ['', Validators.required], + confirmPassword: ['', Validators.required] + }, { validators: this.passwordMatchValidator }); + } + + ngOnInit(): void {} + + onNoClick(): void { + this.dialogRef.close(); + } + + onSubmit(): void { + if (this.userForm.valid) { + this.passwordMismatch = false; + this.updateError = false; + + const userPayload = { + username: this.userForm.value.username, + allowedOrganizationalUnits: this.data.user.allowedOrganizationalUnits, + password: this.userForm.value.password, + enabled: true, + userGroups: this.data.user.userGroups + }; + + this.userService.updateUser(this.data.uuid, userPayload).subscribe( + response => { + console.log('User updated successfully:', response); + this.userEdited.emit(); + this.dialogRef.close(this.userForm.value); + }, + error => { + console.error('Error updating user:', error); + this.updateError = true; + } + ); + } else { + console.error('Form is invalid'); + this.passwordMismatch = this.userForm.hasError('mismatch'); + this.updateError = true; + } + } + + private passwordMatchValidator(form: FormGroup): { [key: string]: boolean } | null { + return form.get('password')?.value === form.get('confirmPassword')?.value ? null : { mismatch: true }; + } +}