2022-06-21 21:42:29 +00:00
|
|
|
import { None } from "@sniptt/monads";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
LoginResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
PasswordChange as PasswordChangeForm,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-09 03:13:26 +00:00
|
|
|
import {
|
|
|
|
capitalizeFirstLetter,
|
|
|
|
isBrowser,
|
2021-07-17 20:42:55 +00:00
|
|
|
setIsoData,
|
|
|
|
toast,
|
|
|
|
wsClient,
|
2020-09-09 03:13:26 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
passwordChangeForm: PasswordChangeForm;
|
|
|
|
loading: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PasswordChange extends Component<any, State> {
|
2020-09-09 03:13:26 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
emptyState: State = {
|
2022-06-21 21:42:29 +00:00
|
|
|
passwordChangeForm: new PasswordChangeForm({
|
2020-09-06 16:15:25 +00:00
|
|
|
token: this.props.match.params.token,
|
|
|
|
password: undefined,
|
|
|
|
password_verify: undefined,
|
2022-06-21 21:42:29 +00:00
|
|
|
}),
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-09 03:13:26 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.siteRes.site_view.match({
|
|
|
|
some: siteView => `${i18n.t("password_change")} - ${siteView.site.name}`,
|
|
|
|
none: "",
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
2022-06-21 21:42:29 +00:00
|
|
|
description={None}
|
|
|
|
image={None}
|
2020-09-11 18:09:21 +00:00
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("password_change")}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.passwordChangeForm()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordChangeForm() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="new-password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("new_password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2021-02-06 20:20:41 +00:00
|
|
|
id="new-password"
|
2020-09-06 16:15:25 +00:00
|
|
|
type="password"
|
|
|
|
value={this.state.passwordChangeForm.password}
|
|
|
|
onInput={linkEvent(this, this.handlePasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="verify-password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("verify_password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2021-02-06 20:20:41 +00:00
|
|
|
id="verify-password"
|
2020-09-06 16:15:25 +00:00
|
|
|
type="password"
|
|
|
|
value={this.state.passwordChangeForm.password_verify}
|
|
|
|
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<button type="submit" className="btn btn-secondary">
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</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();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.passwordChange(i.state.passwordChangeForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ loading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.PasswordChange) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(this.emptyState);
|
2020-09-06 16:15:25 +00:00
|
|
|
UserService.Instance.login(data);
|
2021-02-22 02:39:04 +00:00
|
|
|
this.props.history.push("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|