Merge pull request #150 in TEA/mina-sidor-fa-web from bugfix/TV-676 to next

Squashed commit of the following:

commit e1e440d157044f59537f976273dfc810452c2108
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Sep 28 13:14:36 2021 +0200

    Changed variable-name

commit 1b23131743876338fd231382548ee6f51731e72a
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Sep 28 12:05:02 2021 +0200

    Removed comment

commit 66f5eaee5ba3c3f1fd5d2583c027dee99d744d8d
Merge: 41655741 de8931a8
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Sep 28 11:57:15 2021 +0200

    Merge branch 'next' into bugfix/TV-676

commit 4165574175e7ddcf8e3e3e78abc1ab24c65575d3
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Mon Sep 27 12:13:12 2021 +0200

    WIP
This commit is contained in:
Erik Tiekstra
2021-09-28 13:15:09 +02:00
parent de8931a87d
commit 3d30ac185c
3 changed files with 53 additions and 42 deletions

View File

@@ -19,7 +19,7 @@
<h2>Personallista</h2> <h2>Personallista</h2>
<form class="employees__search-wrapper" (ngSubmit)="setSearchFilter()"> <form class="employees__search-wrapper" (ngSubmit)="setSearchString()">
<digi-form-input-search <digi-form-input-search
af-label="Sök på personalens namn" af-label="Sök på personalens namn"
(afOnInput)="setSearchValue($event)" (afOnInput)="setSearchValue($event)"

View File

@@ -26,8 +26,8 @@ export class EmployeesComponent {
return this._searchValue$.getValue(); return this._searchValue$.getValue();
} }
setSearchFilter(): void { setSearchString(): void {
this.employeeService.setSearchFilter(this.searchValue); this.employeeService.setSearchString(this.searchValue);
} }
setSearchValue($event: CustomEvent<{ target: { value: string } }>): void { setSearchValue($event: CustomEvent<{ target: { value: string } }>): void {

View File

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