@@ -13,6 +13,10 @@ import {
1313 switchMap ,
1414 tap ,
1515} from 'rxjs/operators' ;
16+ import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service' ;
17+ import { RemoteData } from 'src/app/core/data/remote-data' ;
18+ import { ConfigurationProperty } from 'src/app/core/shared/configuration-property.model' ;
19+ import { getFirstCompletedRemoteData } from 'src/app/core/shared/operators' ;
1620
1721import {
1822 AuthActionTypes ,
@@ -72,14 +76,23 @@ export class SuggestionTargetsEffects {
7276 ) , { dispatch : false } ) ;
7377
7478 /**
75- * Show a notification on error.
79+ * Retrieve the current user suggestions after retrieving the authenticated user
7680 */
7781 retrieveUserTargets$ = createEffect ( ( ) => this . actions$ . pipe (
7882 ofType ( AuthActionTypes . RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS ) ,
7983 switchMap ( ( action : RetrieveAuthenticatedEpersonSuccessAction ) => {
80- return this . suggestionsService . retrieveCurrentUserSuggestions ( action . payload ) . pipe (
81- map ( ( suggestionTargets : SuggestionTarget [ ] ) => new AddUserSuggestionsAction ( suggestionTargets ) ) ,
82- ) ;
84+ return this . configurationService . findByPropertyName ( 'researcher-profile.entity-type' ) . pipe (
85+ getFirstCompletedRemoteData ( ) ,
86+ switchMap ( ( configRD : RemoteData < ConfigurationProperty > ) => {
87+ if ( configRD . hasSucceeded && configRD . payload . values . length > 0 ) {
88+ return this . suggestionsService . retrieveCurrentUserSuggestions ( action . payload ) . pipe (
89+ map ( ( suggestionTargets : SuggestionTarget [ ] ) => new AddUserSuggestionsAction ( suggestionTargets ) ) ,
90+ ) ;
91+ } else {
92+ return of ( new AddUserSuggestionsAction ( [ ] ) ) ;
93+ }
94+ } ,
95+ ) ) ;
8396 } ) ) ) ;
8497
8598 /**
@@ -91,16 +104,35 @@ export class SuggestionTargetsEffects {
91104 return this . store$ . select ( ( state : any ) => state . core . auth . userId )
92105 . pipe (
93106 switchMap ( ( userId : string ) => {
94- return this . suggestionsService . retrieveCurrentUserSuggestions ( userId )
95- . pipe (
96- map ( ( suggestionTargets : SuggestionTarget [ ] ) => new AddUserSuggestionsAction ( suggestionTargets ) ) ,
97- catchError ( ( error : unknown ) => {
98- if ( error instanceof Error ) {
99- console . error ( error . message ) ;
100- }
101- return of ( new RefreshUserSuggestionsErrorAction ( ) ) ;
102- } ) ,
103- ) ;
107+ if ( ! userId ) {
108+ return of ( new AddUserSuggestionsAction ( [ ] ) ) ;
109+ }
110+ return this . configurationService . findByPropertyName ( 'researcher-profile.entity-type' ) . pipe (
111+ getFirstCompletedRemoteData ( ) ,
112+ switchMap ( ( configRD : RemoteData < ConfigurationProperty > ) => {
113+ if ( configRD . hasSucceeded && configRD . payload . values . length > 0 ) {
114+ return this . suggestionsService . retrieveCurrentUserSuggestions ( userId )
115+ . pipe (
116+ map ( ( suggestionTargets : SuggestionTarget [ ] ) => new AddUserSuggestionsAction ( suggestionTargets ) ) ,
117+ catchError ( ( error : unknown ) => {
118+ if ( error instanceof Error ) {
119+ console . error ( error . message ) ;
120+ }
121+ return of ( new RefreshUserSuggestionsErrorAction ( ) ) ;
122+ } ) ,
123+ ) ;
124+ } else {
125+ return of ( new AddUserSuggestionsAction ( [ ] ) ) ;
126+ }
127+ } ,
128+ ) ,
129+ catchError ( ( error : unknown ) => {
130+ if ( error instanceof Error ) {
131+ console . error ( error . message ) ;
132+ }
133+ return of ( new RefreshUserSuggestionsErrorAction ( ) ) ;
134+ } ) ,
135+ ) ;
104136 } ) ,
105137 catchError ( ( error : unknown ) => {
106138 if ( error instanceof Error ) {
@@ -119,13 +151,15 @@ export class SuggestionTargetsEffects {
119151 * @param {TranslateService } translate
120152 * @param {NotificationsService } notificationsService
121153 * @param {SuggestionsService } suggestionsService
154+ * @param {ConfigurationDataService } configurationService
122155 */
123156 constructor (
124157 private actions$ : Actions ,
125158 private store$ : Store < any > ,
126159 private translate : TranslateService ,
127160 private notificationsService : NotificationsService ,
128161 private suggestionsService : SuggestionsService ,
162+ private configurationService : ConfigurationDataService ,
129163 ) {
130164 }
131165}
0 commit comments