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/next/components/account/config/anonymous/upgrade.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert, Button, Input, Space } from "antd";
7
import { delay } from "awaiting";
8
import { CSSProperties, useState } from "react";
9
10
import { Icon } from "@cocalc/frontend/components/icon";
11
import { is_valid_email_address as isValidEmailAddress } from "@cocalc/util/misc";
12
import { TermsCheckbox } from "components/auth/sign-up";
13
import SSO from "components/auth/sso";
14
import { Title } from "components/misc";
15
import apiPost from "lib/api/post";
16
import { useRouter } from "next/router";
17
18
interface Props {
19
style: CSSProperties;
20
}
21
22
export default function Upgrade({ style }: Props) {
23
const [terms, setTerms] = useState<boolean>(false);
24
return (
25
<div style={style}>
26
<TermsCheckbox onChange={setTerms} checked={terms} />
27
<br />
28
<br />
29
{terms && <EmailPassword />}
30
<br />
31
<br />
32
{terms && (
33
<SSO
34
style={{ margin: "30px 0" }}
35
header={<Title level={3}>Or Use Single Sign On</Title>}
36
/>
37
)}
38
</div>
39
);
40
}
41
42
function EmailPassword() {
43
const router = useRouter();
44
const [success, setSuccess] = useState<boolean>(false);
45
const [error, setError] = useState<string>("");
46
const [email_address, setEmailAddress] = useState<string>("");
47
const [password, setPassword] = useState<string>("");
48
async function setEmailAndPassword() {
49
setError("");
50
try {
51
await apiPost("/accounts/set-email-address", {
52
email_address,
53
password,
54
});
55
setSuccess(true);
56
// send them to configure their name, which is a good next step...
57
router.push("/config/account/name");
58
await delay(1000);
59
// need to force reload to know that they are no longer anonymous.
60
router.reload();
61
} catch (err) {
62
setError(err.message);
63
}
64
}
65
return (
66
<>
67
<Title level={3}>Set an Email Address and Password</Title>
68
{error && (
69
<Alert
70
type="error"
71
showIcon
72
message={error}
73
style={{ marginTop: "15px" }}
74
/>
75
)}
76
<Space style={{ width: "100%" }}>
77
<Input
78
disabled={success}
79
style={{ fontSize: "12pt" }}
80
placeholder="Email address"
81
autoComplete="username"
82
value={email_address}
83
onChange={(e) => {
84
setEmailAddress(e.target.value);
85
setError("");
86
}}
87
/>
88
<Input.Password
89
disabled={success}
90
style={{ fontSize: "12pt" }}
91
value={password}
92
placeholder="Password"
93
autoComplete="new-password"
94
onChange={(e) => {
95
setPassword(e.target.value);
96
setError("");
97
}}
98
onPressEnter={setEmailAndPassword}
99
/>
100
{/* change height of button to match input boxes */}
101
<Button
102
type="primary"
103
disabled={success || !email_address || password.length < 6}
104
style={{ height: "35px" }}
105
onClick={setEmailAndPassword}
106
>
107
{success ? (
108
<>
109
<Icon name="check" style={{ marginRight: "5px" }} /> Saved
110
</>
111
) : email_address.length > 0 &&
112
!isValidEmailAddress(email_address) ? (
113
"Enter valid email"
114
) : password.length > 0 && password.length < 6 ? (
115
"At least 6 characters"
116
) : (
117
<>
118
<Icon name="check" style={{ marginRight: "5px" }} /> Save
119
</>
120
)}
121
</Button>
122
</Space>
123
</>
124
);
125
}
126
127