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