From 71bc927d54bb3387e5c0f664b7e257751e1ea6ce Mon Sep 17 00:00:00 2001 From: adityachandelgit <> Date: Sun, 27 Apr 2025 14:49:25 -0600 Subject: [PATCH] feat(user-profile): display user information and password change form in dialog --- .../layout-topbar/app.topbar.component.html | 47 +++--- .../layout-topbar/app.topbar.component.ts | 13 +- .../src/app/profile/profile.component.html | 103 ++++++------ .../src/app/profile/profile.component.ts | 26 +-- .../user-profile-dialog.component.html | 81 ++++++++++ .../user-profile-dialog.component.scss | 0 .../user-profile-dialog.component.ts | 151 ++++++++++++++++++ 7 files changed, 335 insertions(+), 86 deletions(-) create mode 100644 booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.html create mode 100644 booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.scss create mode 100644 booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.ts diff --git a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html index c520f57cc..441b5650b 100644 --- a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html +++ b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html @@ -39,11 +39,6 @@ pTooltip="Settings" tooltipPosition="top"> -
  • - -
  • @@ -61,25 +56,31 @@ -
  • - - -
  • - -
  • - -
  • +
    +
  • + + +
  • +
  • + +
  • +
  • + +
  • +
    diff --git a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.ts b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.ts index 0f57c3d01..4bd02f378 100644 --- a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.ts +++ b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.ts @@ -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() { diff --git a/booklore-ui/src/app/profile/profile.component.html b/booklore-ui/src/app/profile/profile.component.html index d096af146..7ba611998 100644 --- a/booklore-ui/src/app/profile/profile.component.html +++ b/booklore-ui/src/app/profile/profile.component.html @@ -1,60 +1,65 @@ -
    -
    -
    -

    User Profile

    -
    - -
    - - +
    +
    + + +
    +
    +

    Profile Information

    +
    + +
    + + +
    -
    -
    -
    - -

    {{ currentUser.username }}

    - +
    +
    + +

    {{ currentUser.username }}

    + +
    +
    + +

    {{ currentUser.name }}

    + +
    +
    + +

    {{ currentUser.email }}

    + +
    -
    - -

    {{ currentUser.name }}

    - -
    -
    - -

    {{ currentUser.email }}

    - -
    -
    -
    + -
    -
    -

    Change Password

    -
    + -
    -
    - - -
    + +
    +

    Change Password

    + +
    + + +
    +
    + + +
    +
    + + +
    +
    + +
    + +
    -
    - - -
    - -
    - - -
    - - -
    -
    + +
    Loading user data...
    diff --git a/booklore-ui/src/app/profile/profile.component.ts b/booklore-ui/src/app/profile/profile.component.ts index 792afc527..10196c2d2 100644 --- a/booklore-ui/src/app/profile/profile.component.ts +++ b/booklore-ui/src/app/profile/profile.component.ts @@ -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' + }); + } + }); } } diff --git a/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.html b/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.html new file mode 100644 index 000000000..d4b41f0bc --- /dev/null +++ b/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.html @@ -0,0 +1,81 @@ + + +
    + +
    +
    +

    User Profile

    +
    +
    +
    + +

    {{ currentUser?.username || 'N/A' }}

    + +
    + +
    + +

    {{ currentUser?.name || 'N/A' }}

    + +
    + +
    + +

    {{ currentUser?.email || 'N/A' }}

    + +
    +
    + +
    +
    + +
    +
    + + +
    +
    +
    +
    +
    + + + +
    +
    +

    Change Password

    +
    +
    +
    +
    + + + +
    + +
    + + + +
    + +
    + + + +
    + +
    + +
    +
    +
    +
    +
    +
    + +
    + +
    + Loading user data... +
    diff --git a/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.scss b/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.ts b/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.ts new file mode 100644 index 000000000..25461ea48 --- /dev/null +++ b/booklore-ui/src/app/user-profile-dialog/user-profile-dialog.component.ts @@ -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 = {}; + 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(); + } +}