"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditableDescription = exports.EditableTitle = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const antd_1 = require("antd");
const react_1 = require("react");
const post_1 = __importDefault(require("lib/api/post"));
const misc_1 = require("@cocalc/util/misc");
function EditableTitle({ license_id, title, onChange, }) {
    return ((0, jsx_runtime_1.jsx)(EditableTextField, { license_id: license_id, field: "title", value: title, onChange: onChange }));
}
exports.EditableTitle = EditableTitle;
function EditableDescription({ license_id, description, onChange, }) {
    return ((0, jsx_runtime_1.jsx)(EditableTextField, { license_id: license_id, field: "description", value: description, rows: 3, onChange: onChange }));
}
exports.EditableDescription = EditableDescription;
function EditableTextField({ license_id, field, value, rows, onChange, }) {
    const [edit, setEdit] = (0, react_1.useState)(false);
    const [value2, setValue] = (0, react_1.useState)(value ?? "");
    const [error, setError] = (0, react_1.useState)("");
    (0, react_1.useEffect)(() => {
        setValue(value ?? "");
        setEdit(false);
        setError("");
    }, [value]);
    async function save(value) {
        setEdit(false);
        setError("");
        const query = { manager_site_licenses: { id: license_id, [field]: value } };
        try {
            await (0, post_1.default)("/user-query", { query });
            onChange?.();
        }
        catch (err) {
            setError(err.message);
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", { style: { cursor: "pointer" }, onClick: () => setEdit(true), children: [error && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: `Error saving ${field} - ${error}` })), (0, misc_1.capitalize)(field), ":", " ", edit &&
                (rows ? ((0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { autoFocus: true, value: value2, onChange: (e) => setValue(e.target.value), onBlur: () => save(value2), rows: rows })) : ((0, jsx_runtime_1.jsx)(antd_1.Input, { autoFocus: true, value: value2, onChange: (e) => setValue(e.target.value), onBlur: () => save(value2), onPressEnter: () => save(value2) }))), !edit && (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: value2.trim() ? value2 : `(set ${field}...)` })] }));
}
//# sourceMappingURL=editable-license.js.map