From 30641db59e6acbe19ff624bb8d65a9dd08623f82 Mon Sep 17 00:00:00 2001 From: Mazdak Gibran <141390141+mazdakgibran@users.noreply.github.com> Date: Wed, 14 Jan 2026 17:52:20 +0500 Subject: [PATCH] setup-user updated setup-user updated to match architecture of other components. --- src/app/services/user-setup.service.ts | 95 ---------- .../setup-user/setup-user.component.html | 72 ++++---- .../setup-user/setup-user.component.ts | 169 +++++++++++------- src/assets/i18n/Arabic.json | 1 + src/assets/i18n/English.json | 1 + 5 files changed, 141 insertions(+), 197 deletions(-) delete mode 100644 src/app/services/user-setup.service.ts diff --git a/src/app/services/user-setup.service.ts b/src/app/services/user-setup.service.ts deleted file mode 100644 index 3ee7018..0000000 --- a/src/app/services/user-setup.service.ts +++ /dev/null @@ -1,95 +0,0 @@ -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 itemsPerPageSubject = new BehaviorSubject(5); - users$ = this.usersSubject.asObservable(); - currentPage$ = this.currentPageSubject.asObservable(); - totalCount$ = this.totalCountSubject.asObservable(); - itemsPerPage$ = this.itemsPerPageSubject.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); - - }, - error: (err) => console.error(err) - }); - } - }); - } - - setItemsPerPage(itemsPerPage: number): void { - this.itemsPerPageSubject.next(itemsPerPage); - this.currentPageSubject.next(1); - - } - - nextPage(): void { - const totalPages = this.getTotalPages(); - const currentPage = this.currentPageSubject.value; - if (currentPage < totalPages) { - this.currentPageSubject.next(currentPage + 1); - } - } - - previousPage(): void { - const currentPage = this.currentPageSubject.value; - if (currentPage > 1) { - this.currentPageSubject.next(currentPage - 1); - - } - } - - goToPage(page: number): void { - const totalPages = this.getTotalPages(); - if (page > 0 && page <= totalPages) { - this.currentPageSubject.next(page); - - } - } - - 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/user-management/setup-user/setup-user.component.html b/src/app/user-management/setup-user/setup-user.component.html index 5564195..53df2b3 100644 --- a/src/app/user-management/setup-user/setup-user.component.html +++ b/src/app/user-management/setup-user/setup-user.component.html @@ -163,7 +163,7 @@
-
+
@@ -175,12 +175,11 @@ - - + + @@ -189,7 +188,7 @@
-
+
@@ -200,30 +199,27 @@ - - - - - - - + + + + + + + + @@ -235,16 +231,19 @@ bindLabel="label" bindValue="value" [(ngModel)]="itemsPerPage" - (change)="onPageSizeChange(itemsPerPage)" + (change)="itemsPerPageChanged()" [searchable]="false" [clearable]="false" [dropdownPosition]="'top'"> -
- {{ 'page' | translate }} {{ currentPage }} {{ 'of' | translate }} {{ getTotalPages() }} ({{ totalCount }} {{ 'totalItems' | translate }}) -
+
+ {{'page' | translate}} {{currentPage}} {{'of' | + translate}} {{totalPages()}} ({{allItems.length}} + {{'totalItems' | translate}}) +
+
- \ No newline at end of file + + +
+

{{'noUserDetailsFound' | translate}}

