@@ -13,6 +13,10 @@ import {
13
13
switchMap ,
14
14
tap ,
15
15
} 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' ;
16
20
17
21
import {
18
22
AuthActionTypes ,
@@ -72,14 +76,23 @@ export class SuggestionTargetsEffects {
72
76
) , { dispatch : false } ) ;
73
77
74
78
/**
75
- * Show a notification on error.
79
+ * Retrieve the current user suggestions after retrieving the authenticated user
76
80
*/
77
81
retrieveUserTargets$ = createEffect ( ( ) => this . actions$ . pipe (
78
82
ofType ( AuthActionTypes . RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS ) ,
79
83
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
+ ) ) ;
83
96
} ) ) ) ;
84
97
85
98
/**
@@ -91,16 +104,35 @@ export class SuggestionTargetsEffects {
91
104
return this . store$ . select ( ( state : any ) => state . core . auth . userId )
92
105
. pipe (
93
106
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
+ ) ;
104
136
} ) ,
105
137
catchError ( ( error : unknown ) => {
106
138
if ( error instanceof Error ) {
@@ -119,13 +151,15 @@ export class SuggestionTargetsEffects {
119
151
* @param {TranslateService } translate
120
152
* @param {NotificationsService } notificationsService
121
153
* @param {SuggestionsService } suggestionsService
154
+ * @param {ConfigurationDataService } configurationService
122
155
*/
123
156
constructor (
124
157
private actions$ : Actions ,
125
158
private store$ : Store < any > ,
126
159
private translate : TranslateService ,
127
160
private notificationsService : NotificationsService ,
128
161
private suggestionsService : SuggestionsService ,
162
+ private configurationService : ConfigurationDataService ,
129
163
) {
130
164
}
131
165
}
0 commit comments