Commit a24de7da authored by Emily Jensen's avatar Emily Jensen

New effects file

parent 731634ab
import { Injectable } from "@angular/core";
import { Action, Store, select } from "@ngrx/store";
import { Effect, Actions, ofType } from "@ngrx/effects";
import { Observable, of } from "rxjs";
import { withLatestFrom, map, catchError, exhaustMap } from "rxjs/operators";
import {
ChangePasswordActionTypes,
SubmitForm,
SubmitFormSuccess,
SubmitFormFailure
} from "./change-password.actions";
import * as fromAccount from "../account.reducer";
import { UserService } from "../user";
@Injectable()
export class ChangePasswordEffects {
constructor(
private actions$: Actions,
private userService: UserService,
private store: Store<any>
) {}
@Effect()
changePassword$: Observable<Action> = this.actions$.pipe(
ofType<SubmitForm>(ChangePasswordActionTypes.SUBMIT_FORM),
withLatestFrom(this.store.pipe(select(fromAccount.changePassword))),
map(([action, form]) => form.value),
exhaustMap(formValue =>
this.userService
.changePassword(formValue.oldPassword, formValue.newPassword)
.pipe(
exhaustMap(() =>
this.userService.makeAndSetBackupCode(formValue.newPassword).pipe(
// change to backupCode once login functionality is added
map(backupCode => new SubmitFormSuccess(backupCode)),
catchError(err =>
of(new SubmitFormFailure(["Unexpected backup code error."]))
)
)
),
catchError(err => {
if (err.status === 400) {
return of(new SubmitFormFailure(["Incorrect password"]));
} else {
return of(new SubmitFormFailure(["Unexpected error."]));
}
})
)
)
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment