@@ -35,6 +35,14 @@ import { RemoteData } from '../../../core/data/remote-data';
3535import { NotificationsService } from '../../notifications/notifications.service' ;
3636import { TranslateService } from '@ngx-translate/core' ;
3737import { DSONameService } from '../../../core/breadcrumbs/dso-name.service' ;
38+ import {
39+ ListableNotificationObject
40+ } from '../../object-list/listable-notification-object/listable-notification-object.model' ;
41+ import { ListableObject } from '../../object-collection/shared/listable-object.model' ;
42+ import { NotificationType } from '../../notifications/models/notification-type' ;
43+ import {
44+ LISTABLE_NOTIFICATION_OBJECT
45+ } from '../../object-list/listable-notification-object/listable-notification-object.resource-type' ;
3846
3947@Component ( {
4048 selector : 'ds-dso-selector' ,
@@ -82,7 +90,7 @@ export class DSOSelectorComponent implements OnInit, OnDestroy {
8290 /**
8391 * List with search results of DSpace objects for the current query
8492 */
85- listEntries$ : BehaviorSubject < SearchResult < DSpaceObject > [ ] > = new BehaviorSubject ( null ) ;
93+ listEntries$ : BehaviorSubject < ListableObject [ ] > = new BehaviorSubject ( null ) ;
8694
8795 /**
8896 * The current page to load
@@ -116,11 +124,6 @@ export class DSOSelectorComponent implements OnInit, OnDestroy {
116124 */
117125 linkTypes = CollectionElementLinkType ;
118126
119- /**
120- * Track whether the element has the mouse over it
121- */
122- isMouseOver = false ;
123-
124127 /**
125128 * Array to track all subscriptions and unsubscribe them onDestroy
126129 * @type {Array }
@@ -182,22 +185,26 @@ export class DSOSelectorComponent implements OnInit, OnDestroy {
182185 } )
183186 ) ;
184187 } )
185- ) . subscribe ( ( rd ) => {
186- this . loading = false ;
187- if ( rd . hasSucceeded ) {
188- const currentEntries = this . listEntries$ . getValue ( ) ;
189- if ( hasNoValue ( currentEntries ) ) {
190- this . listEntries$ . next ( rd . payload . page ) ;
191- } else {
192- this . listEntries$ . next ( [ ... currentEntries , ... rd . payload . page ] ) ;
193- }
194- // Check if there are more pages available after the current one
195- this . hasNextPage = rd . payload . totalElements > this . listEntries$ . getValue ( ) . length ;
188+ ) . subscribe ( ( rd : RemoteData < PaginatedList < SearchResult < DSpaceObject > > > ) => {
189+ this . updateList ( rd ) ;
190+ } ) ) ;
191+ }
192+
193+ updateList ( rd : RemoteData < PaginatedList < SearchResult < DSpaceObject > > > ) {
194+ this . loading = false ;
195+ const currentEntries = this . listEntries$ . getValue ( ) ;
196+ if ( rd . hasSucceeded ) {
197+ if ( hasNoValue ( currentEntries ) ) {
198+ this . listEntries$ . next ( rd . payload . page ) ;
196199 } else {
197- this . listEntries$ . next ( null ) ;
198- this . hasNextPage = false ;
200+ this . listEntries$ . next ( [ ...currentEntries , ...rd . payload . page ] ) ;
199201 }
200- } ) ) ;
202+ // Check if there are more pages available after the current one
203+ this . hasNextPage = rd . payload . totalElements > this . listEntries$ . getValue ( ) . length ;
204+ } else {
205+ this . listEntries$ . next ( [ ...( hasNoValue ( currentEntries ) ? [ ] : this . listEntries$ . getValue ( ) ) , new ListableNotificationObject ( NotificationType . Error , 'dso-selector.results-could-not-be-retrieved' , LISTABLE_NOTIFICATION_OBJECT . value ) ] ) ;
206+ this . hasNextPage = false ;
207+ }
201208 }
202209
203210 /**
@@ -211,16 +218,19 @@ export class DSOSelectorComponent implements OnInit, OnDestroy {
211218 * Perform a search for the current query and page
212219 * @param query Query to search objects for
213220 * @param page Page to retrieve
221+ * @param useCache Whether or not to use the cache
214222 */
215- search ( query : string , page : number ) : Observable < RemoteData < PaginatedList < SearchResult < DSpaceObject > > > > {
223+ search ( query : string , page : number , useCache : boolean = true ) : Observable < RemoteData < PaginatedList < SearchResult < DSpaceObject > > > > {
216224 return this . searchService . search (
217225 new PaginatedSearchOptions ( {
218226 query : query ,
219227 dsoTypes : this . types ,
220228 pagination : Object . assign ( { } , this . defaultPagination , {
221229 currentPage : page
222230 } )
223- } )
231+ } ) ,
232+ null ,
233+ useCache ,
224234 ) . pipe (
225235 getFirstCompletedRemoteData ( )
226236 ) ;
@@ -262,7 +272,28 @@ export class DSOSelectorComponent implements OnInit, OnDestroy {
262272 this . subs . filter ( ( sub ) => hasValue ( sub ) ) . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
263273 }
264274
265- getName ( searchResult : SearchResult < DSpaceObject > ) : string {
266- return this . dsoNameService . getName ( searchResult . indexableObject ) ;
275+ /**
276+ * Handles the user clicks on the {@link ListableObject}s. When the {@link listableObject} is a
277+ * {@link ListableObject} it will retry the error when the user clicks it. Otherwise it will emit the {@link onSelect}.
278+ *
279+ * @param listableObject The {@link ListableObject} to evaluate
280+ */
281+ onClick ( listableObject : ListableObject ) : void {
282+ if ( hasValue ( ( listableObject as SearchResult < DSpaceObject > ) . indexableObject ) ) {
283+ this . onSelect . emit ( ( listableObject as SearchResult < DSpaceObject > ) . indexableObject ) ;
284+ } else {
285+ this . listEntries$ . value . pop ( ) ;
286+ this . hasNextPage = true ;
287+ this . search ( this . input . value ? this . input . value : '' , this . currentPage$ . value , false ) . pipe (
288+ getFirstCompletedRemoteData ( ) ,
289+ ) . subscribe ( ( rd : RemoteData < PaginatedList < SearchResult < DSpaceObject > > > ) => {
290+ this . updateList ( rd ) ;
291+ } ) ;
292+ }
293+ }
294+
295+ getName ( listableObject : ListableObject ) : string {
296+ return hasValue ( ( listableObject as SearchResult < DSpaceObject > ) . indexableObject ) ?
297+ this . dsoNameService . getName ( ( listableObject as SearchResult < DSpaceObject > ) . indexableObject ) : null ;
267298 }
268299}
0 commit comments