From afb4c86c836256a63cac794a3147f82ab5b32f52 Mon Sep 17 00:00:00 2001 From: Oleh Astappiev <4512729+astappiev@users.noreply.github.com> Date: Sat, 26 Apr 2025 12:09:22 +0200 Subject: [PATCH] feat: add profile screen --- .../booklore/service/user/UserService.java | 22 +-- booklore-ui/src/app/app.routes.ts | 3 +- .../layout-topbar/app.topbar.component.html | 5 + .../layout-topbar/app.topbar.component.ts | 4 + .../src/app/profile/profile.component.html | 60 ++++++++ .../src/app/profile/profile.component.scss | 0 .../src/app/profile/profile.component.ts | 133 ++++++++++++++++++ booklore-ui/src/app/user.service.ts | 16 +-- 8 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 booklore-ui/src/app/profile/profile.component.html create mode 100644 booklore-ui/src/app/profile/profile.component.scss create mode 100644 booklore-ui/src/app/profile/profile.component.ts diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java index 32223bd8a..1420dd65c 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java @@ -40,16 +40,18 @@ public class UserService { public BookLoreUser updateUser(Long id, UserUpdateRequest updateRequest) { BookLoreUserEntity user = userRepository.findById(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id)); - user.setName(updateRequest.getName()); user.setEmail(updateRequest.getEmail()); - user.getPermissions().setPermissionUpload(updateRequest.getPermissions().isCanUpload()); - user.getPermissions().setPermissionDownload(updateRequest.getPermissions().isCanDownload()); - user.getPermissions().setPermissionEditMetadata(updateRequest.getPermissions().isCanEditMetadata()); - user.getPermissions().setPermissionEmailBook(updateRequest.getPermissions().isCanEmailBook()); - List libraryIds = updateRequest.getAssignedLibraries(); - if (libraryIds != null) { + if (updateRequest.getPermissions() != null) { + user.getPermissions().setPermissionUpload(updateRequest.getPermissions().isCanUpload()); + user.getPermissions().setPermissionDownload(updateRequest.getPermissions().isCanDownload()); + user.getPermissions().setPermissionEditMetadata(updateRequest.getPermissions().isCanEditMetadata()); + user.getPermissions().setPermissionEmailBook(updateRequest.getPermissions().isCanEmailBook()); + } + + if (updateRequest.getAssignedLibraries() != null) { + List libraryIds = updateRequest.getAssignedLibraries(); List updatedLibraries = libraryRepository.findAllById(libraryIds); user.setLibraries(updatedLibraries); } @@ -104,7 +106,7 @@ public class UserService { throw ApiError.PASSWORD_SAME_AS_CURRENT.createException(); } - if (!isValidPassword(changePasswordRequest.getNewPassword())) { + if (!meetsMinimumPasswordRequirements(changePasswordRequest.getNewPassword())) { throw ApiError.PASSWORD_TOO_SHORT.createException(); } @@ -115,14 +117,14 @@ public class UserService { public void changeUserPassword(ChangeUserPasswordRequest request) { BookLoreUserEntity userEntity = userRepository.findById(request.getUserId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(request.getUserId())); - if (!isValidPassword(request.getNewPassword())) { + if (!meetsMinimumPasswordRequirements(request.getNewPassword())) { throw ApiError.PASSWORD_TOO_SHORT.createException(); } userEntity.setPasswordHash(passwordEncoder.encode(request.getNewPassword())); userRepository.save(userEntity); } - private boolean isValidPassword(String password) { + private boolean meetsMinimumPasswordRequirements(String password) { return password != null && password.length() >= 6; } } diff --git a/booklore-ui/src/app/app.routes.ts b/booklore-ui/src/app/app.routes.ts index 71e17e8d4..b9600127a 100644 --- a/booklore-ui/src/app/app.routes.ts +++ b/booklore-ui/src/app/app.routes.ts @@ -5,10 +5,10 @@ import {AppLayoutComponent} from './layout/component/layout-main/app.layout.comp import {LoginComponent} from './core/component/login/login.component'; import {AuthGuard} from './auth.guard'; import {SettingsComponent} from './settings/settings.component'; +import {ProfileComponent} from './profile/profile.component'; import {PdfViewerComponent} from './book/components/pdf-viewer/pdf-viewer.component'; import {EpubViewerComponent} from './book/components/epub-viewer/component/epub-viewer.component'; import {ChangePasswordComponent} from './core/component/change-password/change-password.component'; -import {EmailComponent} from './settings/email/email.component'; import {BookMetadataCenterComponent} from './metadata/book-metadata-center/book-metadata-center.component'; export const routes: Routes = [ @@ -23,6 +23,7 @@ export const routes: Routes = [ children: [ {path: 'dashboard', component: MainDashboardComponent, canActivate: [AuthGuard]}, {path: 'all-books', component: BookBrowserComponent, canActivate: [AuthGuard]}, + {path: 'profile', component: ProfileComponent, canActivate: [AuthGuard]}, {path: 'settings', component: SettingsComponent, canActivate: [AuthGuard]}, {path: 'library/:libraryId/books', component: BookBrowserComponent, canActivate: [AuthGuard]}, {path: 'shelf/:shelfId/books', component: BookBrowserComponent, canActivate: [AuthGuard]}, 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 e9e95627e..c520f57cc 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,6 +39,11 @@ pTooltip="Settings" tooltipPosition="top"> +
  • + +
  • 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 501040126..0f57c3d01 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 @@ -118,6 +118,10 @@ export class AppTopBarComponent implements OnDestroy { this.router.navigate(['/settings']); } + navigateToProfile() { + this.router.navigate(['/profile']); + } + logout() { this.authService.logout(); window.location.href = '/login'; diff --git a/booklore-ui/src/app/profile/profile.component.html b/booklore-ui/src/app/profile/profile.component.html new file mode 100644 index 000000000..d096af146 --- /dev/null +++ b/booklore-ui/src/app/profile/profile.component.html @@ -0,0 +1,60 @@ +
    +
    +
    +

    User Profile

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

    {{ currentUser.username }}

    + +
    +
    + +

    {{ currentUser.name }}

    + +
    +
    + +

    {{ currentUser.email }}

    + +
    +
    +
    + +
    +
    +

    Change Password

    +
    + +
    +
    + + +
    + +
    + + +
    + +
    + + +
    + + + +
    +
    +
    + Loading user data... +
    diff --git a/booklore-ui/src/app/profile/profile.component.scss b/booklore-ui/src/app/profile/profile.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/booklore-ui/src/app/profile/profile.component.ts b/booklore-ui/src/app/profile/profile.component.ts new file mode 100644 index 000000000..792afc527 --- /dev/null +++ b/booklore-ui/src/app/profile/profile.component.ts @@ -0,0 +1,133 @@ +import {Component, inject, OnDestroy, OnInit} from '@angular/core'; +import {FormsModule} from '@angular/forms'; +import {Button} from 'primeng/button'; +import {NgIf} from '@angular/common'; +import {User, UserService} from '../user.service'; +import {MessageService} from 'primeng/api'; +import {Password} from 'primeng/password'; +import {InputTextModule} from 'primeng/inputtext'; +import {Subscription} from 'rxjs'; +import {ToastModule} from 'primeng/toast'; + +@Component({ + selector: 'app-profile', + standalone: true, + imports: [ + FormsModule, + Button, + NgIf, + Password, + InputTextModule, + ToastModule, + ], + templateUrl: './profile.component.html', + styleUrl: './profile.component.scss', +}) +export class ProfileComponent implements OnInit, OnDestroy { + protected userService = inject(UserService); + private messageService = inject(MessageService); + + currentPassword: string = ''; + newPassword: string = ''; + confirmNewPassword: string = ''; + + isEditing = false; + currentUser: User | null = null; + editUserData: Partial = {}; + private userSubscription: Subscription | null = null; + + ngOnInit(): void { + this.userSubscription = this.userService.userData$.subscribe(user => { + this.currentUser = user; + this.resetEditForm(); + }); + } + + ngOnDestroy(): void { + this.userSubscription?.unsubscribe(); + } + + 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, + }; + } + } + + saveChanges(): 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' + }); + } + }); + } + + resetPasswordForm(): void { + this.currentPassword = ''; + this.newPassword = ''; + this.confirmNewPassword = ''; + } + + submitPasswordChange() { + if (!this.currentUser) { + this.messageService.add({severity: 'error', summary: 'Error', detail: 'User data not available.'}); + return; + } + + if (!this.currentPassword || !this.newPassword || !this.confirmNewPassword) { + this.messageService.add({severity: 'error', summary: 'Error', detail: 'All fields are required'}); + return; + } + + if (this.newPassword !== this.confirmNewPassword) { + this.messageService.add({severity: 'error', summary: 'Error', detail: 'Passwords do not match'}); + this.newPassword = ''; + this.confirmNewPassword = ''; + return; + } + + 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' + }); + } + }); + } +} diff --git a/booklore-ui/src/app/user.service.ts b/booklore-ui/src/app/user.service.ts index 36685ec31..692ec0bd1 100644 --- a/booklore-ui/src/app/user.service.ts +++ b/booklore-ui/src/app/user.service.ts @@ -1,8 +1,7 @@ import {inject, Injectable, Injector} from '@angular/core'; import {HttpClient} from '@angular/common/http'; -import {BehaviorSubject, Observable, of, throwError} from 'rxjs'; +import {BehaviorSubject, Observable, tap, throwError} from 'rxjs'; import {API_CONFIG} from './config/api-config'; -import {jwtDecode} from 'jwt-decode'; import {RxStompService} from './shared/websocket/rx-stomp.service'; import {Library} from './book/model/library.model'; import {catchError} from 'rxjs/operators'; @@ -41,11 +40,6 @@ export interface UserBookPreferences { }; } -interface JwtPayload { - sub: string; - userId: number; -} - @Injectable({ providedIn: 'root' }) @@ -84,8 +78,12 @@ export class UserService { return this.http.get(this.userUrl); } - updateUser(userId: number, updateData: Partial): Observable<{ message: string }> { - return this.http.put<{ message: string }>(`${this.userUrl}/${userId}`, updateData); + updateUser(userId: number, updateData: Partial): Observable { + return this.http.put(`${this.userUrl}/${userId}`, updateData).pipe( + tap(user => { + this.userDataSubject.next(user); + }) + ); } deleteUser(userId: number): Observable {