-import { None } from "@sniptt/monads/build";
import { Component } from "inferno";
-import {
- GetSiteResponse,
- UserOperation,
- VerifyEmail as VerifyEmailForm,
- VerifyEmailResponse,
- wsJsonToRes,
- wsUserOp,
-} from "lemmy-js-client";
-import { Subscription } from "rxjs";
+import { GetSiteResponse, VerifyEmailResponse } from "lemmy-js-client";
import { i18n } from "../../i18next";
-import { WebSocketService } from "../../services";
-import {
- isBrowser,
- setIsoData,
- toast,
- wsClient,
- wsSubscribe,
-} from "../../utils";
+import { HttpService, RequestState } from "../../services/HttpService";
+import { setIsoData, toast } from "../../utils";
import { HtmlTags } from "../common/html-tags";
+import { Spinner } from "../common/icon";
interface State {
- verifyEmailForm: VerifyEmailForm;
+ verifyRes: RequestState<VerifyEmailResponse>;
siteRes: GetSiteResponse;
}
export class VerifyEmail extends Component<any, State> {
private isoData = setIsoData(this.context);
- private subscription: Subscription;
- emptyState: State = {
- verifyEmailForm: new VerifyEmailForm({
- token: this.props.match.params.token,
- }),
+ state: State = {
+ verifyRes: { state: "empty" },
siteRes: this.isoData.site_res,
};
constructor(props: any, context: any) {
super(props, context);
+ }
- this.state = this.emptyState;
+ async verify() {
+ this.setState({
+ verifyRes: { state: "loading" },
+ });
- this.parseMessage = this.parseMessage.bind(this);
- this.subscription = wsSubscribe(this.parseMessage);
- }
+ this.setState({
+ verifyRes: await HttpService.client.verifyEmail({
+ token: this.props.match.params.token,
+ }),
+ });
- componentDidMount() {
- WebSocketService.Instance.send(
- wsClient.verifyEmail(this.state.verifyEmailForm)
- );
+ if (this.state.verifyRes.state == "success") {
+ toast(i18n.t("email_verified"));
+ this.props.history.push("/login");
+ }
}
- componentWillUnmount() {
- if (isBrowser()) {
- this.subscription.unsubscribe();
- }
+ async componentDidMount() {
+ await this.verify();
}
get documentTitle(): string {
- return this.state.siteRes.site_view.match({
- some: siteView => `${i18n.t("verify_email")} - ${siteView.site.name}`,
- none: "",
- });
+ return `${i18n.t("verify_email")} - ${
+ this.state.siteRes.site_view.site.name
+ }`;
}
render() {
return (
- <div className="container">
+ <div className="container-lg">
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
- description={None}
- image={None}
/>
<div className="row">
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
<h5>{i18n.t("verify_email")}</h5>
+ {this.state.verifyRes.state == "loading" && (
+ <h5>
+ <Spinner large />
+ </h5>
+ )}
</div>
</div>
</div>
);
}
-
- parseMessage(msg: any) {
- let op = wsUserOp(msg);
- console.log(msg);
- if (msg.error) {
- toast(i18n.t(msg.error), "danger");
- this.setState(this.state);
- this.props.history.push("/");
- return;
- } else if (op == UserOperation.VerifyEmail) {
- let data = wsJsonToRes<VerifyEmailResponse>(msg, VerifyEmailResponse);
- if (data) {
- toast(i18n.t("email_verified"));
- this.setState(this.emptyState);
- this.props.history.push("/login");
- }
- }
- }
}