This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Actions, createEffect, ofType } from '@ngrx/effects'; | |
import { map, switchMap } from 'rxjs/operators'; | |
import { doLogin, setLoginStateSuccess, setErrorMessage } from './login.action'; | |
import { LoginService } from '../../services/login/login.service'; | |
@Injectable() | |
export class LoginEffects { | |
setLoginStateSuccess$ = createEffect(() => | |
this.actions$.pipe( | |
ofType(doLogin), // ログインのアクションの時、ここが動く | |
switchMap(({loginId, password}) => // actionで引数を2つ定義しましたね | |
this.loginService.doLogin(loginId, password).pipe( // APIを実行します。このAPIはブログの最後にgitのurlを参考に | |
map(result => { | |
if (result) { | |
return setLoginStateSuccess({ isLogin: result }); // trueだったらsetLoginStateSuccessアクション実行 | |
} | |
return setErrorMessage({ errorMessage: 'ログインに失敗しました' }); // falseだったらsetErrorMessageアクション実行 | |
}), | |
// catchError(err => of(doLoginFailure({error: err}))) | |
)), | |
) | |
); | |
constructor(private actions$: Actions, private loginService: LoginService) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment