Skip to content

Instantly share code, notes, and snippets.

@ksakae1216
Created January 29, 2022 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksakae1216/acc5b3c36ed9a0fbf7c8e58e96571de2 to your computer and use it in GitHub Desktop.
Save ksakae1216/acc5b3c36ed9a0fbf7c8e58e96571de2 to your computer and use it in GitHub Desktop.
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