|
| 1 | +import { |
| 2 | + AsyncPipe, |
| 3 | + NgClass, |
| 4 | +} from '@angular/common'; |
| 5 | +import { Component } from '@angular/core'; |
| 6 | +import { |
| 7 | + FormsModule, |
| 8 | + ReactiveFormsModule, |
| 9 | +} from '@angular/forms'; |
| 10 | +import { |
| 11 | + TranslateModule, |
| 12 | + TranslateService, |
| 13 | +} from '@ngx-translate/core'; |
| 14 | +import { InfiniteScrollModule } from 'ngx-infinite-scroll'; |
| 15 | +import { Observable } from 'rxjs'; |
| 16 | +import { map } from 'rxjs/operators'; |
| 17 | + |
| 18 | +import { DSONameService } from '../../../../core/breadcrumbs/dso-name.service'; |
| 19 | +import { CommunityDataService } from '../../../../core/data/community-data.service'; |
| 20 | +import { FindListOptions } from '../../../../core/data/find-list-options.model'; |
| 21 | +import { |
| 22 | + buildPaginatedList, |
| 23 | + PaginatedList, |
| 24 | +} from '../../../../core/data/paginated-list.model'; |
| 25 | +import { RemoteData } from '../../../../core/data/remote-data'; |
| 26 | +import { Community } from '../../../../core/shared/community.model'; |
| 27 | +import { DSpaceObject } from '../../../../core/shared/dspace-object.model'; |
| 28 | +import { getFirstCompletedRemoteData } from '../../../../core/shared/operators'; |
| 29 | +import { SearchService } from '../../../../core/shared/search/search.service'; |
| 30 | +import { hasValue } from '../../../empty.util'; |
| 31 | +import { HoverClassDirective } from '../../../hover-class.directive'; |
| 32 | +import { ThemedLoadingComponent } from '../../../loading/themed-loading.component'; |
| 33 | +import { NotificationsService } from '../../../notifications/notifications.service'; |
| 34 | +import { CommunitySearchResult } from '../../../object-collection/shared/community-search-result.model'; |
| 35 | +import { ListableObjectComponentLoaderComponent } from '../../../object-collection/shared/listable-object/listable-object-component-loader.component'; |
| 36 | +import { SearchResult } from '../../../search/models/search-result.model'; |
| 37 | +import { followLink } from '../../../utils/follow-link-config.model'; |
| 38 | +import { DSOSelectorComponent } from '../dso-selector.component'; |
| 39 | + |
| 40 | +@Component({ |
| 41 | + selector: 'ds-authorized-community-selector', |
| 42 | + styleUrls: ['../dso-selector.component.scss'], |
| 43 | + templateUrl: '../dso-selector.component.html', |
| 44 | + standalone: true, |
| 45 | + imports: [ |
| 46 | + AsyncPipe, |
| 47 | + FormsModule, |
| 48 | + HoverClassDirective, |
| 49 | + InfiniteScrollModule, |
| 50 | + ListableObjectComponentLoaderComponent, |
| 51 | + NgClass, |
| 52 | + ReactiveFormsModule, |
| 53 | + ThemedLoadingComponent, |
| 54 | + TranslateModule, |
| 55 | + ], |
| 56 | +}) |
| 57 | +/** |
| 58 | + * Component rendering a list of communities to select from |
| 59 | + */ |
| 60 | +export class AuthorizedCommunitySelectorComponent extends DSOSelectorComponent { |
| 61 | + /** |
| 62 | + * If present this value is used to filter community list by entity type |
| 63 | + */ |
| 64 | + |
| 65 | + constructor( |
| 66 | + protected searchService: SearchService, |
| 67 | + protected communityDataService: CommunityDataService, |
| 68 | + protected notifcationsService: NotificationsService, |
| 69 | + protected translate: TranslateService, |
| 70 | + protected dsoNameService: DSONameService, |
| 71 | + ) { |
| 72 | + super(searchService, notifcationsService, translate, dsoNameService); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get a query to send for retrieving the current DSO |
| 77 | + */ |
| 78 | + getCurrentDSOQuery(): string { |
| 79 | + return this.currentDSOId; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Perform a search for authorized communities with the current query and page |
| 84 | + * @param query Query to search objects for |
| 85 | + * @param page Page to retrieve |
| 86 | + * @param useCache Whether or not to use the cache |
| 87 | + */ |
| 88 | + search(query: string, page: number, useCache: boolean = true): Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>> { |
| 89 | + let searchListService$: Observable<RemoteData<PaginatedList<Community>>> = null; |
| 90 | + const findOptions: FindListOptions = { |
| 91 | + currentPage: page, |
| 92 | + elementsPerPage: this.defaultPagination.pageSize, |
| 93 | + }; |
| 94 | + |
| 95 | + searchListService$ = this.communityDataService |
| 96 | + .getAuthorizedCommunity(query, findOptions, useCache, false, followLink('parentCommunity')); |
| 97 | + |
| 98 | + return searchListService$.pipe( |
| 99 | + getFirstCompletedRemoteData(), |
| 100 | + map((rd) => Object.assign(new RemoteData(null, null, null, null), rd, { |
| 101 | + payload: hasValue(rd.payload) ? buildPaginatedList(rd.payload.pageInfo, rd.payload.page.map((col) => Object.assign(new CommunitySearchResult(), { indexableObject: col }))) : null, |
| 102 | + })), |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments