feat(user-profile): display user information and password change form in dialog
This commit is contained in:
committed by
Aditya Chandel
parent
fc0d9fff1f
commit
71bc927d54
@@ -39,11 +39,6 @@
|
||||
pTooltip="Settings" tooltipPosition="top"></i>
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<button class="topbar-item" (click)="navigateToProfile()" pTooltip="Profile" tooltipPosition="top">
|
||||
<i class="pi pi-user text-surface-700 dark:text-surface-100"></i>
|
||||
</button>
|
||||
</li>
|
||||
</div>
|
||||
|
||||
<p-divider layout="vertical"/>
|
||||
@@ -61,25 +56,31 @@
|
||||
</li>
|
||||
|
||||
<p-divider layout="vertical"/>
|
||||
<li class="relative">
|
||||
<button type="button"
|
||||
class="topbar-item config-item"
|
||||
enterActiveClass="animate-scalein"
|
||||
enterFromClass="hidden"
|
||||
leaveActiveClass="animate-fadeout"
|
||||
leaveToClass="hidden"
|
||||
pStyleClass="@next" [hideOnOutsideClick]="true">
|
||||
<i class="pi pi-palette"></i>
|
||||
</button>
|
||||
<app-theme-configurator></app-theme-configurator>
|
||||
</li>
|
||||
|
||||
<p-divider layout="vertical"/>
|
||||
<li>
|
||||
<button class="topbar-item" (click)="logout()" pTooltip="Logout" tooltipPosition="top">
|
||||
<i class="pi pi-sign-out text-surface-700 dark:text-surface-100"></i>
|
||||
</button>
|
||||
</li>
|
||||
<div class="flex gap-6">
|
||||
<li class="relative">
|
||||
<button type="button"
|
||||
class="topbar-item config-item"
|
||||
enterActiveClass="animate-scalein"
|
||||
enterFromClass="hidden"
|
||||
leaveActiveClass="animate-fadeout"
|
||||
leaveToClass="hidden"
|
||||
pStyleClass="@next" [hideOnOutsideClick]="true">
|
||||
<i class="pi pi-palette"></i>
|
||||
</button>
|
||||
<app-theme-configurator></app-theme-configurator>
|
||||
</li>
|
||||
<li>
|
||||
<button class="topbar-item" (click)="openUserProfileDialog()" pTooltip="Profile" tooltipPosition="top">
|
||||
<i class="pi pi-user text-surface-700 dark:text-surface-100"></i>
|
||||
</button>
|
||||
</li>
|
||||
<li>
|
||||
<button class="topbar-item" (click)="logout()" pTooltip="Logout" tooltipPosition="top">
|
||||
<i class="pi pi-sign-out text-surface-700 dark:text-surface-100"></i>
|
||||
</button>
|
||||
</li>
|
||||
</div>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import {LiveNotificationBoxComponent} from '../../../core/component/live-notific
|
||||
import {BookUploaderComponent} from '../../../utilities/component/book-uploader/book-uploader.component';
|
||||
import {AuthService} from '../../../core/service/auth.service';
|
||||
import {UserService} from '../../../user.service';
|
||||
import {UserProfileDialogComponent} from '../../../user-profile-dialog/user-profile-dialog.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-topbar',
|
||||
@@ -118,8 +119,16 @@ export class AppTopBarComponent implements OnDestroy {
|
||||
this.router.navigate(['/settings']);
|
||||
}
|
||||
|
||||
navigateToProfile() {
|
||||
this.router.navigate(['/profile']);
|
||||
openUserProfileDialog() {
|
||||
this.ref = this.dialogService.open(UserProfileDialogComponent, {
|
||||
header: 'User Profile Information',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: {
|
||||
position: 'absolute',
|
||||
top: '10%',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logout() {
|
||||
|
||||
@@ -1,60 +1,65 @@
|
||||
<div class="card mx-auto max-w-xl" *ngIf="currentUser">
|
||||
<div class="p-4">
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h2 class="text-xl">User Profile</h2>
|
||||
<div>
|
||||
<p-button *ngIf="!isEditing" label="Edit Profile" icon="pi pi-pencil" (onClick)="toggleEdit()" styleClass="p-button-sm p-button-secondary"></p-button>
|
||||
<div *ngIf="isEditing" class="flex gap-2">
|
||||
<p-button label="Cancel" icon="pi pi-times" (onClick)="toggleEdit()" styleClass="p-button-sm p-button-secondary"></p-button>
|
||||
<p-button label="Save" icon="pi pi-check" (onClick)="saveChanges()" styleClass="p-button-sm p-button-success"></p-button>
|
||||
<div class="card mx-auto max-w-2xl" *ngIf="currentUser">
|
||||
<div class="p-6 space-y-8">
|
||||
|
||||
<!-- Profile Info -->
|
||||
<section class="space-y-6">
|
||||
<div class="flex justify-between items-center">
|
||||
<h2 class="text-2xl font-semibold">Profile Information</h2>
|
||||
<div class="flex gap-2">
|
||||
<p-button *ngIf="!isEditing" label="Edit Profile" icon="pi pi-pencil" (onClick)="toggleEdit()" styleClass="p-button-sm p-button-secondary"></p-button>
|
||||
<div *ngIf="isEditing" class="flex gap-2">
|
||||
<p-button label="Cancel" icon="pi pi-times" (onClick)="toggleEdit()" styleClass="p-button-sm p-button-secondary"></p-button>
|
||||
<p-button label="Save" icon="pi pi-check" (onClick)="saveChanges()" styleClass="p-button-sm p-button-success"></p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
|
||||
<div>
|
||||
<label for="username" class="block text-lg">Username</label>
|
||||
<p *ngIf="!isEditing" class="mt-1 text-lg">{{ currentUser.username }}</p>
|
||||
<input *ngIf="isEditing" id="username" type="text" pInputText [(ngModel)]="currentUser.username" class="w-full mt-1" disabled>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1">Username</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser.username }}</p>
|
||||
<input *ngIf="isEditing" id="username" type="text" pInputText [(ngModel)]="currentUser.username" class="w-full" disabled />
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1">Full Name</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser.name }}</p>
|
||||
<input *ngIf="isEditing" id="fullname" type="text" pInputText [(ngModel)]="editUserData.name" class="w-full" />
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="block text-sm font-medium mb-1">Email</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser.email }}</p>
|
||||
<input *ngIf="isEditing" id="email" type="email" pInputText [(ngModel)]="editUserData.email" class="w-full" />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label for="fullname" class="block text-lg">Full Name</label>
|
||||
<p *ngIf="!isEditing" class="mt-1 text-lg">{{ currentUser.name }}</p>
|
||||
<input *ngIf="isEditing" id="fullname" type="text" pInputText [(ngModel)]="editUserData.name" class="w-full mt-1">
|
||||
</div>
|
||||
<div>
|
||||
<label for="email" class="block text-lg">Email</label>
|
||||
<p *ngIf="!isEditing" class="mt-1 text-lg">{{ currentUser.email }}</p>
|
||||
<input *ngIf="isEditing" id="email" type="email" pInputText [(ngModel)]="editUserData.email" class="w-full mt-1">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="p-4">
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h2 class="text-xl">Change Password</h2>
|
||||
</div>
|
||||
<p-divider></p-divider>
|
||||
|
||||
<form #changePasswordForm="ngForm" (ngSubmit)="submitPasswordChange()">
|
||||
<div class="p-field mb-3">
|
||||
<label for="currentPassword" class="block mb-3 text-lg">Current Password</label>
|
||||
<p-password id="currentPassword" [(ngModel)]="currentPassword" name="currentPassword" [feedback]="false" fluid/>
|
||||
</div>
|
||||
<!-- Change Password -->
|
||||
<section class="space-y-6">
|
||||
<h2 class="text-2xl font-semibold">Change Password</h2>
|
||||
<form #changePasswordForm="ngForm" (ngSubmit)="submitPasswordChange()" class="space-y-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1" for="currentPassword">Current Password</label>
|
||||
<p-password id="currentPassword" [(ngModel)]="currentPassword" name="currentPassword" [feedback]="false" class="w-full" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1" for="newPassword">New Password</label>
|
||||
<p-password id="newPassword" [(ngModel)]="newPassword" name="newPassword" [feedback]="false" class="w-full" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1" for="confirmNewPassword">Confirm New Password</label>
|
||||
<p-password id="confirmNewPassword" [(ngModel)]="confirmNewPassword" name="confirmNewPassword" [feedback]="false" class="w-full" />
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<p-button type="submit" label="Change Password" icon="pi pi-key" class="p-button-sm p-button-primary" />
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<div class="p-field mb-3">
|
||||
<label for="newPassword" class="block mb-3 text-lg">New Password</label>
|
||||
<p-password id="newPassword" [(ngModel)]="newPassword" name="newPassword" [feedback]="false" fluid/>
|
||||
</div>
|
||||
|
||||
<div class="p-field mb-6">
|
||||
<label for="confirmNewPassword" class="block mb-3 text-lg">Confirm New Password</label>
|
||||
<p-password id="confirmNewPassword" [(ngModel)]="confirmNewPassword" name="confirmNewPassword" [feedback]="false" fluid/>
|
||||
</div>
|
||||
|
||||
<p-button type="submit" label="Change Password" icon="pi pi-key" class="p-fluid"/>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div *ngIf="!currentUser">
|
||||
|
||||
<div *ngIf="!currentUser" class="flex items-center justify-center min-h-[50vh] text-lg">
|
||||
Loading user data...
|
||||
</div>
|
||||
|
||||
@@ -8,6 +8,7 @@ import {Password} from 'primeng/password';
|
||||
import {InputTextModule} from 'primeng/inputtext';
|
||||
import {Subscription} from 'rxjs';
|
||||
import {ToastModule} from 'primeng/toast';
|
||||
import {Divider} from 'primeng/divider';
|
||||
|
||||
@Component({
|
||||
selector: 'app-profile',
|
||||
@@ -19,6 +20,7 @@ import {ToastModule} from 'primeng/toast';
|
||||
Password,
|
||||
InputTextModule,
|
||||
ToastModule,
|
||||
Divider,
|
||||
],
|
||||
templateUrl: './profile.component.html',
|
||||
styleUrl: './profile.component.scss',
|
||||
@@ -117,17 +119,17 @@ export class ProfileComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
this.userService.changePassword(this.currentPassword, this.newPassword).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Password changed successfully'});
|
||||
this.resetPasswordForm();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: err?.message || 'Failed to change password'
|
||||
});
|
||||
}
|
||||
});
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Password changed successfully'});
|
||||
this.resetPasswordForm();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: err?.message || 'Failed to change password'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<p-divider></p-divider>
|
||||
|
||||
<div class="min-w-[300px] md:min-w-[500px]">
|
||||
|
||||
<section>
|
||||
<div class="flex flex-col gap-4">
|
||||
<h2 class="text-xl font-semibold">User Profile</h2>
|
||||
<div class="px-4 py-2">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1">Username</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser?.username || 'N/A' }}</p>
|
||||
<input *ngIf="isEditing" disabled id="username" type="text" pInputText [(ngModel)]="editUserData.username" class="w-full"/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-1">Full Name</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser?.name || 'N/A' }}</p>
|
||||
<input *ngIf="isEditing" id="fullname" type="text" pInputText [(ngModel)]="editUserData.name" class="w-full"/>
|
||||
</div>
|
||||
|
||||
<div class="md:col-span-2">
|
||||
<label class="block text-sm font-medium mb-1">Email</label>
|
||||
<p *ngIf="!isEditing" class="text-lg">{{ currentUser?.email || 'N/A' }}</p>
|
||||
<input *ngIf="isEditing" id="email" type="email" pInputText [(ngModel)]="editUserData.email" class="w-full"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-start mt-6">
|
||||
<div *ngIf="!isEditing">
|
||||
<p-button label="Edit" severity="info" icon="pi pi-pencil" (onClick)="toggleEdit()"></p-button>
|
||||
</div>
|
||||
<div *ngIf="isEditing" class="flex gap-2">
|
||||
<p-button label="Cancel" severity="secondary" icon="pi pi-times" (onClick)="toggleEdit()"></p-button>
|
||||
<p-button label="Save" severity="success" icon="pi pi-check" (onClick)="updateProfile()"></p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<p-divider></p-divider>
|
||||
|
||||
<section class="space-y-6 pt-4">
|
||||
<div class="flex flex-col gap-4">
|
||||
<h2 class="text-xl font-semibold">Change Password</h2>
|
||||
<div class="change-password-wrapper">
|
||||
<div class="px-4 py-2">
|
||||
<form [formGroup]="changePasswordForm" (ngSubmit)="submitPasswordChange()">
|
||||
<div class="p-field mb-4">
|
||||
<label for="currentPassword">Current Password</label>
|
||||
<p-password id="currentPassword" formControlName="currentPassword" [feedback]="false" fluid></p-password>
|
||||
<p-message *ngIf="changePasswordForm.get('currentPassword')?.invalid && changePasswordForm.get('currentPassword')?.touched" severity="error" text="Current password is required."></p-message>
|
||||
</div>
|
||||
|
||||
<div class="p-field mb-4">
|
||||
<label for="newPassword">New Password (at least 6 characters)</label>
|
||||
<p-password id="newPassword" formControlName="newPassword" [feedback]="false" fluid></p-password>
|
||||
<p-message *ngIf="changePasswordForm.get('newPassword')?.errors?.['minlength'] && changePasswordForm.get('newPassword')?.touched" severity="error" text="Password must be at least 6 characters long."></p-message>
|
||||
</div>
|
||||
|
||||
<div class="p-field mb-4">
|
||||
<label for="confirmNewPassword">Confirm New Password</label>
|
||||
<p-password id="confirmNewPassword" formControlName="confirmNewPassword" [feedback]="false" fluid></p-password>
|
||||
<p-message *ngIf="passwordsMismatch" severity="error" text="Passwords do not match."></p-message>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-start pt-4">
|
||||
<p-button type="submit" severity="info" label="Change Password" icon="pi pi-key" [disabled]="changePasswordForm.invalid" class="p-fluid"></p-button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
|
||||
<div *ngIf="!currentUser" class="flex items-center justify-center min-h-[50vh] text-lg">
|
||||
Loading user data...
|
||||
</div>
|
||||
@@ -0,0 +1,151 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {FormBuilder, FormGroup, Validators, FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {Password} from 'primeng/password';
|
||||
import {User, UserService} from '../user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Subscription} from 'rxjs';
|
||||
import {Message} from 'primeng/message';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-profile-dialog',
|
||||
imports: [
|
||||
Button,
|
||||
Divider,
|
||||
FormsModule,
|
||||
ReactiveFormsModule,
|
||||
InputText,
|
||||
NgIf,
|
||||
Password,
|
||||
Message
|
||||
],
|
||||
templateUrl: './user-profile-dialog.component.html',
|
||||
styleUrls: ['./user-profile-dialog.component.scss']
|
||||
})
|
||||
export class UserProfileDialogComponent implements OnInit, OnDestroy {
|
||||
|
||||
isEditing = false;
|
||||
currentUser: User | null = null;
|
||||
editUserData: Partial<User> = {};
|
||||
private userSubscription: Subscription | null = null;
|
||||
|
||||
changePasswordForm: FormGroup;
|
||||
|
||||
protected readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly fb = inject(FormBuilder);
|
||||
|
||||
constructor() {
|
||||
this.changePasswordForm = this.fb.group(
|
||||
{
|
||||
currentPassword: ['', Validators.required],
|
||||
newPassword: ['', [Validators.required, Validators.minLength(6)]],
|
||||
confirmNewPassword: ['', [Validators.required, Validators.minLength(6)]]
|
||||
},
|
||||
{
|
||||
validators: [UserProfileDialogComponent.passwordsMatchValidator]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.userSubscription = this.userService.userData$.subscribe(user => {
|
||||
this.currentUser = user;
|
||||
this.resetEditForm();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.userSubscription?.unsubscribe();
|
||||
}
|
||||
|
||||
static passwordsMatchValidator(form: FormGroup) {
|
||||
const newPassword = form.get('newPassword')?.value;
|
||||
const confirmPassword = form.get('confirmNewPassword')?.value;
|
||||
return newPassword === confirmPassword ? null : {mismatch: true};
|
||||
}
|
||||
|
||||
get passwordsMismatch() {
|
||||
return this.changePasswordForm.hasError('mismatch') &&
|
||||
this.changePasswordForm.get('confirmNewPassword')?.touched;
|
||||
}
|
||||
|
||||
toggleEdit(): void {
|
||||
this.isEditing = !this.isEditing;
|
||||
if (this.isEditing) {
|
||||
this.resetEditForm();
|
||||
}
|
||||
}
|
||||
|
||||
resetEditForm(): void {
|
||||
if (this.currentUser) {
|
||||
this.editUserData = {
|
||||
username: this.currentUser.username,
|
||||
name: this.currentUser.name,
|
||||
email: this.currentUser.email,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
updateProfile(): void {
|
||||
if (!this.currentUser) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'User data not available.',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.editUserData.name === this.currentUser.name && this.editUserData.email === this.currentUser.email) {
|
||||
this.messageService.add({severity: 'info', summary: 'Info', detail: 'No changes detected.'});
|
||||
this.isEditing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.updateUser(this.currentUser.id, this.editUserData).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Profile updated successfully'});
|
||||
this.isEditing = false;
|
||||
this.resetEditForm();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: err.error?.message || 'Failed to update profile',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
submitPasswordChange(): void {
|
||||
if (this.changePasswordForm.invalid) {
|
||||
this.changePasswordForm.markAllAsTouched();
|
||||
return;
|
||||
}
|
||||
|
||||
const {currentPassword, newPassword} = this.changePasswordForm.value;
|
||||
|
||||
this.userService.changePassword(currentPassword, newPassword).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Password changed successfully'});
|
||||
this.resetPasswordForm();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: err?.message || 'Failed to change password',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
resetPasswordForm(): void {
|
||||
this.changePasswordForm.reset();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user