"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const antd_1 = require("antd");
const react_1 = require("react");
const logo_square_1 = __importDefault(require("components/logo-square"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
const A_1 = __importDefault(require("components/misc/A"));
const shared_1 = require("./shared");
const post_1 = __importDefault(require("lib/api/post"));
const icon_1 = require("@cocalc/frontend/components/icon");
const contact_1 = __importDefault(require("components/landing/contact"));
const misc_1 = require("@cocalc/util/misc");
function PasswordReset() {
    const { siteName } = (0, use_customize_1.default)();
    const [email, setEmail] = (0, react_1.useState)("");
    const [resetting, setResetting] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)("");
    const [success, setSuccess] = (0, react_1.useState)("");
    async function resetPassword() {
        if (resetting)
            return;
        try {
            setError("");
            setSuccess("");
            setResetting(true);
            const result = await (0, post_1.default)("/auth/password-reset", { email });
            if (result.success) {
                setEmail("");
                setSuccess(result.success);
            }
        }
        catch (err) {
            setError(`${err}`);
        }
        finally {
            setResetting(false);
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", { style: { padding: "0 15px" }, children: [(0, jsx_runtime_1.jsxs)("div", { style: { textAlign: "center", marginBottom: "15px" }, children: [(0, jsx_runtime_1.jsx)(logo_square_1.default, { style: { width: "100px", height: "100px" } }), (0, jsx_runtime_1.jsxs)("h1", { children: ["Reset Your ", siteName, " Password"] })] }), (0, jsx_runtime_1.jsxs)("div", { style: shared_1.LOGIN_STYLE, children: [(0, jsx_runtime_1.jsxs)("div", { style: { margin: "10px 0" }, children: ["Enter your ", siteName, " account's email address and we will email a password reset link to you."] }), (0, jsx_runtime_1.jsxs)("form", { children: [(0, jsx_runtime_1.jsx)(antd_1.Input, { style: { fontSize: "13pt" }, autoFocus: true, placeholder: "Enter your email address", autoComplete: "username", value: email, onChange: (e) => {
                                    setEmail(e.target.value);
                                    setError("");
                                    setSuccess("");
                                }, onPressEnter: (e) => {
                                    e.preventDefault();
                                    if (resetting || !(0, misc_1.is_valid_email_address)(email))
                                        return;
                                    resetPassword();
                                } }), email && ((0, jsx_runtime_1.jsx)(antd_1.Button, { disabled: resetting || !(0, misc_1.is_valid_email_address)(email) || !!error, shape: "round", size: "large", type: "primary", style: { width: "100%", marginTop: "20px" }, onClick: resetPassword, children: resetting ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "spinner", spin: true }), " Sending password reset email..."] })) : !email || !(0, misc_1.is_valid_email_address)(email) || error ? ("Enter your email address.") : ("Send password reset email") }))] }), error && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { marginTop: "20px" }, message: "Error", description: (0, jsx_runtime_1.jsxs)("div", { style: { fontSize: "12pt" }, children: [(0, jsx_runtime_1.jsx)("b", { children: error }), (0, jsx_runtime_1.jsx)("br", {}), " If you are stuck ", (0, jsx_runtime_1.jsx)(contact_1.default, { lower: true }), "."] }), type: "error", showIcon: true })), success && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { marginTop: "20px" }, message: (0, jsx_runtime_1.jsx)("b", { children: "Success" }), description: (0, jsx_runtime_1.jsx)("div", { style: { fontSize: "12pt" }, children: success }), type: "success", showIcon: true }))] }), (0, jsx_runtime_1.jsxs)("div", { style: {
                    ...shared_1.LOGIN_STYLE,
                    backgroundColor: "white",
                    marginTop: "30px",
                    marginBottom: "30px",
                    paddingTop: "15px",
                }, children: [(0, jsx_runtime_1.jsxs)("p", { children: ["Remember your password? ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-in", children: "Sign In" })] }), (0, jsx_runtime_1.jsxs)("p", { children: ["Do not have an account? ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-up", children: "Sign Up" })] }), "You can also", " ", (0, jsx_runtime_1.jsxs)(A_1.default, { href: "/auth/try", children: ["try ", siteName, " without creating an account"] })] })] }));
}
exports.default = PasswordReset;
//# sourceMappingURL=password-reset.js.map