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 class="container">
71 title={this.documentTitle}
72 path={this.context.router.route.match.url}
77 <div class="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 class="form-group row">
90 <label class="col-sm-2 col-form-label" htmlFor="new-password">
91 {i18n.t("new_password")}
93 <div class="col-sm-10">
97 value={this.state.passwordChangeForm.password}
98 onInput={linkEvent(this, this.handlePasswordChange)}
105 <div class="form-group row">
106 <label class="col-sm-2 col-form-label" htmlFor="verify-password">
107 {i18n.t("verify_password")}
109 <div class="col-sm-10">
113 value={this.state.passwordChangeForm.password_verify}
114 onInput={linkEvent(this, this.handleVerifyPasswordChange)}
121 <div class="form-group row">
122 <div class="col-sm-10">
123 <button type="submit" class="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.state.loading = true;
151 WebSocketService.Instance.send(
152 wsClient.passwordChange(i.state.passwordChangeForm)
156 parseMessage(msg: any) {
157 let op = wsUserOp(msg);
160 toast(i18n.t(msg.error), "danger");
161 this.state.loading = false;
162 this.setState(this.state);
164 } else if (op == UserOperation.PasswordChange) {
165 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
166 this.state = this.emptyState;
167 this.setState(this.state);
168 UserService.Instance.login(data);
169 this.props.history.push("/");