77 */
88import { Injectable } from '@angular/core' ;
99import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
10+ import { TranslateService } from '@ngx-translate/core' ;
1011import {
1112 combineLatest ,
1213 Observable ,
14+ Subject ,
1315} from 'rxjs' ;
14- import { map } from 'rxjs/operators' ;
16+ import {
17+ map ,
18+ startWith ,
19+ switchMap ,
20+ } from 'rxjs/operators' ;
21+ import { AuthService } from 'src/app/core/auth/auth.service' ;
1522
1623import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service' ;
1724import { FeatureID } from '../../../core/data/feature-authorization/feature-id' ;
1825import { DSpaceObject } from '../../../core/shared/dspace-object.model' ;
1926import { SubscriptionModalComponent } from '../../subscriptions/subscription-modal/subscription-modal.component' ;
27+ import { SubscriptionsDataService } from '../../subscriptions/subscriptions-data.service' ;
2028import { OnClickMenuItemModel } from '../menu-item/models/onclick.model' ;
2129import { MenuItemType } from '../menu-item-type.model' ;
2230import { PartialMenuSection } from '../menu-provider.model' ;
@@ -27,33 +35,64 @@ import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu';
2735 */
2836@Injectable ( )
2937export class SubscribeMenuProvider extends DSpaceObjectPageMenuProvider {
38+
39+ private refresh$ = new Subject < void > ( ) ;
40+
3041 constructor (
42+ protected authService : AuthService ,
3143 protected authorizationService : AuthorizationDataService ,
44+ protected subscriptionService : SubscriptionsDataService ,
3245 protected modalService : NgbModal ,
46+ protected translateService : TranslateService ,
3347 ) {
3448 super ( ) ;
3549 }
3650
3751 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 ;
52+ return this . refresh$ . pipe (
53+ startWith ( undefined ) ,
54+ switchMap ( ( ) =>
55+ combineLatest ( [
56+ this . authorizationService . isAuthorized ( FeatureID . CanSubscribe , dso . self ) ,
57+ this . authService . getAuthenticatedUserFromStore ( ) ,
58+ ] ) ,
59+ ) ,
60+ switchMap ( ( [ canSubscribe , user ] ) =>
61+ this . subscriptionService . getSubscriptionsByPersonDSO ( user . id , dso . uuid ) . pipe (
62+ map ( ( subscriptionRD ) => {
63+ const subscription = subscriptionRD . payload ?. page [ 0 ] ;
64+ const isSubscribed = subscription != null ;
65+ const key = isSubscribed
66+ ? 'subscriptions.unsubscribe'
67+ : 'subscriptions.tooltip' ;
68+ return [
69+ {
70+ visible : canSubscribe ,
71+ model : {
72+ type : MenuItemType . ONCLICK ,
73+ text : key ,
74+ function : ( ) => {
75+ if ( isSubscribed ) {
76+ this . subscriptionService . deleteSubscription ( subscription . id ) . subscribe ( ( ) => {
77+ this . refresh$ . next ( ) ;
78+ } ) ;
79+ } else {
80+ const modalRef = this . modalService . open ( SubscriptionModalComponent ) ;
81+ modalRef . componentInstance . dso = dso ;
82+ modalRef . componentInstance . updated . subscribe ( ( ) => {
83+ this . refresh$ . next ( ) ;
84+ } ) ;
85+ modalRef . closed . subscribe ( ( ) => {
86+ } ) ;
87+ }
88+ } ,
89+ } as OnClickMenuItemModel ,
90+ icon : 'bell' ,
5191 } ,
52- } as OnClickMenuItemModel ,
53- icon : 'bell' ,
54- } ,
55- ] as PartialMenuSection [ ] ;
56- } ) ,
92+ ] as PartialMenuSection [ ] ;
93+ } ) ,
94+ ) ,
95+ ) ,
5796 ) ;
5897 }
5998}
0 commit comments