2017-06-29 12:54:02 -06:00
|
|
|
import * as React from "react";
|
2017-08-10 17:54:52 -06:00
|
|
|
import {
|
|
|
|
Widget,
|
|
|
|
WidgetHeader,
|
2017-08-15 11:35:25 -06:00
|
|
|
WidgetBody,
|
2020-02-28 09:35:32 -07:00
|
|
|
SaveBtn,
|
2018-01-20 07:46:44 -07:00
|
|
|
} from "../../ui/index";
|
2018-08-01 07:42:56 -06:00
|
|
|
import { SpecialStatus } from "farmbot";
|
2017-08-17 12:36:33 -06:00
|
|
|
import Axios from "axios";
|
|
|
|
import { API } from "../../api/index";
|
2018-04-05 16:41:33 -06:00
|
|
|
import { prettyPrintApiErrors, equals, trim } from "../../util";
|
|
|
|
import { Content } from "../../constants";
|
|
|
|
import { uniq } from "lodash";
|
2018-07-02 13:03:37 -06:00
|
|
|
import { BlurablePassword } from "../../ui/blurable_password";
|
2019-04-02 13:59:37 -06:00
|
|
|
import { t } from "../../i18next_wrapper";
|
2019-06-24 15:39:49 -06:00
|
|
|
import { success, error } from "../../toast/toast";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2017-08-17 12:36:33 -06:00
|
|
|
interface PasswordForm {
|
|
|
|
new_password: string;
|
|
|
|
new_password_confirmation: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:39:12 -06:00
|
|
|
export interface ChangePWState { status: SpecialStatus; form: PasswordForm }
|
2018-03-02 08:04:03 -07:00
|
|
|
|
|
|
|
const EMPTY_FORM =
|
|
|
|
({ new_password: "", new_password_confirmation: "", password: "" });
|
2017-08-17 12:36:33 -06:00
|
|
|
|
|
|
|
export class ChangePassword extends React.Component<{}, ChangePWState> {
|
2018-03-02 08:04:03 -07:00
|
|
|
state: ChangePWState = { status: SpecialStatus.SAVED, form: EMPTY_FORM };
|
2017-08-17 12:36:33 -06:00
|
|
|
|
2018-04-05 16:41:33 -06:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.clearForm();
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:36:33 -06:00
|
|
|
/** Set the `status` flag to `undefined`, but only if the form is empty.
|
|
|
|
* Useful when the user manually clears the form. */
|
2018-03-02 07:52:06 -07:00
|
|
|
maybeClearForm =
|
|
|
|
() => equals(EMPTY_FORM, this.state.form) ? this.clearForm() : false;
|
2017-08-17 12:36:33 -06:00
|
|
|
|
2017-11-06 16:54:52 -07:00
|
|
|
clearForm = () => this.setState({ status: SpecialStatus.SAVED, form: EMPTY_FORM });
|
2017-08-11 11:53:34 -06:00
|
|
|
|
2017-08-17 12:36:33 -06:00
|
|
|
set = (key: keyof PasswordForm) =>
|
|
|
|
(e: React.SyntheticEvent<HTMLInputElement>) => {
|
2017-08-28 05:49:13 -06:00
|
|
|
const wow = {
|
2017-08-17 12:36:33 -06:00
|
|
|
status: SpecialStatus.DIRTY,
|
|
|
|
form: { ...this.state.form, [key]: e.currentTarget.value }
|
|
|
|
};
|
|
|
|
this.setState(wow, this.maybeClearForm);
|
|
|
|
};
|
|
|
|
|
2018-04-05 16:41:33 -06:00
|
|
|
sendChange = () =>
|
2017-08-17 12:36:33 -06:00
|
|
|
Axios
|
|
|
|
.patch(API.current.usersPath, this.state.form)
|
2018-03-13 16:37:24 -06:00
|
|
|
.then(() => {
|
2019-07-02 11:03:16 -06:00
|
|
|
success(t("Your password is changed."));
|
2017-08-17 12:36:33 -06:00
|
|
|
this.clearForm();
|
|
|
|
}, (e) => {
|
2019-07-02 11:03:16 -06:00
|
|
|
error(e ? prettyPrintApiErrors(e) : t("Password change failed."));
|
2017-08-17 12:36:33 -06:00
|
|
|
this.clearForm();
|
|
|
|
});
|
2018-04-05 16:41:33 -06:00
|
|
|
|
|
|
|
save = () => {
|
|
|
|
const numUniqueValues = uniq(Object.values(this.state.form)).length;
|
|
|
|
switch (numUniqueValues) {
|
|
|
|
case 1:
|
2019-07-02 11:03:16 -06:00
|
|
|
error(t("Provided new and old passwords match. Password not changed."));
|
2018-04-05 16:41:33 -06:00
|
|
|
this.clearForm();
|
|
|
|
break;
|
|
|
|
case 2:
|
2018-05-12 09:01:29 -06:00
|
|
|
if (confirm(t(Content.ACCOUNT_PASSWORD_CHANGE))) {
|
2018-04-05 16:41:33 -06:00
|
|
|
this.setState({ status: SpecialStatus.SAVING });
|
|
|
|
this.sendChange();
|
|
|
|
}
|
|
|
|
this.clearForm();
|
|
|
|
break;
|
|
|
|
case 3:
|
2019-07-02 11:03:16 -06:00
|
|
|
error(t("New password and confirmation do not match."));
|
2018-04-05 16:41:33 -06:00
|
|
|
this.clearForm();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.clearForm();
|
|
|
|
throw new Error(trim(`Password change form error:
|
|
|
|
${numUniqueValues} unique values provided.`));
|
|
|
|
}
|
2017-08-17 12:36:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <Widget>
|
|
|
|
<WidgetHeader title={t("Change Password")}>
|
|
|
|
<SaveBtn onClick={this.save} status={this.state.status} />
|
|
|
|
</WidgetHeader>
|
|
|
|
<WidgetBody>
|
|
|
|
<form>
|
|
|
|
<label>
|
|
|
|
{t("Old Password")}
|
|
|
|
</label>
|
2018-07-02 13:03:37 -06:00
|
|
|
<BlurablePassword
|
2017-08-17 12:36:33 -06:00
|
|
|
onCommit={this.set("password")}
|
2018-07-02 13:03:37 -06:00
|
|
|
name="password" />
|
2017-08-17 12:36:33 -06:00
|
|
|
<label>
|
|
|
|
{t("New Password")}
|
|
|
|
</label>
|
2018-07-02 13:03:37 -06:00
|
|
|
<BlurablePassword
|
2017-08-17 12:36:33 -06:00
|
|
|
onCommit={this.set("new_password")}
|
2018-07-02 13:03:37 -06:00
|
|
|
name="new_password" />
|
2017-08-17 12:36:33 -06:00
|
|
|
<label>
|
2018-04-05 16:41:33 -06:00
|
|
|
{t("Confirm New Password")}
|
2017-08-17 12:36:33 -06:00
|
|
|
</label>
|
2018-07-02 13:03:37 -06:00
|
|
|
<BlurablePassword
|
2017-08-17 12:36:33 -06:00
|
|
|
onCommit={this.set("new_password_confirmation")}
|
2018-07-02 13:03:37 -06:00
|
|
|
name={"new_password_confirmation"} />
|
2017-08-17 12:36:33 -06:00
|
|
|
</form>
|
|
|
|
</WidgetBody>
|
|
|
|
</Widget>;
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
2018-05-12 09:09:40 -06:00
|
|
|
}
|