1- import { ChangeDetectionStrategy , Component , Input , OnInit , ViewEncapsulation } from '@angular/core' ;
2- import { Observable , of } from 'rxjs' ;
1+ import { isPlatformBrowser } from '@angular/common' ;
2+ import {
3+ ChangeDetectionStrategy ,
4+ Component ,
5+ Inject ,
6+ Input ,
7+ OnInit ,
8+ PLATFORM_ID ,
9+ ViewEncapsulation
10+ } from '@angular/core' ;
11+
12+ import { BehaviorSubject , Observable , of } from 'rxjs' ;
13+
314import { Metric } from '../../../core/shared/metric.model' ;
4- import { hasValue } from '../../empty.util' ;
5- import { getFirstSucceededRemoteListPayload } from '../../../core/shared/operators' ;
15+ import { isEmpty } from '../../empty.util' ;
16+ import { getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
617import { map } from 'rxjs/operators' ;
718import { Item } from '../../../core/shared/item.model' ;
819import { LinkService } from '../../../core/cache/builders/link.service' ;
920import { followLink } from '../../utils/follow-link-config.model' ;
21+ import { RemoteData } from '../../../core/data/remote-data' ;
22+ import { PaginatedList } from '../../../core/data/paginated-list.model' ;
1023
1124export const allowedDonuts = [ 'altmetric' , 'dimensions' , 'plumX' ] ;
1225
@@ -21,27 +34,45 @@ export const allowedDonuts = ['altmetric', 'dimensions', 'plumX'];
2134 */
2235export class MetricDonutsComponent implements OnInit {
2336
37+ /**
38+ * The item object for which to show the metrics donuts
39+ */
2440 @Input ( ) item : Item ;
2541
26- constructor ( private linkService : LinkService ) {
42+ /**
43+ * The list of metric donuts to load
44+ */
45+ metrics$ : BehaviorSubject < Metric [ ] > = new BehaviorSubject < Metric [ ] > ( [ ] ) ;
46+
47+ constructor ( private linkService : LinkService , @Inject ( PLATFORM_ID ) protected platformId : Object ) {
2748 }
2849
2950 ngOnInit ( ) {
3051 this . linkService . resolveLink ( this . item , followLink ( 'metrics' ) ) ;
52+ if ( isPlatformBrowser ( this . platformId ) ) {
53+ this . retrieveMetrics ( ) . subscribe ( ( metrics : Metric [ ] ) => {
54+ this . metrics$ . next ( metrics ) ;
55+ } )
56+ }
3157 }
3258
3359 /**
34- * Filter metrics with a positive metricCount value .
60+ * Retrieve metrics from item object .
3561 */
36- donuts ( ) : Observable < Metric [ ] > {
37- if ( ! hasValue ( this . item . metrics ) ) {
62+ private retrieveMetrics ( ) : Observable < Metric [ ] > {
63+ if ( isEmpty ( this . item . metrics ) ) {
3864 return of ( [ ] ) ;
65+ } else {
66+ return this . item . metrics . pipe (
67+ getFirstCompletedRemoteData ( ) ,
68+ map ( ( metricsRD : RemoteData < PaginatedList < Metric > > ) => {
69+ if ( metricsRD . hasSucceeded ) {
70+ return metricsRD . payload . page . filter ( metric => allowedDonuts . includes ( metric . metricType ) ) ;
71+ } else {
72+ return [ ] ;
73+ }
74+ } ) ) ;
3975 }
40- return this . item . metrics . pipe (
41- getFirstSucceededRemoteListPayload ( ) ,
42- map ( ( metrics : Metric [ ] ) => {
43- return metrics . filter ( metric => allowedDonuts . includes ( metric . metricType ) ) ;
44- } ) ) ;
4576 }
4677
4778 identify ( index , item ) {
0 commit comments