]> Untitled Git - lemmy-ui.git/blob - src/shared/components/person/verify-email.tsx
Removing monads. Fixes #884 (#886)
[lemmy-ui.git] / src / shared / components / person / verify-email.tsx
1 import { Component } from "inferno";
2 import {
3   GetSiteResponse,
4   UserOperation,
5   VerifyEmail as VerifyEmailForm,
6   VerifyEmailResponse,
7   wsJsonToRes,
8   wsUserOp,
9 } from "lemmy-js-client";
10 import { Subscription } from "rxjs";
11 import { i18n } from "../../i18next";
12 import { WebSocketService } from "../../services";
13 import {
14   isBrowser,
15   setIsoData,
16   toast,
17   wsClient,
18   wsSubscribe,
19 } from "../../utils";
20 import { HtmlTags } from "../common/html-tags";
21
22 interface State {
23   verifyEmailForm: VerifyEmailForm;
24   siteRes: GetSiteResponse;
25 }
26
27 export class VerifyEmail extends Component<any, State> {
28   private isoData = setIsoData(this.context);
29   private subscription?: Subscription;
30
31   state: State = {
32     verifyEmailForm: {
33       token: this.props.match.params.token,
34     },
35     siteRes: this.isoData.site_res,
36   };
37
38   constructor(props: any, context: any) {
39     super(props, context);
40
41     this.parseMessage = this.parseMessage.bind(this);
42     this.subscription = wsSubscribe(this.parseMessage);
43   }
44
45   componentDidMount() {
46     WebSocketService.Instance.send(
47       wsClient.verifyEmail(this.state.verifyEmailForm)
48     );
49   }
50
51   componentWillUnmount() {
52     if (isBrowser()) {
53       this.subscription?.unsubscribe();
54     }
55   }
56
57   get documentTitle(): string {
58     return `${i18n.t("verify_email")} - ${
59       this.state.siteRes.site_view.site.name
60     }`;
61   }
62
63   render() {
64     return (
65       <div className="container-lg">
66         <HtmlTags
67           title={this.documentTitle}
68           path={this.context.router.route.match.url}
69         />
70         <div className="row">
71           <div className="col-12 col-lg-6 offset-lg-3 mb-4">
72             <h5>{i18n.t("verify_email")}</h5>
73           </div>
74         </div>
75       </div>
76     );
77   }
78
79   parseMessage(msg: any) {
80     let op = wsUserOp(msg);
81     console.log(msg);
82     if (msg.error) {
83       toast(i18n.t(msg.error), "danger");
84       this.setState(this.state);
85       this.props.history.push("/");
86       return;
87     } else if (op == UserOperation.VerifyEmail) {
88       let data = wsJsonToRes<VerifyEmailResponse>(msg);
89       if (data) {
90         toast(i18n.t("email_verified"));
91         this.props.history.push("/login");
92       }
93     }
94   }
95 }