import {Component, Input, SimpleChanges} from '@angular/core'; import {FormsModule} from "@angular/forms"; import axios from "axios"; import {firstValueFrom} from "rxjs"; import {DevelopmentStore} from "../../../store/DevelopmentStore"; import {AuthStore} from "../../../store/authStore"; import {Router} from "@angular/router"; @Component({ selector: 'app-edituser', standalone: true, imports: [ FormsModule ], templateUrl: './edituser.component.html', styleUrl: './edituser.component.scss' }) export class EdituserComponent { @Input("username") parsedUsername: string = ""; username: string = ""; originalPassword: string = ""; newPassword: string = ""; confirmNewPassword: string = ""; constructor(private developmentStore: DevelopmentStore, private authStore: AuthStore, private router: Router) {} async saveUser() { if(this.newPassword !== this.confirmNewPassword) { alert("New password and confirm password do not match"); return; } try { const response = await axios({ method: 'post', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/users', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) }, data: { originalUsername: this.parsedUsername, username: this.username, originalPassword: this.originalPassword, newPassword: this.newPassword, newPasswordConfirm: this.confirmNewPassword } }); console.log("User updated successfully"); alert("User updated successfully. Please log in again to continue."); await this.router.navigate(['/login']); } catch (error) { console.error(error); } } ngOnChanges(changes: SimpleChanges) { if (changes['parsedUsername'] && !this.username) { this.username = changes['parsedUsername'].currentValue; } } }