Commit e8e40731 authored by Emily Jensen's avatar Emily Jensen

Moved change password functionality to effects file

parent 2a7db854
import { take } from "rxjs/operators";
import { Component } from "@angular/core";
import { Store, select } from "@ngrx/store";
// import { selectChangePasswordNewBackupCode } from "../account.reducer";
import { SubmitForm, ResetForm, SubmitFormSuccess, SubmitFormFailure } from "./change-password.actions";
import { selectChangePasswordNewBackupCode } from "../account.reducer";
import { SubmitForm, ResetForm } from "./change-password.actions";
import * as fromRoot from "../../app.reducers";
import * as fromAccount from "../account.reducer";
import * as fromChangePassword from "./change-password.reducer";
import { LogoutAction } from "../account.actions";
import { UserService } from "../user";
import { SetValueAction } from "ngrx-forms";
@Component({
selector: "change-password-container",
template: `
<change-password
[form]="form$ | async"
[hasStarted]="hasStarted$ | async"
[hasFinished]="hasFinished$ | async"
[nonFieldErrors]="nonFieldErrors"
[backupCode]="backupCode"
(changePassword)="changePassword()"
(toggleConfirm)="toggleConfirm()"
(reset)="reset()"
(onForgotPassword)="onForgotPassword()">
<change-password
[form]="form$ | async"
[hasStarted]="hasStarted$ | async"
[hasFinished]="hasFinished$ | async"
[nonFieldErrors]="nonFieldErrors"
[backupCode]="backupCode"
(changePassword)="changePassword()"
(toggleConfirm)="toggleConfirm()"
(reset)="reset()"
(onForgotPassword)="onForgotPassword()"
>
</change-password>
`
`
})
export class ChangePasswordContainer {
form$ = this.store.pipe(select(fromAccount.changePassword));
hasStarted$ = this.store.pipe(select(fromAccount.changePasswordHasStarted));
hasFinished$ = this.store.pipe(select(fromAccount.changePasswordHasFinished));
errorMessage$ = this.store.select(fromAccount.changePasswordErrorMessage);
// backupCode: string;
backupCode: string;
showConfirm: boolean;
nonFieldErrors: string[];
clickMessage = "";
backupCode = "CBVDCH4G23MJ46FXJBR8GDX4WKBF97VR";
constructor(
private store: Store<fromRoot.IState>,
private userService: UserService
) {
// this.store
// .select(selectChangePasswordNewBackupCode)
// .subscribe(
// backupCode => (backupCode ? (this.backupCode = backupCode) : null)
// );
constructor(private store: Store<fromRoot.IState>) {
this.store
.select(selectChangePasswordNewBackupCode)
.subscribe(backupCode =>
backupCode ? (this.backupCode = backupCode) : null
);
this.form$.subscribe(
form => (this.showConfirm = form.controls.showConfirm.value)
);
......@@ -73,46 +67,9 @@ export class ChangePasswordContainer {
reset() {
this.store.dispatch(new ResetForm());
console.log("BOOM");
}
changePassword() {
this.form$.pipe(take(1)).subscribe(form => {
this.store.dispatch(new SubmitForm());
this.nonFieldErrors = [];
setTimeout(() => {
this.userService
.changePassword(
form.controls.oldPassword.value,
form.controls.newPassword.value
)
.then(() => {
this.store.dispatch(new SubmitFormSuccess(this.backupCode));
// this.store.dispatch(new LogoutAction());
// setTimeout(() => (this.hasFinished = false), 5000);
})
.catch(err => {
// console.error(err);
if (err.res && err.res.status === 400) {
if (err.res.error.old_password) {
this.nonFieldErrors = this.nonFieldErrors.concat(
"Current password is not correct."
);
this.store.dispatch(new SubmitFormFailure(["Current password is not correct"]));
} else {
this.nonFieldErrors = this.nonFieldErrors.concat(
);
this.store.dispatch(new SubmitFormFailure(["Unknown error. Password was not reset."]));
}
} else {
this.nonFieldErrors = this.nonFieldErrors.concat(
"Unknown error."
);
this.store.dispatch(new SubmitFormFailure(["Unknown error."]));
}
});
}, 200);
});
this.store.dispatch(new SubmitForm());
}
}
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