Skip to content

Commit 647bf4f

Browse files
committed
[DSC-1700] fix(entity-dropdown): add support for importing from external sources
1 parent 03d4de2 commit 647bf4f

File tree

3 files changed

+82
-37
lines changed

3 files changed

+82
-37
lines changed

src/app/my-dspace-page/my-dspace-new-submission/my-dspace-new-external-dropdown/my-dspace-new-external-dropdown.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class="dropdown-menu p-0"
2626
id="importControlsDropdownMenu"
2727
aria-labelledby="dropdownImport">
28-
<ds-entity-dropdown [isSubmission]="false" (selectionChange)="openPage($event)"></ds-entity-dropdown>
28+
<ds-entity-dropdown [isSubmission]="false" [isImportFromExternalSource]="true" (selectionChange)="openPage($event)"></ds-entity-dropdown>
2929
</div>
3030
</div>
3131
}

src/app/shared/entity-dropdown/entity-dropdown.component.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import {
1717
} from '@ngx-translate/core';
1818
import { getTestScheduler } from 'jasmine-marbles';
1919
import { InfiniteScrollDirective } from 'ngx-infinite-scroll';
20+
import { of } from 'rxjs';
2021
import { TestScheduler } from 'rxjs/testing';
2122

2223
import { EntityTypeDataService } from '../../core/data/entity-type-data.service';
24+
import { ItemExportFormatService } from '../../core/itemexportformat/item-export-format.service';
2325
import {
2426
ItemExportFormat,
2527
ItemExportFormatMap,
@@ -108,6 +110,10 @@ describe('EntityDropdownComponent', () => {
108110
getAllAuthorizedRelationshipTypeImport: jasmine.createSpy('getAllAuthorizedRelationshipTypeImport'),
109111
});
110112

113+
const itemExportFormatServiceMock: any = jasmine.createSpyObj('ItemExportFormatService', {
114+
byEntityTypeAndMolteplicity: jasmine.createSpy('byEntityTypeAndMolteplicity'),
115+
});
116+
111117
const translateServiceMock: any = {
112118
instant(name) {
113119
return 'Statistics';
@@ -130,6 +136,7 @@ describe('EntityDropdownComponent', () => {
130136
],
131137
providers: [
132138
{ provide: EntityTypeDataService, useValue: entityTypeServiceMock },
139+
{ provide: ItemExportFormatService, useValue: itemExportFormatServiceMock },
133140
{ provide: TranslateService, useValue: translateServiceMock },
134141
ChangeDetectorRef,
135142
],
@@ -145,6 +152,7 @@ describe('EntityDropdownComponent', () => {
145152
componentAsAny = fixture.componentInstance;
146153
componentAsAny.entityTypeService.getAllAuthorizedRelationshipType.and.returnValue(paginatedEntitiesRD$);
147154
componentAsAny.entityTypeService.getAllAuthorizedRelationshipTypeImport.and.returnValue(paginatedEntitiesRD$);
155+
componentAsAny.itemExportFormatService.byEntityTypeAndMolteplicity.and.returnValue(of(entityFormatList));
148156
component.isSubmission = true;
149157
});
150158

@@ -164,6 +172,24 @@ describe('EntityDropdownComponent', () => {
164172
expect((component as any).entityTypeService.getAllAuthorizedRelationshipType).toHaveBeenCalled();
165173
});
166174

175+
it('should init component with entities list when isImportFromExternalSource is true', () => {
176+
component.isSubmission = false;
177+
component.isImportFromExternalSource = true;
178+
spyOn(component.subs, 'push');
179+
spyOn(component, 'resetPagination');
180+
spyOn(component, 'populateEntityList').and.callThrough();
181+
182+
scheduler.schedule(() => fixture.detectChanges());
183+
scheduler.flush();
184+
const elements = fixture.debugElement.queryAll(By.css('.entity-item'));
185+
186+
expect(elements.length).toEqual(5);
187+
expect(component.subs.push).toHaveBeenCalled();
188+
expect(component.resetPagination).toHaveBeenCalled();
189+
expect(component.populateEntityList).toHaveBeenCalled();
190+
expect((component as any).entityTypeService.getAllAuthorizedRelationshipTypeImport).toHaveBeenCalled();
191+
});
192+
167193
it('should trigger onSelect method when select a new entity from list', () => {
168194
scheduler.schedule(() => fixture.detectChanges());
169195
scheduler.flush();
@@ -202,4 +228,12 @@ describe('EntityDropdownComponent', () => {
202228
expect(component.searchListEntity).toEqual([]);
203229
});
204230

231+
it('should invoke the method byEntityTypeAndMolteplicity of EntityTypeService when isSubmission is false', () => {
232+
component.isSubmission = false;
233+
234+
scheduler.schedule(() => fixture.detectChanges());
235+
scheduler.flush();
236+
237+
expect((component as any).itemExportFormatService.byEntityTypeAndMolteplicity).toHaveBeenCalled();
238+
});
205239
});

src/app/shared/entity-dropdown/entity-dropdown.component.ts

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
reduce,
2626
startWith,
2727
switchMap,
28+
take,
2829
tap,
2930
} from 'rxjs/operators';
3031
import { SortPipe } from 'src/app/shared/utils/sort.pipe';
@@ -36,12 +37,14 @@ import {
3637
PaginatedList,
3738
} from '../../core/data/paginated-list.model';
3839
import { RemoteData } from '../../core/data/remote-data';
39-
import { ItemExportFormatService } from '../../core/itemexportformat/item-export-format.service';
40+
import {
41+
ItemExportFormatMolteplicity,
42+
ItemExportFormatService,
43+
} from '../../core/itemexportformat/item-export-format.service';
4044
import { ItemType } from '../../core/shared/item-relationships/item-type.model';
4145
import { getFirstSucceededRemoteWithNotEmptyData } from '../../core/shared/operators';
4246
import {
4347
hasValue,
44-
isEmpty,
4548
isNotNull,
4649
} from '../empty.util';
4750
import { ThemedLoadingComponent } from '../loading/themed-loading.component';
@@ -84,11 +87,16 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
8487
public searchListEntity: ItemType[] = [];
8588

8689
/**
87-
* TRUE if the parent operation is a 'new submission' operation, FALSE otherwise (eg.: is an 'Import metadata from an external source'
88-
* operation).
90+
* TRUE if the parent operation is a 'new submission' operation, FALSE otherwise (eg.: is an 'Import metadata from an external source' operation).
8991
*/
9092
@Input() isSubmission: boolean;
9193

94+
/**
95+
* TRUE if the main operation is "Import metadata from an external source", FALSE otherwise.
96+
* Used to determine which list type to populate.
97+
*/
98+
@Input() isImportFromExternalSource: boolean;
99+
92100
/**
93101
* The entity to output to the parent component
94102
*/
@@ -127,6 +135,8 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
127135
constructor(
128136
private changeDetectorRef: ChangeDetectorRef,
129137
private entityTypeService: EntityTypeDataService,
138+
private itemExportFormatService: ItemExportFormatService,
139+
private el: ElementRef,
130140
private translate: TranslateService,
131141
) { }
132142

@@ -190,23 +200,44 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
190200
public populateEntityList(page: number) {
191201
this.isLoadingList.next(true);
192202
let searchListEntity$: Observable<RemoteData<PaginatedList<ItemType>>>;
193-
// Set the pagination info
194-
const findOptions: FindListOptions = {
195-
elementsPerPage: 10,
196-
currentPage: page,
197-
};
198-
if (this.isSubmission) {
199-
searchListEntity$ =
200-
this.entityTypeService.getAllAuthorizedRelationshipType(findOptions);
203+
if (this.isSubmission || this.isImportFromExternalSource) {
204+
// Set the pagination info
205+
const findOptions: FindListOptions = {
206+
elementsPerPage: 10,
207+
currentPage: page,
208+
};
209+
210+
searchListEntity$ = this.isSubmission ?
211+
this.entityTypeService.getAllAuthorizedRelationshipType(findOptions) :
212+
this.entityTypeService.getAllAuthorizedRelationshipTypeImport(findOptions);
213+
214+
searchListEntity$ = searchListEntity$.pipe(
215+
getFirstSucceededRemoteWithNotEmptyData(),
216+
tap(entityType => {
217+
if ((this.searchListEntity.length + findOptions.elementsPerPage) >= entityType.payload.totalElements) {
218+
this.hasNextPage = false;
219+
}
220+
}),
221+
);
201222
} else {
202223
searchListEntity$ =
203-
this.entityTypeService.getAllAuthorizedRelationshipTypeImport(findOptions);
224+
this.itemExportFormatService.byEntityTypeAndMolteplicity(null, ItemExportFormatMolteplicity.MULTIPLE)
225+
.pipe(
226+
take(1),
227+
map((formatTypes: any) => {
228+
const entityList: ItemType[] = Object.keys(formatTypes)
229+
.filter((entityType: string) => isNotNull(entityType) && entityType !== 'null')
230+
.map((entityType: string) => ({
231+
id: entityType,
232+
label: entityType,
233+
} as any));
234+
return createSuccessfulRemoteDataObject(buildPaginatedList(null, entityList));
235+
}),
236+
tap(() => this.hasNextPage = false),
237+
);
204238
}
205239

206240
this.searchListEntity$ = searchListEntity$.pipe(
207-
getFirstSucceededRemoteWithNotEmptyData(),
208-
map((formatTypes: RemoteData<PaginatedList<ItemType>>) =>this.parseItemTypesResponse(formatTypes)),
209-
tap((entityTypes) => this.hasNextPages(findOptions, entityTypes)),
210241
switchMap((entityType: RemoteData<PaginatedList<ItemType>>) => entityType.payload.page),
211242
map((item: ItemType) => {
212243
return {
@@ -229,26 +260,6 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
229260
);
230261
}
231262

232-
private parseItemTypesResponse(formatTypes: RemoteData<PaginatedList<ItemType>>) {
233-
if (formatTypes.statusCode !== 200) {
234-
return createSuccessfulRemoteDataObject(buildPaginatedList(null, []));
235-
}
236-
const entityList: ItemType[] = (formatTypes?.payload?.page || [])
237-
.filter(itemType => isNotNull(itemType?.id));
238-
return createSuccessfulRemoteDataObject(buildPaginatedList(null, entityList));
239-
}
240-
241-
private hasNextPages<A>(findOptions: FindListOptions, entityType: RemoteData<PaginatedList<A>>) {
242-
if (
243-
findOptions.elementsPerPage > 0 &&
244-
(isEmpty(entityType?.payload?.totalElements) || entityType.payload.totalElements < findOptions.elementsPerPage)
245-
) {
246-
this.hasNextPage = false;
247-
} else {
248-
this.hasNextPage = true;
249-
}
250-
}
251-
252263
/**
253264
* Reset pagination values
254265
*/

0 commit comments

Comments
 (0)