lemmy-ui/src/shared/components/person/password-change.tsx

175 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import {
GetSiteResponse,
LoginResponse,
PasswordChangeAfterReset,
UserOperation,
wsJsonToRes,
wsUserOp,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
2020-09-09 03:13:26 +00:00
import {
capitalizeFirstLetter,
isBrowser,
setIsoData,
toast,
wsClient,
2020-09-09 03:13:26 +00:00
wsSubscribe,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
interface State {
form: {
token: string;
password?: string;
password_verify?: string;
};
loading: boolean;
siteRes: GetSiteResponse;
}
export class PasswordChange extends Component<any, State> {
2020-09-09 03:13:26 +00:00
private isoData = setIsoData(this.context);
private subscription?: Subscription;
state: State = {
form: {
token: this.props.match.params.token,
},
loading: false,
siteRes: this.isoData.site_res,
};
constructor(props: any, context: any) {
super(props, context);
2020-09-09 03:13:26 +00:00
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
}
componentWillUnmount() {
2020-09-09 03:13:26 +00:00
if (isBrowser()) {
this.subscription?.unsubscribe();
2020-09-09 03:13:26 +00:00
}
}
get documentTitle(): string {
2022-11-09 19:53:07 +00:00
return `${i18n.t("password_change")} - ${
this.state.siteRes.site_view.site.name
}`;
}
render() {
return (
<div className="container-lg">
2020-09-11 18:09:21 +00:00
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
<div className="row">
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
2021-02-22 02:39:04 +00:00
<h5>{i18n.t("password_change")}</h5>
{this.passwordChangeForm()}
</div>
</div>
</div>
);
}
passwordChangeForm() {
return (
<form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
<div className="form-group row">
<label className="col-sm-2 col-form-label" htmlFor="new-password">
2021-02-22 02:39:04 +00:00
{i18n.t("new_password")}
</label>
<div className="col-sm-10">
<input
id="new-password"
type="password"
value={this.state.form.password}
onInput={linkEvent(this, this.handlePasswordChange)}
className="form-control"
required
maxLength={60}
/>
</div>
</div>
<div className="form-group row">
<label className="col-sm-2 col-form-label" htmlFor="verify-password">
2021-02-22 02:39:04 +00:00
{i18n.t("verify_password")}
</label>
<div className="col-sm-10">
<input
id="verify-password"
type="password"
value={this.state.form.password_verify}
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
className="form-control"
required
maxLength={60}
/>
</div>
</div>
<div className="form-group row">
<div className="col-sm-10">
<button type="submit" className="btn btn-secondary">
{this.state.loading ? (
2021-02-11 20:35:27 +00:00
<Spinner />
) : (
2021-02-22 02:39:04 +00:00
capitalizeFirstLetter(i18n.t("save"))
)}
</button>
</div>
</div>
</form>
);
}
handlePasswordChange(i: PasswordChange, event: any) {
i.state.form.password = event.target.value;
i.setState(i.state);
}
handleVerifyPasswordChange(i: PasswordChange, event: any) {
i.state.form.password_verify = event.target.value;
i.setState(i.state);
}
handlePasswordChangeSubmit(i: PasswordChange, event: any) {
event.preventDefault();
i.setState({ loading: true });
2023-06-05 21:31:12 +00:00
const password = i.state.form.password;
const password_verify = i.state.form.password_verify;
if (password && password_verify) {
2023-06-05 21:31:12 +00:00
const form: PasswordChangeAfterReset = {
token: i.state.form.token,
password,
password_verify,
};
WebSocketService.Instance.send(wsClient.passwordChange(form));
}
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
2023-06-05 21:31:12 +00:00
const op = wsUserOp(msg);
2021-04-07 15:54:38 +00:00
console.log(msg);
if (msg.error) {
2021-02-22 02:39:04 +00:00
toast(i18n.t(msg.error), "danger");
this.setState({ loading: false });
return;
} else if (op == UserOperation.PasswordChangeAfterReset) {
2023-06-05 21:31:12 +00:00
const data = wsJsonToRes<LoginResponse>(msg);
UserService.Instance.login(data);
2021-02-22 02:39:04 +00:00
this.props.history.push("/");
2022-11-09 19:53:07 +00:00
location.reload();
}
}
}