1 import { None } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
6 PasswordChange as PasswordChangeForm,
10 } from "lemmy-js-client";
11 import { Subscription } from "rxjs";
12 import { i18n } from "../../i18next";
13 import { UserService, WebSocketService } from "../../services";
15 capitalizeFirstLetter,
22 import { HtmlTags } from "../common/html-tags";
23 import { Spinner } from "../common/icon";
26 passwordChangeForm: PasswordChangeForm;
28 siteRes: GetSiteResponse;
31 export class PasswordChange extends Component<any, State> {
32 private isoData = setIsoData(this.context);
33 private subscription: Subscription;
36 passwordChangeForm: new PasswordChangeForm({
37 token: this.props.match.params.token,
39 password_verify: undefined,
42 siteRes: this.isoData.site_res,
45 constructor(props: any, context: any) {
46 super(props, context);
48 this.state = this.emptyState;
50 this.parseMessage = this.parseMessage.bind(this);
51 this.subscription = wsSubscribe(this.parseMessage);
54 componentWillUnmount() {
56 this.subscription.unsubscribe();
60 get documentTitle(): string {
61 return `${i18n.t("password_change")} - ${
62 this.state.siteRes.site_view.site.name
68 <div className="container-lg">
70 title={this.documentTitle}
71 path={this.context.router.route.match.url}
76 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
77 <h5>{i18n.t("password_change")}</h5>
78 {this.passwordChangeForm()}
85 passwordChangeForm() {
87 <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
88 <div className="form-group row">
89 <label className="col-sm-2 col-form-label" htmlFor="new-password">
90 {i18n.t("new_password")}
92 <div className="col-sm-10">
96 value={this.state.passwordChangeForm.password}
97 onInput={linkEvent(this, this.handlePasswordChange)}
98 className="form-control"
104 <div className="form-group row">
105 <label className="col-sm-2 col-form-label" htmlFor="verify-password">
106 {i18n.t("verify_password")}
108 <div className="col-sm-10">
112 value={this.state.passwordChangeForm.password_verify}
113 onInput={linkEvent(this, this.handleVerifyPasswordChange)}
114 className="form-control"
120 <div className="form-group row">
121 <div className="col-sm-10">
122 <button type="submit" className="btn btn-secondary">
123 {this.state.loading ? (
126 capitalizeFirstLetter(i18n.t("save"))
135 handlePasswordChange(i: PasswordChange, event: any) {
136 i.state.passwordChangeForm.password = event.target.value;
140 handleVerifyPasswordChange(i: PasswordChange, event: any) {
141 i.state.passwordChangeForm.password_verify = event.target.value;
145 handlePasswordChangeSubmit(i: PasswordChange, event: any) {
146 event.preventDefault();
147 i.setState({ loading: true });
149 WebSocketService.Instance.send(
150 wsClient.passwordChange(i.state.passwordChangeForm)
154 parseMessage(msg: any) {
155 let op = wsUserOp(msg);
158 toast(i18n.t(msg.error), "danger");
159 this.setState({ loading: false });
161 } else if (op == UserOperation.PasswordChange) {
162 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
163 this.setState(this.emptyState);
164 UserService.Instance.login(data);
165 this.props.history.push("/");