Skip to content

Commit

Permalink
288: add isActive filtering for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Spahr committed Jul 27, 2021
1 parent fd99109 commit d819ad3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 11 deletions.
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<erz-typeahead
[typeaheadService]="studyClassService"
[(value)]="filter.studyClass"
[additionalHttpParams]="this.classesHttpFilter"
></erz-typeahead>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export class EditAbsencesHeaderComponent implements OnInit {
private translate: TranslateService
) {}

classesHttpFilter = {
params: {
fields: 'IsActive',
['filter.IsActive']: '=true',
},
};

ngOnInit(): void {}

show(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<erz-typeahead
[typeaheadService]="studyClassesService"
[(value)]="filter.studyClass"
[additionalHttpParams]="this.classesHttpFilter"
></erz-typeahead>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export class EvaluateAbsencesHeaderComponent implements OnInit {
public studyClassesService: StudyClassesRestService
) {}

classesHttpFilter = {
params: {
fields: 'IsActive',
['filter.IsActive']: '=true',
},
};

ngOnInit(): void {}

show(): void {
Expand Down
9 changes: 7 additions & 2 deletions src/app/shared/components/typeahead/typeahead.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import {
import { uniqueId } from 'lodash-es';

import { longerOrEqual } from '../../utils/filter';
import { TypeaheadService } from '../../services/typeahead-rest.service';
import {
AdditionalHttpParams,
TypeaheadService,
} from '../../services/typeahead-rest.service';
import { DropDownItem } from '../../models/drop-down-item.model';
import { HttpParams, HttpParamsOptions } from '@angular/common/http';

const FETCH_DEBOUNCE_TIME = 300;
const MINIMAL_TERM_LENGTH = 3;
Expand All @@ -37,6 +41,7 @@ export class TypeaheadComponent implements OnInit, OnChanges {
@Input() typeaheadService: TypeaheadService;
@Input() placeholder = 'shared.typeahead.default-placeholder';
@Input() value: Option<number>;
@Input() additionalHttpParams: AdditionalHttpParams;

@Output()
valueChange = this.selectedItem$.pipe(
Expand Down Expand Up @@ -87,7 +92,7 @@ export class TypeaheadComponent implements OnInit, OnChanges {
private fetchItems(term: string): Observable<ReadonlyArray<DropDownItem>> {
this.loading$.next(true);
return this.typeaheadService
.getTypeaheadItems(term)
.getTypeaheadItems(term, this.additionalHttpParams)
.pipe(finalize(() => this.loading$.next(false)));
}

Expand Down
49 changes: 41 additions & 8 deletions src/app/shared/services/typeahead-rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import { decodeArray, decode } from '../utils/decode';
import { RestService } from '../services/rest.service';
import { DropDownItem } from '../models/drop-down-item.model';

export class AdditionalHttpParams {
params: { [param: string]: string };
}

export interface TypeaheadService {
getTypeaheadItems(term: string): Observable<ReadonlyArray<DropDownItem>>;
getTypeaheadItems(
term: string,
additionalParams?: { params: { [param: string]: string } }
): Observable<ReadonlyArray<DropDownItem>>;
getTypeaheadItemById(id: number): Observable<DropDownItem>;
}

Expand All @@ -32,14 +39,24 @@ export abstract class TypeaheadRestService<T extends t.InterfaceType<any>>
super(http, settings, codec, resourcePath);
}

getTypeaheadItems(term: string): Observable<ReadonlyArray<DropDownItem>> {
getTypeaheadItems(
term: string,
additionalParams?: { params: { [param: string]: string } }
): Observable<ReadonlyArray<DropDownItem>> {
const params = {
params: {
fields: [this.idAttr, this.labelAttr].join(','),
[`filter.${this.labelAttr}`]: `~*${term}*`,
},
};

return this.http
.get<unknown>(`${this.baseUrl}/`, {
params: {
fields: [this.idAttr, this.labelAttr].join(','),
[`filter.${this.labelAttr}`]: `~*${term}*`,
},
})
.get<unknown>(
`${this.baseUrl}/`,
additionalParams
? this.mergeHttpParams(params, additionalParams)
: params
)
.pipe(
switchMap(decodeArray(this.typeaheadCodec)),
map((items) =>
Expand All @@ -60,4 +77,20 @@ export abstract class TypeaheadRestService<T extends t.InterfaceType<any>>
map((item) => ({ Key: item[this.idAttr], Value: item[this.labelAttr] }))
);
}

private mergeHttpParams(
typeaheadParams: AdditionalHttpParams,
additionalParams: AdditionalHttpParams
): AdditionalHttpParams {
const merged = {
params: { ...typeaheadParams.params, ...additionalParams.params },
};
if (additionalParams.params.fields) {
merged.params.fields = typeaheadParams.params.fields.concat(
',',
additionalParams.params.fields
);
}
return merged;
}
}

0 comments on commit d819ad3

Please sign in to comment.