@@ -25,7 +25,6 @@ import {
2525 reduce ,
2626 startWith ,
2727 switchMap ,
28- take ,
2928 tap ,
3029} from 'rxjs/operators' ;
3130import { SortPipe } from 'src/app/shared/utils/sort.pipe' ;
@@ -37,14 +36,12 @@ import {
3736 PaginatedList ,
3837} from '../../core/data/paginated-list.model' ;
3938import { RemoteData } from '../../core/data/remote-data' ;
40- import {
41- ItemExportFormatMolteplicity ,
42- ItemExportFormatService ,
43- } from '../../core/itemexportformat/item-export-format.service' ;
39+ import { ItemExportFormatService } from '../../core/itemexportformat/item-export-format.service' ;
4440import { ItemType } from '../../core/shared/item-relationships/item-type.model' ;
4541import { getFirstSucceededRemoteWithNotEmptyData } from '../../core/shared/operators' ;
4642import {
4743 hasValue ,
44+ isEmpty ,
4845 isNotNull ,
4946} from '../empty.util' ;
5047import { ThemedLoadingComponent } from '../loading/themed-loading.component' ;
@@ -87,7 +84,8 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
8784 public searchListEntity : ItemType [ ] = [ ] ;
8885
8986 /**
90- * TRUE if the parent operation is a 'new submission' operation, FALSE otherwise (eg.: is an 'Import metadata from an external source' operation).
87+ * TRUE if the parent operation is a 'new submission' operation, FALSE otherwise (eg.: is an 'Import metadata from an external source'
88+ * operation).
9189 */
9290 @Input ( ) isSubmission : boolean ;
9391
@@ -129,8 +127,6 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
129127 constructor (
130128 private changeDetectorRef : ChangeDetectorRef ,
131129 private entityTypeService : EntityTypeDataService ,
132- private itemExportFormatService : ItemExportFormatService ,
133- private el : ElementRef ,
134130 private translate : TranslateService ,
135131 ) { }
136132
@@ -193,41 +189,24 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
193189 */
194190 public populateEntityList ( page : number ) {
195191 this . isLoadingList . next ( true ) ;
196- let searchListEntity$ ;
192+ let searchListEntity$ : Observable < RemoteData < PaginatedList < ItemType > > > ;
193+ // Set the pagination info
194+ const findOptions : FindListOptions = {
195+ elementsPerPage : 10 ,
196+ currentPage : page ,
197+ } ;
197198 if ( this . isSubmission ) {
198- // Set the pagination info
199- const findOptions : FindListOptions = {
200- elementsPerPage : 10 ,
201- currentPage : page ,
202- } ;
203199 searchListEntity$ =
204- this . entityTypeService . getAllAuthorizedRelationshipType ( findOptions )
205- . pipe (
206- getFirstSucceededRemoteWithNotEmptyData ( ) ,
207- tap ( entityType => {
208- if ( ( this . searchListEntity . length + findOptions . elementsPerPage ) >= entityType . payload . totalElements ) {
209- this . hasNextPage = false ;
210- }
211- } ) ,
212- ) ;
200+ this . entityTypeService . getAllAuthorizedRelationshipType ( findOptions ) ;
213201 } else {
214202 searchListEntity$ =
215- this . itemExportFormatService . byEntityTypeAndMolteplicity ( null , ItemExportFormatMolteplicity . MULTIPLE )
216- . pipe (
217- take ( 1 ) ,
218- map ( ( formatTypes : any ) => {
219- const entityList : ItemType [ ] = Object . keys ( formatTypes )
220- . filter ( ( entityType : string ) => isNotNull ( entityType ) && entityType !== 'null' )
221- . map ( ( entityType : string ) => ( {
222- id : entityType ,
223- label : entityType ,
224- } as any ) ) ;
225- return createSuccessfulRemoteDataObject ( buildPaginatedList ( null , entityList ) ) ;
226- } ) ,
227- tap ( ( ) => this . hasNextPage = false ) ,
228- ) ;
203+ this . entityTypeService . getAllAuthorizedRelationshipTypeImport ( findOptions ) ;
229204 }
205+
230206 this . searchListEntity$ = searchListEntity$ . pipe (
207+ getFirstSucceededRemoteWithNotEmptyData ( ) ,
208+ map ( ( formatTypes : RemoteData < PaginatedList < ItemType > > ) => this . parseItemTypesResponse ( formatTypes ) ) ,
209+ tap ( ( entityTypes ) => this . hasNextPages ( findOptions , entityTypes ) ) ,
231210 switchMap ( ( entityType : RemoteData < PaginatedList < ItemType > > ) => entityType . payload . page ) ,
232211 map ( ( item : ItemType ) => {
233212 return {
@@ -239,6 +218,7 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
239218 reduce ( ( acc : any , value : any ) => [ ...acc , value ] , [ ] ) ,
240219 startWith ( [ ] ) ,
241220 ) ;
221+
242222 this . subs . push (
243223 this . searchListEntity$ . subscribe ( {
244224 next : ( result : ItemType [ ] ) => {
@@ -249,6 +229,26 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
249229 ) ;
250230 }
251231
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+
252252 /**
253253 * Reset pagination values
254254 */
0 commit comments