Improve Angular project structure and code organization (#1328)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<div class="change-password-container">
|
||||
<div class="change-password-wrapper">
|
||||
<p-card class="change-password-card p-fluid">
|
||||
|
||||
<ng-template pTemplate="content">
|
||||
<form #changePasswordForm="ngForm" (ngSubmit)="changePassword()">
|
||||
<div class="p-field">
|
||||
<label for="currentPassword">Current Password</label>
|
||||
<p-password id="currentPassword" [(ngModel)]="currentPassword" name="currentPassword" [feedback]="false" required fluid></p-password>
|
||||
</div>
|
||||
|
||||
<div class="p-field">
|
||||
<label for="newPassword">New Password</label>
|
||||
<p-password id="newPassword" [(ngModel)]="newPassword" name="newPassword" [feedback]="false" required fluid></p-password>
|
||||
</div>
|
||||
|
||||
<div class="p-field">
|
||||
<label for="confirmNewPassword">Confirm New Password</label>
|
||||
<p-password id="confirmNewPassword" [(ngModel)]="confirmNewPassword" name="confirmNewPassword" [feedback]="false" required fluid></p-password>
|
||||
@if (!passwordsMatch && confirmNewPassword) {
|
||||
<p-message severity="error" text="New passwords do not match."></p-message>
|
||||
}
|
||||
</div>
|
||||
|
||||
<p-button type="submit" label="Change Password" icon="pi pi-key" class="p-fluid"></p-button>
|
||||
|
||||
@if (errorMessage) {
|
||||
<p-message severity="error" text="{{ errorMessage }}"></p-message>
|
||||
}
|
||||
@if (successMessage) {
|
||||
<p-message severity="success" text="{{ successMessage }}"></p-message>
|
||||
}
|
||||
</form>
|
||||
</ng-template>
|
||||
</p-card>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,28 @@
|
||||
.change-password-container {
|
||||
@apply flex items-center justify-center min-h-screen bg-gradient-to-br from-blue-900 via-cyan-600 to-blue-400 relative;
|
||||
}
|
||||
|
||||
.change-password-wrapper {
|
||||
@apply flex flex-col items-center gap-6 absolute top-1/4 transform -translate-y-1/4;
|
||||
}
|
||||
|
||||
.app-title {
|
||||
@apply text-5xl font-extrabold text-white drop-shadow-lg;
|
||||
text-shadow: 0px 4px 6px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
.change-password-card {
|
||||
@apply w-96 shadow-lg rounded-xl bg-opacity-90 backdrop-blur-lg;
|
||||
}
|
||||
|
||||
form {
|
||||
@apply flex flex-col gap-4;
|
||||
}
|
||||
|
||||
.p-field {
|
||||
@apply flex flex-col gap-2;
|
||||
}
|
||||
|
||||
p-button {
|
||||
@apply mt-4;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Card} from 'primeng/card';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {Message} from 'primeng/message';
|
||||
|
||||
import {Password} from 'primeng/password';
|
||||
import {MessageService, PrimeTemplate} from 'primeng/api';
|
||||
import {UserService} from '../../../features/settings/user-management/user.service';
|
||||
import {AuthService} from '../../service/auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-change-password',
|
||||
standalone: true,
|
||||
imports: [
|
||||
Button,
|
||||
Card,
|
||||
FormsModule,
|
||||
Message,
|
||||
Password,
|
||||
PrimeTemplate,
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './change-password.component.html',
|
||||
styleUrl: './change-password.component.scss'
|
||||
})
|
||||
export class ChangePasswordComponent {
|
||||
currentPassword: string = '';
|
||||
newPassword: string = '';
|
||||
confirmNewPassword: string = '';
|
||||
errorMessage: string | null = null;
|
||||
successMessage: string | null = null;
|
||||
|
||||
protected userService = inject(UserService);
|
||||
protected authService = inject(AuthService);
|
||||
protected messageService = inject(MessageService);
|
||||
|
||||
get passwordsMatch(): boolean {
|
||||
return this.newPassword === this.confirmNewPassword;
|
||||
}
|
||||
|
||||
changePassword() {
|
||||
this.errorMessage = null;
|
||||
this.successMessage = null;
|
||||
|
||||
if (!this.currentPassword || !this.newPassword || !this.confirmNewPassword) {
|
||||
this.errorMessage = 'All fields are required.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.passwordsMatch) {
|
||||
this.errorMessage = 'New passwords do not match.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentPassword === this.newPassword) {
|
||||
this.errorMessage = 'New password cannot be the same as the current password.';
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.changePassword(this.currentPassword, this.newPassword).subscribe({
|
||||
next: () => {
|
||||
this.successMessage = 'Password changed successfully!';
|
||||
this.logout();
|
||||
},
|
||||
error: (err) => {
|
||||
this.errorMessage = err.message;
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Password Change Failed',
|
||||
detail: this.errorMessage ?? 'An unknown error occurred.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.authService.logout();
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user