@@ -10,13 +10,23 @@ import { AuthorizationDataService } from '@dspace/core/data/feature-authorizatio
1010import { FeatureID } from '@dspace/core/data/feature-authorization/feature-id' ;
1111import { DSpaceObject } from '@dspace/core/shared/dspace-object.model' ;
1212import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
13+ import { TranslateService } from '@ngx-translate/core' ;
1314import {
1415 combineLatest ,
1516 Observable ,
17+ of ,
18+ Subject ,
1619} from 'rxjs' ;
17- import { map } from 'rxjs/operators' ;
20+ import {
21+ first ,
22+ map ,
23+ startWith ,
24+ switchMap ,
25+ } from 'rxjs/operators' ;
26+ import { AuthService } from 'src/app/core/auth/auth.service' ;
1827
1928import { SubscriptionModalComponent } from '../../subscriptions/subscription-modal/subscription-modal.component' ;
29+ import { SubscriptionsDataService } from '../../subscriptions/subscriptions-data.service' ;
2030import { OnClickMenuItemModel } from '../menu-item/models/onclick.model' ;
2131import { MenuItemType } from '../menu-item-type.model' ;
2232import { PartialMenuSection } from '../menu-provider.model' ;
@@ -27,33 +37,59 @@ import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu';
2737 */
2838@Injectable ( )
2939export class SubscribeMenuProvider extends DSpaceObjectPageMenuProvider {
40+ private refresh$ = new Subject < void > ( ) ;
41+
3042 constructor (
43+ protected authService : AuthService ,
3144 protected authorizationService : AuthorizationDataService ,
45+ protected subscriptionService : SubscriptionsDataService ,
3246 protected modalService : NgbModal ,
47+ protected translateService : TranslateService ,
3348 ) {
3449 super ( ) ;
3550 }
3651
3752 public getSectionsForContext ( dso : DSpaceObject ) : Observable < PartialMenuSection [ ] > {
38- return combineLatest ( [
39- this . authorizationService . isAuthorized ( FeatureID . CanSubscribe , dso . self ) ,
40- ] ) . pipe (
41- map ( ( [ canSubscribe ] ) => {
42- return [
43- {
44- visible : canSubscribe ,
45- model : {
46- type : MenuItemType . ONCLICK ,
47- text : 'subscriptions.tooltip' ,
48- function : ( ) => {
49- const modalRef = this . modalService . open ( SubscriptionModalComponent ) ;
50- modalRef . componentInstance . dso = dso ;
51- } ,
52- } as OnClickMenuItemModel ,
53- icon : 'bell' ,
54- } ,
55- ] as PartialMenuSection [ ] ;
53+ const realSections$ = this . refresh$ . pipe (
54+ startWith ( undefined ) ,
55+ switchMap ( ( ) =>
56+ combineLatest ( [
57+ this . authorizationService . isAuthorized ( FeatureID . CanSubscribe , dso . self ) ,
58+ this . authService . getAuthenticatedUserFromStore ( ) . pipe ( first ( ) ) ,
59+ ] ) ,
60+ ) ,
61+ switchMap ( ( [ canSubscribe , user ] ) => {
62+ if ( ! canSubscribe || ! user ) {
63+ return of ( [ ] ) ;
64+ }
65+ const openModal = ( ) => {
66+ const modalRef = this . modalService . open ( SubscriptionModalComponent ) ;
67+ modalRef . componentInstance . dso = dso ;
68+ modalRef . componentInstance . updated . subscribe ( ( ) => this . refresh$ . next ( ) ) ;
69+ } ;
70+ return this . subscriptionService . getSubscriptionsByPersonDSO ( user . id , dso . uuid ) . pipe (
71+ map ( ( rd ) => {
72+ const subscription = rd . payload ?. page ?. [ 0 ] ;
73+ const key = subscription
74+ ? 'subscriptions.manage'
75+ : 'subscriptions.tooltip' ;
76+ return [
77+ {
78+ visible : true ,
79+ model : {
80+ type : MenuItemType . ONCLICK ,
81+ text : key ,
82+ function : openModal ,
83+ } as OnClickMenuItemModel ,
84+ icon : 'bell' ,
85+ } as PartialMenuSection ,
86+ ] ;
87+ } ) ,
88+ ) ;
5689 } ) ,
5790 ) ;
91+ return realSections$ . pipe (
92+ startWith ( [ ] as PartialMenuSection [ ] ) ,
93+ ) ;
5894 }
5995}
0 commit comments