Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORCID Registry Queue table: Fix behavior on item removal from the table #3463

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
<div class="container">
<h2>{{ 'person.orcid.registry.queue' | translate }}</h2>

<ds-alert *ngIf="(processing$ | async) !== true && (getList() | async)?.payload?.totalElements === 0"
<ds-alert *ngIf="(processing$ | async) !== true && listDataRD?.payload?.totalElements === 0"
[type]="AlertTypeEnum.Info">
{{ 'person.page.orcid.sync-queue.empty-message' | translate}}
</ds-alert>
<ds-pagination *ngIf="(processing$ | async) !== true && (getList() | async)?.payload?.totalElements > 0"
<ds-pagination *ngIf="listDataRD?.payload?.totalElements > 0"
[paginationOptions]="paginationOptions"
[collectionSize]="(getList() | async)?.payload?.totalElements"
[retainScrollPosition]="false" [hideGear]="true" (paginationChange)="updateList()">
[collectionSize]="listDataRD?.payload?.totalElements"
[retainScrollPosition]="true"
[hideGear]="true"
(paginationChange)="onPaginationChange()">

<div class="table-responsive">
<table id="groups" class="table table-sm table-striped table-hover table-bordered">
Expand All @@ -22,7 +24,7 @@ <h2>{{ 'person.orcid.registry.queue' | translate }}</h2>
</tr>
</thead>
<tbody>
<tr *ngFor="let entry of (getList() | async)?.payload?.page" data-test="orcidQueueElementRow">
<tr *ngFor="let entry of listDataRD?.payload?.page" data-test="orcidQueueElementRow">
<td style="width: 15%" class="text-center align-middle">
<i [ngClass]="getIconClass(entry)" [ngbTooltip]="getIconTooltip(entry) | translate"
class="fa-2x" aria-hidden="true"></i>
Expand Down Expand Up @@ -51,3 +53,4 @@ <h2>{{ 'person.orcid.registry.queue' | translate }}</h2>
</ds-pagination>
</div>
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import {
TranslateLoader,
Expand All @@ -21,10 +22,14 @@ import { OrcidHistoryDataService } from '../../../core/orcid/orcid-history-data.
import { OrcidQueueDataService } from '../../../core/orcid/orcid-queue-data.service';
import { PaginationService } from '../../../core/pagination/pagination.service';
import { Item } from '../../../core/shared/item.model';
import { RouterMock } from '../../../shared/mocks/router.mock';
import { TranslateLoaderMock } from '../../../shared/mocks/translate-loader.mock';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { PaginationComponent } from '../../../shared/pagination/pagination.component';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import {
createNoContentRemoteDataObject$,
createSuccessfulRemoteDataObject$,
} from '../../../shared/remote-data.utils';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub';
import { createPaginatedList } from '../../../shared/testing/utils.test';
Expand Down Expand Up @@ -111,7 +116,7 @@ describe('OrcidQueueComponent test suite', () => {

const orcidQueueElements = [orcidQueueElement(1), orcidQueueElement(2)];

const orcidQueueServiceSpy = jasmine.createSpyObj('orcidQueueService', ['searchByProfileItemId', 'clearFindByProfileItemRequests']);
const orcidQueueServiceSpy = jasmine.createSpyObj('orcidQueueService', ['searchByProfileItemId', 'clearFindByProfileItemRequests', 'deleteById']);
orcidQueueServiceSpy.searchByProfileItemId.and.returnValue(createSuccessfulRemoteDataObject$<PaginatedList<OrcidQueue>>(createPaginatedList<OrcidQueue>(orcidQueueElements)));

beforeEach(waitForAsync(() => {
Expand All @@ -136,6 +141,7 @@ describe('OrcidQueueComponent test suite', () => {
{ provide: OrcidHistoryDataService, useValue: {} },
{ provide: PaginationService, useValue: new PaginationServiceStub() },
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
{ provide: Router, useValue: new RouterMock() },
],
schemas: [NO_ERRORS_SCHEMA],
})
Expand Down Expand Up @@ -166,4 +172,18 @@ describe('OrcidQueueComponent test suite', () => {
expect(table.length).toBe(2);
});

it('should handle pagination change', () => {
spyOn(component, 'updateList');
component.onPaginationChange();
expect(component.updateList).toHaveBeenCalled();
});

it('should discard an entry', waitForAsync(() => {
spyOn(component, 'removeEntryFromList');
orcidQueueServiceSpy.deleteById.and.returnValue(createNoContentRemoteDataObject$());
component.discardEntry(orcidQueueElements[0]);
fixture.whenStable().then(() => {
expect(component.removeEntryFromList).toHaveBeenCalledWith(orcidQueueElements[0].id);
});
}));
});
71 changes: 56 additions & 15 deletions src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { CommonModule } from '@angular/common';
import {
CommonModule,
DOCUMENT,
} from '@angular/common';
import {
Component,
Inject,
Input,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
} from '@angular/core';
import { Router } from '@angular/router';
import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
import {
TranslateModule,
Expand Down Expand Up @@ -40,6 +45,7 @@
import { hasValue } from '../../../shared/empty.util';
import { ThemedLoadingComponent } from '../../../shared/loading/themed-loading.component';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { ObjectTableComponent } from '../../../shared/object-table/object-table.component';
import { PaginationComponent } from '../../../shared/pagination/pagination.component';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';

Expand All @@ -54,6 +60,7 @@
ThemedLoadingComponent,
AlertComponent,
PaginationComponent,
ObjectTableComponent,
],
standalone: true,
})
Expand All @@ -70,6 +77,7 @@
public paginationOptions: PaginationComponentOptions = Object.assign(new PaginationComponentOptions(), {
id: 'oqp',
pageSize: 5,
currentPage: 1,
});

/**
Expand All @@ -80,7 +88,7 @@
/**
* A list of orcid queue records
*/
private list$: BehaviorSubject<RemoteData<PaginatedList<OrcidQueue>>> = new BehaviorSubject<RemoteData<PaginatedList<OrcidQueue>>>({} as any);
listDataRD: RemoteData<PaginatedList<OrcidQueue>>;

/**
* The AlertType enumeration
Expand All @@ -100,6 +108,8 @@
private paginationService: PaginationService,
private notificationsService: NotificationsService,
private orcidHistoryService: OrcidHistoryDataService,
private router: Router,
@Inject(DOCUMENT) private _document: Document,
) {
}

Expand All @@ -119,24 +129,36 @@
updateList() {
this.subs.push(
this.paginationService.getCurrentPagination(this.paginationOptions.id, this.paginationOptions).pipe(
debounceTime(100),
debounceTime(300),
distinctUntilChanged(),
tap(() => this.processing$.next(true)),
switchMap((config: PaginationComponentOptions) => this.orcidQueueService.searchByProfileItemId(this.item.id, config, false)),
switchMap((currentPaginationOptions) => this.orcidQueueService.searchByProfileItemId(this.item.id, currentPaginationOptions, false)),
getFirstCompletedRemoteData(),
).subscribe((result: RemoteData<PaginatedList<OrcidQueue>>) => {
this.processing$.next(false);
this.list$.next(result);
this.orcidQueueService.clearFindByProfileItemRequests();
).subscribe({
next: (result: RemoteData<PaginatedList<OrcidQueue>>) => {
this.paginationOptions = Object.assign(this.paginationOptions, {
currentPage: result.payload.pageInfo.currentPage,
});
this.processing$.next(false);
this.listDataRD = result;
this.orcidQueueService.clearFindByProfileItemRequests();
},
}),
);
}

/**
* Return the list of orcid queue records
* Handle pagination change.
* Scroll to the pagination element and update the list with the new page
*/
getList(): Observable<RemoteData<PaginatedList<OrcidQueue>>> {
return this.list$.asObservable();
onPaginationChange(){
const element = this._document.getElementById(`p-${this.paginationOptions.id}`);
if (element) {
setTimeout(() => {
element.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });

Check warning on line 158 in src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts#L157-L158

Added lines #L157 - L158 were not covered by tests
}, 300);
}
this.updateList();
}

