import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse, PasswordChangeAfterReset, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { i18n } from "../../i18next"; import { UserService, WebSocketService } from "../../services"; import { capitalizeFirstLetter, isBrowser, setIsoData, toast, wsClient, 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 { 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); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); } componentWillUnmount() { if (isBrowser()) { this.subscription?.unsubscribe(); } } get documentTitle(): string { return `${i18n.t("password_change")} - ${ this.state.siteRes.site_view.site.name }`; } render() { return (
{i18n.t("password_change")}
{this.passwordChangeForm()}
); } passwordChangeForm() { return (
); } 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 }); let password = i.state.form.password; let password_verify = i.state.form.password_verify; if (password && password_verify) { let form: PasswordChangeAfterReset = { token: i.state.form.token, password, password_verify, }; WebSocketService.Instance.send(wsClient.passwordChange(form)); } } parseMessage(msg: any) { let op = wsUserOp(msg); console.log(msg); if (msg.error) { toast(i18n.t(msg.error), "danger"); this.setState({ loading: false }); return; } else if (op == UserOperation.PasswordChangeAfterReset) { let data = wsJsonToRes(msg); UserService.Instance.login(data); this.props.history.push("/"); location.reload(); } } }