Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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