diff --git a/apps/mina-sidor-fa/src/app/pages/administration/pages/employees/employees.component.html b/apps/mina-sidor-fa/src/app/pages/administration/pages/employees/employees.component.html index 7b8bef2..e0e9dd8 100644 --- a/apps/mina-sidor-fa/src/app/pages/administration/pages/employees/employees.component.html +++ b/apps/mina-sidor-fa/src/app/pages/administration/pages/employees/employees.component.html @@ -19,7 +19,7 @@

Personallista

-
+ ): void { diff --git a/apps/mina-sidor-fa/src/app/shared/services/api/employee.service.ts b/apps/mina-sidor-fa/src/app/shared/services/api/employee.service.ts index f4b0c06..49be182 100644 --- a/apps/mina-sidor-fa/src/app/shared/services/api/employee.service.ts +++ b/apps/mina-sidor-fa/src/app/shared/services/api/employee.service.ts @@ -23,19 +23,35 @@ import { ErrorService } from '@msfa-services/error.service'; import { BehaviorSubject, combineLatest, Observable, of, throwError } from 'rxjs'; import { catchError, distinctUntilChanged, filter, map, switchMap, take, tap } from 'rxjs/operators'; +interface EmployeeParams { + page: number; + limit: number; + sort: keyof EmployeeCompactResponse; + order: SortOrder; + search: string; + onlyEmployeesWithoutAuthorization: boolean; +} + @Injectable({ providedIn: 'root', }) export class EmployeeService extends UnsubscribeDirective { private _apiBaseUrl = `${environment.api.url}/users`; private _currentEmployeeId$ = new BehaviorSubject(null); - private _limit$ = new BehaviorSubject(10); - private _page$ = new BehaviorSubject(1); - private _sort$ = new BehaviorSubject>({ key: 'name', order: SortOrder.ASC }); - public sort$: Observable> = this._sort$.asObservable(); - private _searchFilter$ = new BehaviorSubject(''); - private _onlyEmployeesWithoutAuthorization$ = new BehaviorSubject(false); - public onlyEmployeesWithoutAuthorization$: Observable = this._onlyEmployeesWithoutAuthorization$.asObservable(); + private _params$ = new BehaviorSubject({ + page: 1, + limit: 10, + sort: 'name', + order: SortOrder.ASC, + search: '', + onlyEmployeesWithoutAuthorization: false, + }); + public sort$: Observable> = this._params$.pipe( + map(({ sort, order }) => ({ key: sort, order })) + ); + public onlyEmployeesWithoutAuthorization$: Observable = this._params$.pipe( + map(({ onlyEmployeesWithoutAuthorization }) => onlyEmployeesWithoutAuthorization) + ); private _employee$ = new BehaviorSubject(null); public employee$: Observable = this._employee$.asObservable(); private _lastUpdatedEmployeeId$ = new BehaviorSubject(null); @@ -67,17 +83,8 @@ export class EmployeeService extends UnsubscribeDirective { ); } - public employeesData$: Observable = combineLatest([ - this._limit$, - this._page$, - this._sort$, - this._searchFilter$, - this._onlyEmployeesWithoutAuthorization$, - this._lastDeletedEmployee$, - ]).pipe( - switchMap(([limit, page, sort, searchFilter, onlyEmployeesWithoutAuthorization]) => - this._fetchEmployees$(limit, page, sort, searchFilter, onlyEmployeesWithoutAuthorization) - ) + public employeesData$: Observable = combineLatest([this._params$, this._lastDeletedEmployee$]).pipe( + switchMap(([params]) => this._fetchEmployees$(params)) ); public setCurrentEmployeeId(currentEmployeeId: string): void { @@ -91,26 +98,21 @@ export class EmployeeService extends UnsubscribeDirective { this._lastUpdatedEmployeeId$.next(null); } - private _fetchEmployees$( - limit: number, - page: number, - sort: Sort, - searchFilter: string, - onlyEmployeesWithoutAuthorization?: boolean - ): Observable { + private _fetchEmployees$(employeeParams: EmployeeParams): Observable { + const { sort, order, limit, page, search, onlyEmployeesWithoutAuthorization } = employeeParams; const params: Params = { - sort: sort.key as string, - order: sort.order as string, + sort, + order, limit: limit.toString(), page: page.toString(), }; - if (searchFilter) { - params.search = searchFilter; + if (search) { + params.search = search; } if (onlyEmployeesWithoutAuthorization) { - params.onlyEmployeesWithoutAuthorization = onlyEmployeesWithoutAuthorization?.toString(); + params.onlyEmployeesWithoutAuthorization = onlyEmployeesWithoutAuthorization.toString(); } this._employeesLoading$.next(true); @@ -134,12 +136,20 @@ export class EmployeeService extends UnsubscribeDirective { ); } - public setSearchFilter(value: string): void { - this._searchFilter$.next(value); + public setSearchString(value: string): void { + this._params$.next({ + ...this._params$.getValue(), + search: value, + page: 1, + }); } public setOnlyEmployeesWithoutAuthorization(value: boolean): void { - this._onlyEmployeesWithoutAuthorization$.next(value); + this._params$.next({ + ...this._params$.getValue(), + onlyEmployeesWithoutAuthorization: value, + page: 1, + }); } public setEmployeeToDelete(employee: Employee): void { @@ -156,16 +166,17 @@ export class EmployeeService extends UnsubscribeDirective { ); } - public setSort(newSortKey: keyof EmployeeCompactResponse): void { - const currentSort = this._sort$.getValue(); - const order = - currentSort.key === newSortKey && currentSort.order === SortOrder.ASC ? SortOrder.DESC : SortOrder.ASC; + public setSort(sort: keyof EmployeeCompactResponse): void { + const currentParams = this._params$.getValue(); + const currentSort = currentParams.sort; + const currentOrder = currentParams.order; + const order = currentSort === sort && currentOrder === SortOrder.ASC ? SortOrder.DESC : SortOrder.ASC; - this._sort$.next({ key: newSortKey, order }); + this._params$.next({ ...this._params$.getValue(), sort, order }); } public setPage(page: number): void { - this._page$.next(page); + this._params$.next({ ...this._params$.getValue(), page }); } public postEmployeeInvitation(emails: string[]): Observable {