Commits
Tim Donohue authored and GitHub committed 23586810b33 Merge
1 + | import { AbstractControl, ValidationErrors } from '@angular/forms'; |
2 + | import { Observable } from 'rxjs'; |
3 + | import { map} from 'rxjs/operators'; |
4 + | |
5 + | import { GroupDataService } from '../../../../core/eperson/group-data.service'; |
6 + | import { getFirstSucceededRemoteListPayload } from '../../../../core/shared/operators'; |
7 + | import { Group } from '../../../../core/eperson/models/group.model'; |
8 + | |
9 + | export class ValidateGroupExists { |
10 + | |
11 + | /** |
12 + | * This method will create the validator with the groupDataService requested from component |
13 + | * @param groupDataService the service with DI in the component that this validator is being utilized. |
14 + | * @return Observable<ValidationErrors | null> |
15 + | */ |
16 + | static createValidator(groupDataService: GroupDataService) { |
17 + | return (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => { |
18 + | return groupDataService.searchGroups(control.value, { |
19 + | currentPage: 1, |
20 + | elementsPerPage: 100 |
21 + | }) |
22 + | .pipe( |
23 + | getFirstSucceededRemoteListPayload(), |
24 + | map( (groups: Group[]) => { |
25 + | return groups.filter(group => group.name === control.value); |
26 + | }), |
27 + | map( (groups: Group[]) => { |
28 + | return groups.length > 0 ? { groupExists: true } : null; |
29 + | }), |
30 + | ); |
31 + | }; |
32 + | } |
33 + | } |