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