-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 { GetSiteResponse, LoginResponse } from "lemmy-js-client";
import { i18n } from "../../i18next";
-import { UserService, WebSocketService } from "../../services";
-import {
- capitalizeFirstLetter,
- isBrowser,
- setIsoData,
- toast,
- wsClient,
- wsSubscribe,
-} from "../../utils";
+import { HttpService, UserService } from "../../services";
+import { RequestState } from "../../services/HttpService";
+import { capitalizeFirstLetter, myAuth, setIsoData } from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
interface State {
- passwordChangeForm: PasswordChangeForm;
- loading: boolean;
+ passwordChangeRes: RequestState<LoginResponse>;
+ form: {
+ token: string;
+ password?: string;
+ password_verify?: string;
+ };
siteRes: GetSiteResponse;
}
export class PasswordChange extends Component<any, State> {
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,
+ state: State = {
+ passwordChangeRes: { state: "empty" },
siteRes: this.isoData.site_res,
+ form: {
+ token: this.props.match.params.token,
+ },
};
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 this.state.siteRes.site_view.match({
- some: siteView => `${i18n.t("password_change")} - ${siteView.site.name}`,
- none: "",
- });
+ return `${i18n.t("password_change")} - ${
+ this.state.siteRes.site_view.site.name
+ }`;
}
render() {
return (
- <div class="container">
+ <div className="password-change container-lg">
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
- description={None}
- image={None}
/>
- <div class="row">
- <div class="col-12 col-lg-6 offset-lg-3 mb-4">
+ <div className="row">
+ <div className="col-12 col-lg-6 offset-lg-3 mb-4">
<h5>{i18n.t("password_change")}</h5>
{this.passwordChangeForm()}
</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">
+ <div className="mb-3 row">
+ <label className="col-sm-2 col-form-label" htmlFor="new-password">
{i18n.t("new_password")}
</label>
- <div class="col-sm-10">
+ <div className="col-sm-10">
<input
id="new-password"
type="password"
- value={this.state.passwordChangeForm.password}
+ value={this.state.form.password}
onInput={linkEvent(this, this.handlePasswordChange)}
- class="form-control"
+ className="form-control"
required
maxLength={60}
/>
</div>
</div>
- <div class="form-group row">
- <label class="col-sm-2 col-form-label" htmlFor="verify-password">
+ <div className="mb-3 row">
+ <label className="col-sm-2 col-form-label" htmlFor="verify-password">
{i18n.t("verify_password")}
</label>
- <div class="col-sm-10">
+ <div className="col-sm-10">
<input
id="verify-password"
type="password"
- value={this.state.passwordChangeForm.password_verify}
+ value={this.state.form.password_verify}
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
- class="form-control"
+ className="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 ? (
+ <div className="mb-3 row">
+ <div className="col-sm-10">
+ <button type="submit" className="btn btn-secondary">
+ {this.state.passwordChangeRes.state == "loading" ? (
<Spinner />
) : (
capitalizeFirstLetter(i18n.t("save"))
}
handlePasswordChange(i: PasswordChange, event: any) {
- i.state.passwordChangeForm.password = event.target.value;
+ i.state.form.password = event.target.value;
i.setState(i.state);
}
handleVerifyPasswordChange(i: PasswordChange, event: any) {
- i.state.passwordChangeForm.password_verify = event.target.value;
+ i.state.form.password_verify = event.target.value;
i.setState(i.state);
}
- handlePasswordChangeSubmit(i: PasswordChange, event: any) {
+ async handlePasswordChangeSubmit(i: PasswordChange, event: any) {
event.preventDefault();
- i.state.loading = true;
- i.setState(i.state);
+ i.setState({ passwordChangeRes: { state: "loading" } });
- WebSocketService.Instance.send(
- wsClient.passwordChange(i.state.passwordChangeForm)
- );
- }
+ const password = i.state.form.password;
+ const password_verify = i.state.form.password_verify;
+
+ if (password && password_verify) {
+ i.setState({
+ passwordChangeRes: await HttpService.client.passwordChangeAfterReset({
+ token: i.state.form.token,
+ password,
+ password_verify,
+ }),
+ });
+
+ if (i.state.passwordChangeRes.state === "success") {
+ const data = i.state.passwordChangeRes.data;
+ UserService.Instance.login(data);
+
+ const site = await HttpService.client.getSite({ auth: myAuth() });
+ if (site.state === "success") {
+ UserService.Instance.myUserInfo = site.data.my_user;
+ }
- parseMessage(msg: any) {
- let op = wsUserOp(msg);
- console.log(msg);
- if (msg.error) {
- toast(i18n.t(msg.error), "danger");
- this.state.loading = false;
- this.setState(this.state);
- return;
- } else if (op == UserOperation.PasswordChange) {
- let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
- this.state = this.emptyState;
- this.setState(this.state);
- UserService.Instance.login(data);
- this.props.history.push("/");
+ i.props.history.replace("/");
+ }
}
}
}