From bc18bcf946c4d136c0b897e7dcc68ae0ebf0a6cc Mon Sep 17 00:00:00 2001 From: Mazdak Gibran <141390141+mazdakgibran@users.noreply.github.com> Date: Mon, 29 Dec 2025 15:15:47 +0500 Subject: [PATCH 1/2] Setup User API Integration createUser and getAllUsers API Integration --- src/app/models/user.ts | 10 +- src/app/services/user-setup.service.ts | 112 ++++++++++++++++++ .../change-password.component.html | 2 +- .../setup-user/setup-user.component.html | 54 +++++---- .../setup-user/setup-user.component.ts | 72 +++++++++-- src/app/utils/uri-enums.ts | 4 +- src/assets/data/app.uri.json | 10 ++ src/assets/i18n/Arabic.json | 2 + src/assets/i18n/English.json | 2 + 9 files changed, 231 insertions(+), 37 deletions(-) create mode 100644 src/app/services/user-setup.service.ts 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..99c0faa --- /dev/null +++ b/src/app/services/user-setup.service.ts @@ -0,0 +1,112 @@ +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'; + +@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); +} + +} \ No newline at end of file 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/setup-user/setup-user.component.html b/src/app/user-management/setup-user/setup-user.component.html index 988ee25..1522bbf 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/>