diff --git a/src/app/models/user.ts b/src/app/models/user.ts index fbf6405..9844065 100644 --- a/src/app/models/user.ts +++ b/src/app/models/user.ts @@ -2,4 +2,12 @@ export class User { Username: string=""; Email?: string=""; Password: string=""; -} \ No newline at end of file +} + +export interface SetupUser { + userId: string; + userFullname: string; + defaultPassword: string; + email: string; + role: string; +} diff --git a/src/app/services/user-setup.service.ts b/src/app/services/user-setup.service.ts new file mode 100644 index 0000000..b26f4c2 --- /dev/null +++ b/src/app/services/user-setup.service.ts @@ -0,0 +1,126 @@ +import { Injectable } from '@angular/core'; +import { SetupUser } from '../models/user'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { URIKey } from '../utils/uri-enums'; +import { URIService } from '../app.uri'; +import { HttpURIService } from '../app.http.uri.service'; +import { HttpParams } from '@angular/common/http'; + +@Injectable({ + providedIn: 'root' +}) +export class UserSetupService { + private usersSubject = new BehaviorSubject([]); + private currentPageSubject = new BehaviorSubject(1); + private totalCountSubject = new BehaviorSubject(0); + private searchTextSubject = new BehaviorSubject(''); + private itemsPerPageSubject = new BehaviorSubject(5); + private paginatedUsersSubject = new BehaviorSubject([]); + users$ = this.usersSubject.asObservable(); + currentPage$ = this.currentPageSubject.asObservable(); + totalCount$ = this.totalCountSubject.asObservable(); + searchText$ = this.searchTextSubject.asObservable(); + itemsPerPage$ = this.itemsPerPageSubject.asObservable(); + paginatedUsers$ = this.paginatedUsersSubject.asObservable(); + + constructor(private httpURIService: HttpURIService, private uriService: URIService) { } + +loadUsers(): void { + this.uriService.canSubscribe.subscribe(can => { + if (can) { + this.httpURIService + .requestGET(URIKey.GET_ALL_USERS) + .subscribe({ + next: (res) => { + const users = Array.isArray(res) ? res : res?.data; + this.usersSubject.next(users ?? []); + this.totalCountSubject.next(users.length); + this.applyPagination(); + }, + error: (err) => console.error(err) + }); + } + }); + } + + private applyPagination(): void { + const allUsers = this.usersSubject.value; + const searchText = this.searchTextSubject.value.toLowerCase(); + const currentPage = this.currentPageSubject.value; + const itemsPerPage = this.itemsPerPageSubject.value; + + let filtered = allUsers.filter(user => + user.userId.toLowerCase().includes(searchText) || + user.userFullname.toLowerCase().includes(searchText) || + user.email.toLowerCase().includes(searchText) + ); + + const totalCount = filtered.length; + const startIndex = (currentPage - 1) * itemsPerPage; + const paginatedUsers = filtered.slice(startIndex, startIndex + itemsPerPage); + + this.paginatedUsersSubject.next(paginatedUsers); + this.totalCountSubject.next(totalCount); + } + + setSearchText(searchText: string): void { + this.searchTextSubject.next(searchText); + this.currentPageSubject.next(1); + this.applyPagination(); + } + + setItemsPerPage(itemsPerPage: number): void { + this.itemsPerPageSubject.next(itemsPerPage); + this.currentPageSubject.next(1); + this.applyPagination(); + } + + nextPage(): void { + const totalPages = this.getTotalPages(); + const currentPage = this.currentPageSubject.value; + if (currentPage < totalPages) { + this.currentPageSubject.next(currentPage + 1); + this.applyPagination(); + } + } + + previousPage(): void { + const currentPage = this.currentPageSubject.value; + if (currentPage > 1) { + this.currentPageSubject.next(currentPage - 1); + this.applyPagination(); + } + } + + goToPage(page: number): void { + const totalPages = this.getTotalPages(); + if (page > 0 && page <= totalPages) { + this.currentPageSubject.next(page); + this.applyPagination(); + } + } + + getTotalPages(): number { + const totalCount = this.totalCountSubject.value; + const itemsPerPage = this.itemsPerPageSubject.value; + return Math.ceil(totalCount / itemsPerPage); + } + + addUser(payload: SetupUser): Observable { + return this.httpURIService.requestPOST(URIKey.CREATE_USER, payload); +} + + +getUserById(userId: any){ + const params = new HttpParams().set('userId', userId) + + return this.httpURIService.requestGET(URIKey.GET_USER_BY_ID, params); +} + +deleteUser(userId: any){ + const params = new HttpParams().set('userId', userId) + console.log("params success",params) + return this.httpURIService.requestDELETE(URIKey.DELETE_USER, params) +} + +} \ No newline at end of file diff --git a/src/app/shared/components/side-nav/side-nav.component.html b/src/app/shared/components/side-nav/side-nav.component.html index b8f4024..a4d7e2b 100644 --- a/src/app/shared/components/side-nav/side-nav.component.html +++ b/src/app/shared/components/side-nav/side-nav.component.html @@ -30,7 +30,7 @@
  • - + {{ 'changePassword' | translate }}
  • diff --git a/src/app/shared/components/side-nav/side-nav.component.ts b/src/app/shared/components/side-nav/side-nav.component.ts index 975a608..269b107 100644 --- a/src/app/shared/components/side-nav/side-nav.component.ts +++ b/src/app/shared/components/side-nav/side-nav.component.ts @@ -40,6 +40,12 @@ export class SideNavComponent { this.sidebarService.currentSubModule = this.storageService.getItem('currentSubModule') ?? 'dashboard'; this.closeSidebarMenu(); } + + navigateToChangePassword() { + this.router.navigate(['/home/changePassword'], { + state: { fromMenu: true } + }); +} closeSidebarMenu(): void { if (isPlatformBrowser(this.platformId)) { const subMenus = document.querySelectorAll('#sidebar-menu .sub-menu'); diff --git a/src/app/user-management/change-password/change-password.component.html b/src/app/user-management/change-password/change-password.component.html index 219142f..a194aec 100644 --- a/src/app/user-management/change-password/change-password.component.html +++ b/src/app/user-management/change-password/change-password.component.html @@ -47,7 +47,7 @@
    -
    diff --git a/src/app/user-management/change-password/change-password.component.ts b/src/app/user-management/change-password/change-password.component.ts index dc6d5bd..fb7f0bd 100644 --- a/src/app/user-management/change-password/change-password.component.ts +++ b/src/app/user-management/change-password/change-password.component.ts @@ -41,18 +41,30 @@ passwordType2: string = 'password'; } ngOnInit(): void { + // Call the method to check if first-time login this.checkIfFirstTimeChangePasswordOrNot(); } - checkIfFirstTimeChangePasswordOrNot(){ - let currentUser: any = JSON.parse(this.storageService.getItem('user')!) - if(currentUser?.user?.isFirstLogin){ - this.isFirstLogin = true; - } - else{ + checkIfFirstTimeChangePasswordOrNot() { + const fromMenu = history.state?.['fromMenu']; + + if (fromMenu) { this.isFirstLogin = false; + } else { + try { + const currentUser: any = JSON.parse(this.storageService.getItem('user') || '{}'); + + // Check if user exists and has isFirstLogin flag + if (currentUser?.user?.isFirstLogin) { + this.isFirstLogin = true; + } else { + this.isFirstLogin = false; + } + } catch (error) { + console.error('Error parsing user data:', error); + this.isFirstLogin = false; + } } } - } diff --git a/src/app/user-management/setup-user/setup-user.component.html b/src/app/user-management/setup-user/setup-user.component.html index 988ee25..b8a681c 100644 --- a/src/app/user-management/setup-user/setup-user.component.html +++ b/src/app/user-management/setup-user/setup-user.component.html @@ -26,15 +26,16 @@
    -
    - @@ -54,9 +55,10 @@
    - @@ -71,15 +73,16 @@
    -
    - + [(ngModel)]="defaultPassword" + name="defaultPassword" + placeholder="{{ 'passwordPlaceHolder' | translate }}" appNoWhitespaces/>