-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
183 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
libs/openchallenges/challenge-search/src/lib/challenge-search-data.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject, forkJoin, iif, Observable, of, Subject } from 'rxjs'; | ||
import { | ||
catchError, | ||
debounceTime, | ||
distinctUntilChanged, | ||
map, | ||
switchMap, | ||
takeUntil, | ||
} from 'rxjs/operators'; | ||
import { | ||
ChallengePlatformSearchQuery, | ||
ChallengePlatformService, | ||
ChallengePlatformSort, | ||
Image, | ||
ImageAspectRatio, | ||
ImageHeight, | ||
ImageQuery, | ||
ImageService, | ||
Organization, | ||
OrganizationSearchQuery, | ||
OrganizationService, | ||
OrganizationSort, | ||
} from '@sagebionetworks/openchallenges/api-client-angular'; | ||
import { forkJoinConcurrent } from '@sagebionetworks/openchallenges/util'; | ||
import { Filter } from '@sagebionetworks/openchallenges/ui'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ChallengeSearchDataService { | ||
private platformSearchTerms: BehaviorSubject<string> = | ||
new BehaviorSubject<string>(''); | ||
|
||
private organizationSearchTerms: BehaviorSubject<string> = | ||
new BehaviorSubject<string>(''); | ||
|
||
private destroy = new Subject<void>(); | ||
|
||
constructor( | ||
private challengePlatformService: ChallengePlatformService, | ||
private organizationService: OrganizationService, | ||
private imageService: ImageService | ||
) {} | ||
|
||
setPlatformSearchTerms(searchTerms: string) { | ||
this.platformSearchTerms.next(searchTerms); | ||
} | ||
|
||
setOriganizationSearchTerms(searchTerms: string) { | ||
this.organizationSearchTerms.next(searchTerms); | ||
} | ||
|
||
searchPlatforms(): Observable<Filter[]> { | ||
return this.platformSearchTerms.pipe( | ||
debounceTime(400), | ||
distinctUntilChanged(), | ||
takeUntil(this.destroy), | ||
switchMap((searchTerm: string) => { | ||
const sortedBy: ChallengePlatformSort = 'name'; | ||
const platformQuery: ChallengePlatformSearchQuery = { | ||
searchTerms: searchTerm, | ||
sort: sortedBy, | ||
}; | ||
return this.challengePlatformService.listChallengePlatforms( | ||
platformQuery | ||
); | ||
}), | ||
map((page) => | ||
page.challengePlatforms.map((platform) => ({ | ||
value: platform.slug, | ||
label: platform.name, | ||
active: false, | ||
})) | ||
) | ||
); | ||
} | ||
|
||
searchOriganizations(): Observable<Filter[]> { | ||
return this.organizationSearchTerms.pipe( | ||
debounceTime(400), | ||
distinctUntilChanged(), | ||
// takeUntil(this.destroy), | ||
switchMap((searchTerm: string) => { | ||
const sortBy: OrganizationSort = 'name'; | ||
const orgQuery: OrganizationSearchQuery = { | ||
searchTerms: searchTerm, | ||
sort: sortBy, | ||
}; | ||
return this.organizationService.listOrganizations(orgQuery); | ||
}), | ||
map((page) => page.organizations), | ||
switchMap((orgs) => | ||
forkJoin({ | ||
orgs: of(orgs), | ||
avatarUrls: forkJoinConcurrent( | ||
orgs.map((org) => this.getOrganizationAvatarUrl(org)), | ||
Infinity | ||
), | ||
}) | ||
), | ||
map(({ orgs, avatarUrls }) => | ||
orgs.map((org, index) => ({ | ||
value: org.id, | ||
label: org.name, | ||
avatarUrl: avatarUrls[index]?.url, | ||
active: false, | ||
})) | ||
) | ||
); | ||
} | ||
|
||
private getOrganizationAvatarUrl(org: Organization): Observable<Image> { | ||
return iif( | ||
() => !!org.avatarKey, | ||
this.imageService.getImage({ | ||
objectKey: org.avatarKey, | ||
height: ImageHeight._32px, | ||
aspectRatio: ImageAspectRatio._11, | ||
} as ImageQuery), | ||
of({ url: '' }) | ||
).pipe( | ||
catchError(() => { | ||
console.error( | ||
'Unable to get the image url. Please check the logs of the image service.' | ||
); | ||
return of({ url: '' }); | ||
}) | ||
); | ||
} | ||
} |
Oops, something went wrong.