"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 react_1 = require("react");
const antd_1 = require("antd");
const register_1 = __importDefault(require("../register"));
const A_1 = __importDefault(require("components/misc/A"));
const loading_1 = __importDefault(require("components/share/loading"));
const post_1 = __importDefault(require("lib/api/post"));
(0, register_1.default)({
    path: "account/password",
    title: "Password",
    icon: "user-secret",
    desc: "Change or reset your password.",
    Component: () => {
        const [currentPassword, setCurrentPassword] = (0, react_1.useState)("");
        const [newPassword, setNewPassword] = (0, react_1.useState)("");
        const [changing, setChanging] = (0, react_1.useState)(false);
        const [changed, setChanged] = (0, react_1.useState)("");
        const [error, setError] = (0, react_1.useState)("");
        async function resetPassword() {
            setError("");
            setChanging(true);
            setChanged("");
            try {
                await (0, post_1.default)("/accounts/set-password", {
                    currentPassword,
                    newPassword,
                });
                setChanged(newPassword);
            }
            catch (err) {
                setError(err.message);
            }
            finally {
                setChanging(false);
            }
        }
        return ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", children: [(0, jsx_runtime_1.jsx)("h2", { children: "Change Password" }), error && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [error, " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/password-reset", children: "Reset password..." })] }), showIcon: true })), (0, jsx_runtime_1.jsx)("b", { children: "Current password:" }), (0, jsx_runtime_1.jsx)(antd_1.Input.Password, { disabled: changing, placeholder: "Current password...", value: currentPassword, onChange: (e) => setCurrentPassword(e.target.value) }), "(leave blank if you have not set a password)", (0, jsx_runtime_1.jsx)("b", { children: "New password:" }), (0, jsx_runtime_1.jsx)(antd_1.Input.Password, { disabled: changing, placeholder: "New password...", value: newPassword, onChange: (e) => {
                        setNewPassword(e.target.value);
                    }, onPressEnter: resetPassword }), "(at least 6 characters)", (0, jsx_runtime_1.jsx)(antd_1.Button, { type: "primary", disabled: (changed && changed == newPassword) ||
                        changing ||
                        newPassword.length < 6 ||
                        newPassword == currentPassword, onClick: resetPassword, children: changing ? ((0, jsx_runtime_1.jsx)(loading_1.default, { delay: 0, children: "Changing Password" })) : ("Change Password") }), changed && changed == newPassword && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "success", message: "Password successfully changed!", showIcon: true })), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("h2", { children: "Reset Password" }), (0, jsx_runtime_1.jsxs)("div", { children: ["You can also ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/password-reset", children: "reset your password" }), ", in case you don't remember it."] })] }));
    },
});
//# sourceMappingURL=password.js.map