+
+
\ No newline at end of file diff --git a/src/app/user-management/setup-user/setup-user.component.ts b/src/app/user-management/setup-user/setup-user.component.ts index a411383..48202df 100644 --- a/src/app/user-management/setup-user/setup-user.component.ts +++ b/src/app/user-management/setup-user/setup-user.component.ts @@ -5,22 +5,24 @@ import { NgSelectModule } from '@ng-select/ng-select'; import { TranslateModule } from '@ngx-translate/core'; import { pageSizeOptions } from '../../utils/app.constants'; import { SetupUser } from '../../models/user'; -import { UserSetupService } from '../../services/user-setup.service'; -import { UserFilterPipe } from '../../shared/pipes/userFilterPipe'; import { FormBuilder, Validators, FormGroup } from '@angular/forms'; import { ButtonManagementService } from '../../services/button-management.service'; import { StorageService } from '../../shared/services/storage.service'; +import { TableFilterPipe } from '../../shared/pipes/table-filter.pipe'; +import { URIKey } from '../../utils/uri-enums'; +import { HttpParams } from '@angular/common/http'; +import { HttpURIService } from '../../app.http.uri.service'; + @Component({ selector: 'app-setup-user', standalone: true, - imports: [TranslateModule, ReactiveFormsModule, FormsModule, CommonModule, NgSelectModule, UserFilterPipe], + imports: [TranslateModule, ReactiveFormsModule, FormsModule, CommonModule, NgSelectModule, TableFilterPipe], templateUrl: './setup-user.component.html', styleUrl: './setup-user.component.scss' }) export class SetupUserComponent implements OnInit { - userForm!: FormGroup; showForm = false; selectedUserId!: any; @@ -30,11 +32,16 @@ export class SetupUserComponent implements OnInit { currentPage: number = 1; pageSizeOptions = pageSizeOptions itemsPerPage: number = 5; - searchText: string = ''; + pagedItems: any[] = []; + searchText: any = ''; renewalDataExpanded: boolean = true; totalCount: number = 0; mode: 'edit' | 'view' = 'view'; + + userSetupDataExpanded: boolean = true buttonPermissions: any; + isLoading: boolean = false; + setupUserList: SetupUser[] = []; roleOptions = [ { label: 'Admin', value: 'ADMIN' }, { label: 'User', value: 'USER' } @@ -42,41 +49,41 @@ export class SetupUserComponent implements OnInit { constructor( - private userService: UserSetupService, private fb: FormBuilder, private buttonManagementService: ButtonManagementService, - private storageService: StorageService + private storageService: StorageService, + private httpService: HttpURIService ){} - get users$(){ - return this.userService.users$; - } - onSearch(value: string): void { - this.searchText = value; -} - - onPageSizeChange(pageSize: number): void { - this.userService.setItemsPerPage(pageSize); + this.searchText = value; } - nextPage(): void { - this.userService.nextPage(); + totalPages(): number { + return Math.ceil(this.allItems.length / this.itemsPerPage); } previousPage(): void { - this.userService.previousPage(); + if (this.currentPage > 1) { + this.currentPage--; + } + } + + nextPage(): void { + if (this.currentPage < this.totalPages()) { + this.currentPage++; + } } - - getTotalPages(): number { - return this.userService.getTotalPages(); + itemsPerPageChanged(): void { + this.currentPage = 1; + this.updatePagedItems(); } onSubmit() { if (this.userForm.invalid) { - this.userForm.markAllAsTouched(); - return; -} + this.userForm.markAllAsTouched(); + return; + } const newUser : SetupUser = { userId: this.userForm.value.userId, @@ -87,12 +94,11 @@ export class SetupUserComponent implements OnInit { password: this.userForm.value.defaultPassword } - this.userService.addUser(newUser).subscribe({ + this.httpService.requestPOST(URIKey.CREATE_USER, newUser).subscribe({ next: () => { - this.userService.loadUsers(); - this.userService.loadUsers(); this.userForm.reset(); this.mode = 'edit'; + this.loadUsersDirect() }, error: (err: any) => console.error(err) @@ -101,33 +107,19 @@ export class SetupUserComponent implements OnInit { } - onView(userId: any){ - this.mode = 'view'; - this.showForm = true; - this.selectedUserId = userId; - this.userService.getUserById(userId).subscribe((user: any)=>{ - this.userForm.patchValue({ - userId : user.userId, - userFullname : user.userFullname, - defaultPassword : '', - }) - }) - } - - onDelete(userId: any){ - this.userService.deleteUser(userId).subscribe({ - next: (res: any) => { - this.userService.loadUsers(); - this.userForm.reset() - this.selectedUserId = null; - }, - error: (err:any) =>{ - console.log('user not deleted') - } - - }); + updatePagedItems(): void { + const startIndex = (this.currentPage - 1) * this.itemsPerPage; + const endIndex = startIndex + this.itemsPerPage; + this.pagedItems = this.allItems.slice(startIndex, endIndex); +} + getButtonPermissions(){ + this.buttonPermissions = this.buttonManagementService.buttonPermissions["setupUser"]; } + + toggleTableCard(): void { + this.userSetupDataExpanded = !this.userSetupDataExpanded; +} ngOnInit(): void { @@ -140,25 +132,66 @@ ngOnInit(): void { email: ['', [Validators.required, Validators.email]], userRole: [null, Validators.required] }); - - this.userService.loadUsers(); + this.loadUsersDirect(); + } - this.userService.users$.subscribe((users: SetupUser[]) => { - this.allItems = users; - }); - this.userService.currentPage$.subscribe((page: number) => { - this.currentPage = page; - }); - this.userService.totalCount$.subscribe((count: number) => { - this.totalCount = count; + loadUsersDirect(): void { + this.isLoading = false; + let params = new HttpParams() + .set('page', this.currentPage.toString()) + .set('size', this.itemsPerPage.toString()); + + this.httpService.requestGET(URIKey.GET_ALL_USER_URI, params).subscribe({ + next: (response) => { + this.setupUserList = response + this.allItems = [...this.setupUserList]; + this.updatePagedItems(); + this.isLoading = false; + }, + error: (err) => { + console.error('Error fetching users:', err); + this.allItems = []; + this.isLoading = false; + } }); - this.userService.itemsPerPage$.subscribe((size: number) => { - this.itemsPerPage = size; + } + + onView(userId: any): void{ + let params = new HttpParams().set('userId', userId); + this.httpService.requestGET(URIKey.GET_USER_BY_ID, params).subscribe({ + next: (response: SetupUser) => { + this.userForm.patchValue({ + userId: response.userId, + userFullname: response.userFullname, + email: response.email, + userRole: response.role, + defaultPassword: '' + }); + this.selectedUserId = userId; + this.isLoading = false; + }, + error: (err) => { + console.error('Error fetching users:', err); + this.allItems = []; + this.isLoading = false; + } }); -} + } - getButtonPermissions(){ - this.buttonPermissions = this.buttonManagementService.buttonPermissions["setupUser"]; + onDelete(userId: any){ + let params = new HttpParams().set('userId', userId); + this.httpService.requestDELETE(URIKey.DELETE_USER, params).subscribe({ + next: (response) =>{ + this.loadUsersDirect(); + this.userForm.reset() + this.selectedUserId = null; + }, + error: (err) =>{ + console.error('Error fetching users:', err); + this.allItems = []; + this.isLoading = false; + } + }) } } diff --git a/src/assets/i18n/Arabic.json b/src/assets/i18n/Arabic.json index aa3054f..a145ad4 100644 --- a/src/assets/i18n/Arabic.json +++ b/src/assets/i18n/Arabic.json @@ -249,6 +249,7 @@ "allow": "يسمح", "toDateInvalidError": "يجب أن يكون تاريخ اليوم أكبر من أو يساوي تاريخ البداية", "noLoggingDetailsFound": "لم يتم العثور على تفاصيل التسجيل", + "noUserDetailsFound": "لم يتم العثور على تفاصيل المستخدم", "ERR_SEC_0001": "البريد الإلكتروني موجود بالفعل", "ERR_SEC_0002": "اسم المستخدم موجود بالفعل", "ERR_SEC_0003": "كلمة المرور القديمة غير صحيحة", diff --git a/src/assets/i18n/English.json b/src/assets/i18n/English.json index a1fd3e9..bd969fb 100644 --- a/src/assets/i18n/English.json +++ b/src/assets/i18n/English.json @@ -249,6 +249,7 @@ "allow": "Allow", "toDateInvalidError": "To Date must be greater than or equal to From Date", "noLoggingDetailsFound": "No Logging Details found", + "noUserDetailsFound":"No User Details found", "ERR_SEC_0001": "Email already exists", "ERR_SEC_0002": "Username already exists", "ERR_SEC_0003": "Old Password is not correct",
{{ item.userId }}{{ item.userFullname }} -
- - - - - -
-
{{ item.userId }}{{ item.userFullname }} +
+ + + + + +
+