]> Untitled Git - lemmy-ui.git/blob - src/shared/components/person/verify-email.tsx
Adding option types 2 (#689)
[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 this.state.siteRes.site_view.match({
62       some: siteView => `${i18n.t("verify_email")} - ${siteView.site.name}`,
63       none: "",
64     });
65   }
66
67   render() {
68     return (
69       <div class="container">
70         <HtmlTags
71           title={this.documentTitle}
72           path={this.context.router.route.match.url}
73           description={None}
74           image={None}
75         />
76         <div class="row">
77           <div class="col-12 col-lg-6 offset-lg-3 mb-4">
78             <h5>{i18n.t("verify_email")}</h5>
79           </div>
80         </div>
81       </div>
82     );
83   }
84
85   parseMessage(msg: any) {
86     let op = wsUserOp(msg);
87     console.log(msg);
88     if (msg.error) {
89       toast(i18n.t(msg.error), "danger");
90       this.setState(this.state);
91       this.props.history.push("/");
92       return;
93     } else if (op == UserOperation.VerifyEmail) {
94       let data = wsJsonToRes<VerifyEmailResponse>(msg, VerifyEmailResponse);
95       if (data) {
96         toast(i18n.t("email_verified"));
97         this.state = this.emptyState;
98         this.setState(this.state);
99         this.props.history.push("/login");
100       }
101     }
102   }
103 }