I am receiving an error I can't understand the cause.
I've read everything I could on effect creation, but still can't figure this error cause.
I'm new to angular and especially Ngrx, so maybe I don't see something obvious.
I've created an action:
export interface ServerAuthData{
AccessToken: string;
RefreshToken: string;
Utente: Utente;
}
export interface AuthState {
isAuthenticated: boolean;
errorMessage: string;
accessToken: string;
refreshToken: string;
utente: Utente;
}
export const authLogin = createAction(
'[Auth] Login',
props<{ username: string,password: string }>()
);
its reducer:
const reducer = createReducer(
initialState,
on(authLogin, (state) => ({ ...state })),
on(authLogout, (state) => (
{ ...state,
isAuthenticated: false ,
utente: null,
accessToken: "",
refreshToken: "",
})),
on(authLoginSuccess, (state,{authState}) => (
{ ...state,
isAuthenticated: true,
utente: authState.utente,
accessToken: authState.accessToken,
refreshToken: authState.refreshToken,
erroMessage: authState.errorMessage,
}))
);
the service I'm going to call to login the user:
login(username: string,password: string) {
var formData: any = new FormData();
formData.append("username", username);
formData.append("password", password);
console.log("Chiamo il server");
return this.http.post<ServerAuthData>(this.loginUrl, formData);
}
and finally the effect:
login$ =
createEffect(() =>
this.actions$.pipe(
ofType(authLogin),
switchMap((action) =>
this.authService.login(action.username,action.password).pipe(
tap((serverAuthData) => console.log('Dopo login. Ricevuto:' + serverAuthData)),
map((serverAuthData) =>
console.log("Converting data");
authLoginSuccess({authState:{
isAuthenticated: true,
accessToken: serverAuthData.AccessToken,
errorMessage: "",
refreshToken : serverAuthData.RefreshToken,
utente: serverAuthData.Utente,
}}
)),
catchError((error: any) => of(this.authService.loginFailure(error)))
)
)
)
);
Everything worked before using map instead of switchMap,and with {dispatch: false} but of course no new action was dispatched. Switching to switchMap and removing {dispatch: false}has generate the error:
Type 'Observable<void | ({ authState: AuthState; } & TypedAction<"[Auth] LoginSuccess">)>' is not assignable to type 'EffectResult'.
Type 'Observable<void | ({ authState: AuthState; } & TypedAction<"[Auth] LoginSuccess">)>' is not assignable to type 'Observable'.
Type 'void | ({ authState: AuthState; } & TypedAction<"[Auth] LoginSuccess">)' is not assignable to type 'Action'.
Type 'void' is not assignable to type 'Action'.
Can someone help me?
EDIT: I understand that switchMap HAS to return a value. Doesn't dispatching a new action returns a value? I've also tried return authLoginSuccess but no way.