From a6cd39c9067b1ee4dd7c004a28519becc83e5467 Mon Sep 17 00:00:00 2001 From: Davide Negretti Date: Fri, 23 Dec 2022 18:25:04 +0100 Subject: [PATCH] [CST-7757] Subscriptions porting (wip) --- .../subscription-modal.component.ts | 39 ++++++++--- .../subscriptions-page.component.html | 2 +- .../subscriptions-page.component.ts | 66 +++++++++++-------- src/assets/i18n/en.json5 | 32 +++++++++ 4 files changed, 101 insertions(+), 38 deletions(-) diff --git a/src/app/shared/subscriptions/components/subscription-modal/subscription-modal.component.ts b/src/app/shared/subscriptions/components/subscription-modal/subscription-modal.component.ts index 39cc79ff8d..b283e12d34 100644 --- a/src/app/shared/subscriptions/components/subscription-modal/subscription-modal.component.ts +++ b/src/app/shared/subscriptions/components/subscription-modal/subscription-modal.component.ts @@ -188,25 +188,44 @@ export class SubscriptionModalComponent implements OnInit { } } - if (currentSubscription && someCheckboxSelected) { - console.log('UPDATE'); - this.subscriptionService.updateSubscription(body, this.ePersonId, this.dso.uuid).pipe( - getFirstCompletedRemoteData(), - ).subscribe((res) => { + if (currentSubscription) { + const subscriptionsToBeRemobed = this.subscriptions?.filter( + (s) => s.subscriptionType === type && s.id !== currentSubscription.id + ); + for (let s of subscriptionsToBeRemobed) { + this.subscriptionService.deleteSubscription(currentSubscription.id).pipe( + getFirstCompletedRemoteData(), + ).subscribe((res) => { if (res.hasSucceeded) { - this.notifySuccess(); - this.activeModal.close(); + console.warn(`An additional subscription with type=${type} and id=${s.id} has been removed`); } else { this.notifyFailure(); } + }); + } + } + + + if (currentSubscription && someCheckboxSelected) { + // Update the existing subscription + this.subscriptionService.updateSubscription(body, this.ePersonId, this.dso.uuid).pipe( + getFirstCompletedRemoteData(), + ).subscribe((res) => { + if (res.hasSucceeded) { + this.notifySuccess(); + this.activeModal.close(); + } else { + this.notifyFailure(); } - ); + }); } else if (currentSubscription && !someCheckboxSelected) { - console.log('DELETE'); + // Delete the existing subscription this.subscriptionService.deleteSubscription(currentSubscription.id).subscribe(console.log); + // TODO handle notifications } else if (someCheckboxSelected) { - console.log('CREATE'); + // Create a new subscription this.subscriptionService.createSubscription(body, this.ePersonId, this.dso.uuid).subscribe(console.log); + // TODO handle notifications } diff --git a/src/app/subscriptions-page/subscriptions-page.component.html b/src/app/subscriptions-page/subscriptions-page.component.html index bfbdbe72fc..9d1e6d504e 100644 --- a/src/app/subscriptions-page/subscriptions-page.component.html +++ b/src/app/subscriptions-page/subscriptions-page.component.html @@ -10,7 +10,7 @@ diff --git a/src/app/subscriptions-page/subscriptions-page.component.ts b/src/app/subscriptions-page/subscriptions-page.component.ts index b81b7c5617..2951cf037c 100644 --- a/src/app/subscriptions-page/subscriptions-page.component.ts +++ b/src/app/subscriptions-page/subscriptions-page.component.ts @@ -1,6 +1,6 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; -import { BehaviorSubject, Subscription as rxSubscription } from 'rxjs'; -import { switchMap, take } from 'rxjs/operators'; +import { BehaviorSubject, combineLatestWith, Observable, of, shareReplay, Subscription as rxSubscription } from 'rxjs'; +import { combineLatest, map, switchMap, take, tap } from 'rxjs/operators'; import { Subscription } from '../shared/subscriptions/models/subscription.model'; import { buildPaginatedList, PaginatedList } from '../core/data/paginated-list.model'; import { SubscriptionService } from '../shared/subscriptions/subscription.service'; @@ -9,6 +9,7 @@ import { PaginationService } from '../core/pagination/pagination.service'; import { PageInfo } from '../core/shared/page-info.model'; import { AuthService } from '../core/auth/auth.service'; import { EPerson } from '../core/eperson/models/eperson.model'; +import { getFirstSucceededRemoteDataPayload } from '../core/shared/operators'; @Component({ selector: 'ds-subscriptions-page', @@ -42,10 +43,12 @@ export class SubscriptionsPageComponent implements OnInit, OnDestroy { */ loading$: BehaviorSubject = new BehaviorSubject(false); + ePersonId$: Observable; + /** * EPerson id of the logged in user */ - eperson: string; + // ePersonId: string; constructor( private paginationService: PaginationService, @@ -58,27 +61,32 @@ export class SubscriptionsPageComponent implements OnInit, OnDestroy { * When page is changed it will request the new subscriptions for the new page config */ ngOnInit(): void { - this.authService.getAuthenticatedUserFromStore().pipe(take(1)).subscribe( (eperson: EPerson) => { - this.eperson = eperson.id; - - this.sub = this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( - switchMap((findListOptions) => { - this.loading$.next(true); - return this.subscriptionService.findByEPerson(this.eperson,{ - currentPage: findListOptions.currentPage, - elementsPerPage: findListOptions.pageSize - }); - } - ) - ).subscribe({ - next: (res: any) => { - this.subscriptions$.next(res); - this.loading$.next(false); - }, - error: () => { - this.loading$.next(false); - } - }); + this.ePersonId$ = this.authService.getAuthenticatedUserFromStore().pipe( + take(1), + map((ePerson: EPerson) => ePerson.id), + shareReplay(), + /*tap((ePersonId: string) => { // TODO unused + this.ePersonId = ePersonId; + }),*/ + ); + const currentPagination$ = this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( + tap(console.log), + combineLatestWith(this.ePersonId$), + tap(() => {this.loading$.next(true);}), + switchMap(([currentPagination, ePersonId]) => this.subscriptionService.findByEPerson(ePersonId,{ + currentPage: currentPagination.currentPage, + elementsPerPage: currentPagination.pageSize + })), + tap((x) => console.log('find', x)), + // getFirstSucceededRemoteDataPayload(), + ).subscribe({ + next: (res: any) => { + this.subscriptions$.next(res); + this.loading$.next(false); + }, + error: () => { + this.loading$.next(false); + } }); } @@ -86,11 +94,11 @@ export class SubscriptionsPageComponent implements OnInit, OnDestroy { * When an action is made and the information is changed refresh the information */ refresh(): void { - this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( + /*this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( take(1), switchMap((findListOptions) => { this.loading$.next(true); - return this.subscriptionService.findByEPerson(this.eperson,{ + return this.subscriptionService.findByEPerson(this.ePersonId,{ currentPage: findListOptions.currentPage, elementsPerPage: findListOptions.pageSize }); @@ -104,7 +112,7 @@ export class SubscriptionsPageComponent implements OnInit, OnDestroy { error: () => { this.loading$.next(false); } - }); + });*/ } /** @@ -114,4 +122,8 @@ export class SubscriptionsPageComponent implements OnInit, OnDestroy { this.sub.unsubscribe(); } + obs(v) { + return of(v); + } + } diff --git a/src/assets/i18n/en.json5 b/src/assets/i18n/en.json5 index 12a21beaa2..2af33eb11d 100644 --- a/src/assets/i18n/en.json5 +++ b/src/assets/i18n/en.json5 @@ -2915,6 +2915,8 @@ "nav.stop-impersonating": "Stop impersonating EPerson", + "nav.subscriptions" : "Subscriptions", + "nav.toggle" : "Toggle navigation", "nav.user.description" : "User profile bar", @@ -4492,6 +4494,36 @@ "submission.workspace.generic.view-help": "Select this option to view the item's metadata.", + "subscriptions.title": "Subscriptions", + + "subscriptions.item": "Subscriptions for items", + + "subscriptions.collection": "Subscriptions for collections", + + "subscriptions.community": "Subscriptions for communities", + + "subscriptions.subscription_type": "Subscription type", + + "subscriptions.frequency": "Subscription frequency", + + "subscriptions.frequency.D": "Daily", + + "subscriptions.frequency.M": "Monthly", + + "subscriptions.frequency.W": "Weekly", + + "subscriptions.table.dso": "Subject", + + "subscriptions.table.subscription_type": "Subscription Type", + + "subscriptions.table.subscription_frequency": "Subscription Frequency", + + "subscriptions.table.action": "Action", + + "subscriptions.table.empty.message": "You have not subscribed any notification yet. To subscribe notification about an object please use the contextual menu in the object detail view", + + + "thumbnail.default.alt": "Thumbnail Image", "thumbnail.default.placeholder": "No Thumbnail Available",