feat: add profile screen
This commit is contained in:
committed by
Aditya Chandel
parent
ad6f848752
commit
afb4c86c83
+12
-10
@@ -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<Long> 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<Long> libraryIds = updateRequest.getAssignedLibraries();
|
||||
List<LibraryEntity> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]},
|
||||
|
||||
@@ -39,6 +39,11 @@
|
||||
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"/>
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<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>
|
||||
</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>
|
||||
<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>
|
||||
|
||||
<div class="p-4">
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h2 class="text-xl">Change Password</h2>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
|
||||
<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">
|
||||
Loading user data...
|
||||
</div>
|
||||
@@ -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<User> = {};
|
||||
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'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<User[]>(this.userUrl);
|
||||
}
|
||||
|
||||
updateUser(userId: number, updateData: Partial<User>): Observable<{ message: string }> {
|
||||
return this.http.put<{ message: string }>(`${this.userUrl}/${userId}`, updateData);
|
||||
updateUser(userId: number, updateData: Partial<User>): Observable<User> {
|
||||
return this.http.put<User>(`${this.userUrl}/${userId}`, updateData).pipe(
|
||||
tap(user => {
|
||||
this.userDataSubject.next(user);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
deleteUser(userId: number): Observable<void> {
|
||||
|
||||
Reference in New Issue
Block a user