lemmy-ui/src/shared/components/password_change.tsx

167 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import { Subscription } from "rxjs";
import {
UserOperation,
LoginResponse,
2020-12-24 01:58:27 +00:00
PasswordChange as PasswordChangeForm,
SiteView,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import { WebSocketService, UserService } from "../services";
2020-09-09 03:13:26 +00:00
import {
wsJsonToRes,
capitalizeFirstLetter,
toast,
setIsoData,
isBrowser,
wsSubscribe,
2020-12-24 01:58:27 +00:00
wsUserOp,
wsClient,
2021-02-22 02:39:04 +00:00
} from "../utils";
import { i18n } from "../i18next";
import { HtmlTags } from "./html-tags";
import { Spinner } from "./icon";
interface State {
passwordChangeForm: PasswordChangeForm;
loading: boolean;
2020-12-24 01:58:27 +00:00
site_view: SiteView;
}
export class PasswordChange extends Component<any, State> {
2020-09-09 03:13:26 +00:00
private isoData = setIsoData(this.context);
private subscription: Subscription;
emptyState: State = {
passwordChangeForm: {
token: this.props.match.params.token,
password: undefined,
password_verify: undefined,
},
loading: false,
2020-12-24 01:58:27 +00:00
site_view: this.isoData.site_res.site_view,
};
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
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();
}
}
get documentTitle(): string {
2021-02-22 02:39:04 +00:00
return `${i18n.t("password_change")} - ${this.state.site_view.site.name}`;
}
render() {
return (
<div class="container">
2020-09-11 18:09:21 +00:00
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
<div class="row">
<div class="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 class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="new-password">
2021-02-22 02:39:04 +00:00
{i18n.t("new_password")}
</label>
<div class="col-sm-10">
<input
id="new-password"
type="password"
value={this.state.passwordChangeForm.password}
onInput={linkEvent(this, this.handlePasswordChange)}
class="form-control"
required
maxLength={60}
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" htmlFor="verify-password">
2021-02-22 02:39:04 +00:00
{i18n.t("verify_password")}
</label>
<div class="col-sm-10">
<input
id="verify-password"
type="password"
value={this.state.passwordChangeForm.password_verify}
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
class="form-control"
required
maxLength={60}
/>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="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.passwordChangeForm.password = event.target.value;
i.setState(i.state);
}
handleVerifyPasswordChange(i: PasswordChange, event: any) {
i.state.passwordChangeForm.password_verify = event.target.value;
i.setState(i.state);
}
handlePasswordChangeSubmit(i: PasswordChange, event: any) {
event.preventDefault();
i.state.loading = true;
i.setState(i.state);
WebSocketService.Instance.send(
wsClient.passwordChange(i.state.passwordChangeForm)
);
}
2020-12-24 01:58:27 +00:00
parseMessage(msg: any) {
let 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.state.loading = false;
this.setState(this.state);
return;
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.PasswordChange) {
let data = wsJsonToRes<LoginResponse>(msg).data;
this.state = this.emptyState;
this.setState(this.state);
UserService.Instance.login(data);
2021-02-22 02:39:04 +00:00
this.props.history.push("/");
}
}
}