import { None } from "@sniptt/monads"; import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse, PasswordChange as PasswordChangeForm, 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 { passwordChangeForm: PasswordChangeForm; loading: boolean; siteRes: GetSiteResponse; } export class PasswordChange extends Component { private isoData = setIsoData(this.context); private subscription: Subscription; emptyState: State = { passwordChangeForm: new PasswordChangeForm({ token: this.props.match.params.token, password: undefined, password_verify: undefined, }), loading: false, siteRes: this.isoData.site_res, }; constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; 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.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.setState({ loading: true }); WebSocketService.Instance.send( wsClient.passwordChange(i.state.passwordChangeForm) ); } 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.PasswordChange) { let data = wsJsonToRes(msg, LoginResponse); this.setState(this.emptyState); UserService.Instance.login(data); this.props.history.push("/"); location.reload(); } } }