Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/account/settings/email-address-setting.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { FormattedMessage, useIntl } from "react-intl";6import { alert_message } from "@cocalc/frontend/alerts";7import { Button, Card, Input, Space } from "antd";8import { useState } from "react";9import { ErrorDisplay, LabeledRow, Saving } from "@cocalc/frontend/components";10import { labels } from "@cocalc/frontend/i18n";11import { log } from "@cocalc/frontend/user-tracking";12import { webapp_client } from "@cocalc/frontend/webapp-client";13import { COLORS } from "@cocalc/util/theme";1415interface Props {16email_address?: string;17disabled?: boolean;18is_anonymous?: boolean;19verify_emails?: boolean;20}2122export const EmailAddressSetting = ({23email_address: email_address0,24disabled,25is_anonymous,26verify_emails,27}: Props) => {28const intl = useIntl();29const [state, setState] = useState<"view" | "edit" | "saving">("view");30const [password, setPassword] = useState<string>("");31const [email_address, set_email_address] = useState<string>(32email_address0 ?? "",33);34const [error, setError] = useState<string>("");3536function start_editing() {37setState("edit");38set_email_address(email_address0 ?? "");39setError("");40setPassword("");41}4243function cancel_editing() {44setState("view");45setPassword("");46}4748async function save_editing(): Promise<void> {49if (password.length < 6) {50setState("edit");51setError("Password must be at least 6 characters long.");52return;53}54setState("saving");55try {56await webapp_client.account_client.change_email(email_address, password);57} catch (error) {58setState("edit");59setError(`Error -- ${error}`);60return;61}62if (is_anonymous) {63log("email_sign_up", { source: "anonymous_account" });64}65setState("view");66setError("");67setPassword("");68// if email verification is enabled, send out a token69// in any case, send a welcome email to an anonymous user, possibly70// including an email verification link71if (!(verify_emails || is_anonymous)) {72return;73}74try {75// anonymouse users will get the "welcome" email76await webapp_client.account_client.send_verification_email(!is_anonymous);77} catch (error) {78const err_msg = `Problem sending welcome email: ${error}`;79console.log(err_msg);80alert_message({ type: "error", message: err_msg });81}82}8384function is_submittable(): boolean {85return !!(password !== "" && email_address !== email_address0);86}8788function render_error() {89if (error) {90return (91<ErrorDisplay92error={error}93onClose={() => setError("")}94style={{ marginTop: "15px" }}95/>96);97}98}99100function render_edit() {101const password_label = intl.formatMessage(102{103id: "account.settings.email_address.password_label",104defaultMessage:105"{have_email, select, true {Current password} other {Choose a password}}",106},107{108have_email: !!email_address,109},110);111return (112<Card style={{ marginTop: "3ex" }}>113<div style={{ marginBottom: "15px" }}>114<FormattedMessage115id="account.settings.email_address.new_email_address_label"116defaultMessage="New email address"117/>118<Input119autoFocus120placeholder="[email protected]"121onChange={(e) => {122set_email_address(e.target.value);123}}124maxLength={254}125/>126</div>127{password_label}128<Input.Password129value={password}130placeholder={password_label}131onChange={(e) => {132const pw = e.target.value;133if (pw != null) {134setPassword(pw);135}136}}137onPressEnter={() => {138if (is_submittable()) {139return save_editing();140}141}}142/>143<Space style={{ marginTop: "15px" }}>144<Button onClick={cancel_editing}>Cancel</Button>145<Button146disabled={!is_submittable()}147onClick={save_editing}148type="primary"149>150{button_label()}151</Button>152</Space>153{render_error()}154{render_saving()}155</Card>156);157}158159function render_saving() {160if (state === "saving") {161return <Saving />;162}163}164165function button_label(): string {166return intl.formatMessage(167{168id: "account.settings.email_address.button_label",169defaultMessage: `{type, select,170anonymous {Sign up using an email address and password}171have_email {Change email address}172other {Set email address and password}}`,173},174{175type: is_anonymous ? "anonymous" : email_address ? "have_email" : "",176},177);178}179180const label = is_anonymous ? (181<h5 style={{ color: COLORS.GRAY_M }}>182Sign up using an email address and password183</h5>184) : (185intl.formatMessage(labels.email_address)186);187188return (189<LabeledRow190label={label}191style={disabled ? { color: COLORS.GRAY_M } : undefined}192>193<div>194{email_address}195{state === "view" ? (196<Button197disabled={disabled}198className="pull-right"199onClick={start_editing}200>201{button_label()}...202</Button>203) : undefined}204</div>205{state !== "view" ? render_edit() : undefined}206</LabeledRow>207);208};209210211