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