import { Component, linkEvent } from 'inferno'; import { Subscription } from 'rxjs'; import { UserOperation, LoginResponse, PasswordChangeForm, WebSocketJsonResponse, Site, } from 'lemmy-js-client'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, capitalizeFirstLetter, toast, setIsoData, isBrowser, wsSubscribe, } from '../utils'; import { i18n } from '../i18next'; import { HtmlTags } from './html-tags'; interface State { passwordChangeForm: PasswordChangeForm; loading: boolean; site: Site; } export class PasswordChange extends Component { private isoData = setIsoData(this.context); private subscription: Subscription; emptyState: State = { passwordChangeForm: { token: this.props.match.params.token, password: undefined, password_verify: undefined, }, loading: false, site: this.isoData.site.site, }; 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.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.state.loading = true; i.setState(i.state); WebSocketService.Instance.passwordChange(i.state.passwordChangeForm); } parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (msg.error) { toast(i18n.t(msg.error), 'danger'); this.state.loading = false; this.setState(this.state); return; } else if (res.op == UserOperation.PasswordChange) { let data = res.data as LoginResponse; this.state = this.emptyState; this.setState(this.state); UserService.Instance.login(data); this.props.history.push('/'); } } }