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 this.state.siteRes.site_view.match({
62 some: siteView => `${i18n.t("password_change")} - ${siteView.site.name}`,
69 <div className="container">
71 title={this.documentTitle}
72 path={this.context.router.route.match.url}
77 <div className="col-12 col-lg-6 offset-lg-3 mb-4">
78 <h5>{i18n.t("password_change")}</h5>
79 {this.passwordChangeForm()}
86 passwordChangeForm() {
88 <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
89 <div className="form-group row">
90 <label className="col-sm-2 col-form-label" htmlFor="new-password">
91 {i18n.t("new_password")}
93 <div className="col-sm-10">
97 value={this.state.passwordChangeForm.password}
98 onInput={linkEvent(this, this.handlePasswordChange)}
99 className="form-control"
105 <div className="form-group row">
106 <label className="col-sm-2 col-form-label" htmlFor="verify-password">
107 {i18n.t("verify_password")}
109 <div className="col-sm-10">
113 value={this.state.passwordChangeForm.password_verify}
114 onInput={linkEvent(this, this.handleVerifyPasswordChange)}
115 className="form-control"
121 <div className="form-group row">
122 <div className="col-sm-10">
123 <button type="submit" className="btn btn-secondary">
124 {this.state.loading ? (
127 capitalizeFirstLetter(i18n.t("save"))
136 handlePasswordChange(i: PasswordChange, event: any) {
137 i.state.passwordChangeForm.password = event.target.value;
141 handleVerifyPasswordChange(i: PasswordChange, event: any) {
142 i.state.passwordChangeForm.password_verify = event.target.value;
146 handlePasswordChangeSubmit(i: PasswordChange, event: any) {
147 event.preventDefault();
148 i.setState({ loading: true });
150 WebSocketService.Instance.send(
151 wsClient.passwordChange(i.state.passwordChangeForm)
155 parseMessage(msg: any) {
156 let op = wsUserOp(msg);
159 toast(i18n.t(msg.error), "danger");
160 this.setState({ loading: false });
162 } else if (op == UserOperation.PasswordChange) {
163 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
164 this.setState(this.emptyState);
165 UserService.Instance.login(data);
166 this.props.history.push("/");