/**
Expand Down Expand Up @@ -222,20 +244,40 @@
* @param orcidQueue The OrcidQueue object to discard
*/
discardEntry(orcidQueue: OrcidQueue) {
this.processing$.next(true);
this.subs.push(this.orcidQueueService.deleteById(orcidQueue.id).pipe(
getFirstCompletedRemoteData(),
).subscribe((remoteData) => {
this.processing$.next(false);
if (remoteData.isSuccess) {
this.removeEntryFromList(orcidQueue.id);
this.notificationsService.success(this.translateService.get('person.page.orcid.sync-queue.discard.success'));
this.updateList();
} else {
this.notificationsService.error(this.translateService.get('person.page.orcid.sync-queue.discard.error'));
}
}));
}

/**
* Remove an entry from the list.
* If the last element of the page is removed, navigate to the previous page.
* @param id The id of the entry to remove
*/
removeEntryFromList(id: number) {
const index = this.listDataRD?.payload?.page.findIndex((item) => item.id === id);

Check warning on line 265 in src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts#L265

Added line #L265 was not covered by tests
if (index > -1) {
this.listDataRD.payload.page.splice(index, 1);

Check warning on line 267 in src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts#L267

Added line #L267 was not covered by tests
if (this.listDataRD.payload.page.length === 0 && this.listDataRD.payload.pageInfo.currentPage > 0) {
this.paginationOptions.currentPage = this.paginationOptions.currentPage - 1;
this.router.navigate([], {

Check warning on line 270 in src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts#L269-L270

Added lines #L269 - L270 were not covered by tests
queryParams: {
[`${this.paginationOptions.id}.page`]: this.paginationOptions.currentPage,
},
fragment: `p-${this.paginationOptions.id}`,
});
this.updateList();

Check warning on line 276 in src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/item-page/orcid-page/orcid-queue/orcid-queue.component.ts#L276

Added line #L276 was not covered by tests
}
}
}

/**
* Send a queue entry to orcid for the synchronization
*
Expand Down Expand Up @@ -327,7 +369,6 @@
* Unsubscribe from all subscriptions
*/
ngOnDestroy(): void {
this.list$ = null;
this.subs.filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe());
}
Expand Down
Loading