CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/account/settings/email-verification.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Map } from "immutable";
7
import { FormattedMessage, useIntl } from "react-intl";
8
9
import { alert_message } from "@cocalc/frontend/alerts";
10
import { Button } from "@cocalc/frontend/antd-bootstrap";
11
import {
12
Rendered,
13
useEffect,
14
useIsMountedRef,
15
useState,
16
} from "@cocalc/frontend/app-framework";
17
import { LabeledRow } from "@cocalc/frontend/components";
18
import { webapp_client } from "@cocalc/frontend/webapp-client";
19
20
interface Props {
21
email_address?: string;
22
email_address_verified?: Map<string, boolean>;
23
}
24
25
export function EmailVerification({
26
email_address,
27
email_address_verified,
28
}: Props) {
29
const intl = useIntl();
30
31
const is_mounted = useIsMountedRef();
32
const [disabled_button, set_disabled_button] = useState(false);
33
34
useEffect(() => {
35
set_disabled_button(false);
36
}, [email_address]);
37
38
async function verify(): Promise<void> {
39
try {
40
await webapp_client.account_client.send_verification_email();
41
} catch (err) {
42
const err_msg = `Problem sending email verification: ${err}`;
43
console.log(err_msg);
44
alert_message({ type: "error", message: err_msg });
45
} finally {
46
if (is_mounted.current) {
47
set_disabled_button(true);
48
}
49
}
50
}
51
52
function render_status(): Rendered {
53
if (email_address == null) {
54
return (
55
<span>
56
<FormattedMessage
57
id="account.settings.email-verification.unknown"
58
defaultMessage={"Unknown"}
59
/>
60
</span>
61
);
62
} else {
63
if (email_address_verified?.get(email_address)) {
64
return (
65
<span style={{ color: "green" }}>
66
<FormattedMessage
67
id="account.settings.email-verification.verified"
68
defaultMessage={"Verified"}
69
/>
70
</span>
71
);
72
} else {
73
return (
74
<>
75
<span key={1} style={{ color: "red", paddingRight: "3em" }}>
76
<FormattedMessage
77
id="account.settings.email-verification.button.label"
78
defaultMessage={"Not Verified"}
79
/>
80
</span>
81
<Button
82
onClick={verify}
83
bsStyle="success"
84
disabled={disabled_button}
85
>
86
<FormattedMessage
87
id="account.settings.email-verification.button.status"
88
defaultMessage={`{disabled_button, select, true {Email Sent} other {Send Verification Email}}`}
89
values={{ disabled_button }}
90
/>
91
</Button>
92
</>
93
);
94
}
95
}
96
}
97
98
return (
99
<LabeledRow
100
label={intl.formatMessage({
101
id: "account.settings.email-verification.label",
102
defaultMessage: "Email verification",
103
})}
104
style={{ marginBottom: "15px" }}
105
>
106
<div>
107
<FormattedMessage
108
id="account.settings.email-verification.status"
109
defaultMessage={"Status: {status}"}
110
values={{ status: render_status() }}
111
/>
112
</div>
113
</LabeledRow>
114
);
115
}
116
117