"use strict";
exports.id = 6348;
exports.ids = [6348];
exports.modules = {

/***/ 196348:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Z": () => (/* binding */ SaveButton)
/* harmony export */ });
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(920997);
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(616689);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(546517);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var antd__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(545725);
/* harmony import */ var antd__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(antd__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var lib_hooks_mounted__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(900686);
/* harmony import */ var components_share_loading__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(976087);
/* harmony import */ var lib_api_post__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(194184);
/* harmony import */ var _cocalc_frontend_components_icon__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(807772);
/* harmony import */ var _cocalc_frontend_components_icon__WEBPACK_IMPORTED_MODULE_7___default = /*#__PURE__*/__webpack_require__.n(_cocalc_frontend_components_icon__WEBPACK_IMPORTED_MODULE_7__);
/* harmony import */ var _cocalc_util_schema__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(685219);
/* harmony import */ var _cocalc_util_misc__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(477389);
/* harmony import */ var _cocalc_util_misc__WEBPACK_IMPORTED_MODULE_9___default = /*#__PURE__*/__webpack_require__.n(_cocalc_util_misc__WEBPACK_IMPORTED_MODULE_9__);










const DEBOUNCE_MS = 1500;
function SaveButton({ disabled , edited , original , setOriginal , table , style , onSave , isValid , debounce_ms  }) {
    if (debounce_ms == null) debounce_ms = DEBOUNCE_MS;
    const { 0: saving , 1: setSaving  } = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)(false);
    const { 0: error , 1: setError  } = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)("");
    // Tricky hooks: We have to store the state in a ref as well so that
    // we can use it in the save function, since that function
    // is memoized and called from a debounced function.
    const saveRef = (0,react__WEBPACK_IMPORTED_MODULE_1__.useRef)({
        edited,
        original,
        table
    });
    saveRef.current = {
        edited,
        original,
        table
    };
    const isMounted = (0,lib_hooks_mounted__WEBPACK_IMPORTED_MODULE_4__/* ["default"] */ .Z)();
    const save = (0,react__WEBPACK_IMPORTED_MODULE_1__.useMemo)(()=>{
        return async ()=>{
            const { edited , original , table  } = saveRef.current;
            let changes = false;
            const e = {};
            for(const field in edited){
                if (!(0,lodash__WEBPACK_IMPORTED_MODULE_2__.isEqual)(original[field], edited[field])) {
                    e[field] = (0,lodash__WEBPACK_IMPORTED_MODULE_2__.cloneDeep)(edited[field]);
                    changes = true;
                }
            }
            if (!changes) {
                // no changes to save.
                return false;
            }
            for (const field1 of preserveFields(table)){
                e[field1] = (0,lodash__WEBPACK_IMPORTED_MODULE_2__.cloneDeep)(edited[field1]);
            }
            const query = {
                [table]: e
            };
            if (isMounted.current) {
                setSaving(true);
                setError("");
            }
            let result;
            try {
                // Note -- we definitely do want to do the save
                // itself, even if the component is already unmounted,
                // so we don't loose changes.
                result = await (0,lib_api_post__WEBPACK_IMPORTED_MODULE_6__/* ["default"] */ .Z)("/user-query", {
                    query
                });
            } catch (err) {
                if (!isMounted.current) return;
                setError(err.message);
                return false;
            } finally{
                if (isMounted.current) {
                    setSaving(false);
                }
            }
            if (!isMounted.current) return;
            if (result.error) {
                setError(result.error);
            } else {
                setOriginal((0,lodash__WEBPACK_IMPORTED_MODULE_2__.cloneDeep)(edited));
            }
            return true; // successful save
        };
    }, []);
    async function doSave() {
        const e = (0,lodash__WEBPACK_IMPORTED_MODULE_2__.cloneDeep)(saveRef.current.edited);
        if (table) {
            const didSave = await save();
            if (!isMounted.current) return;
            if (didSave) {
                await onSave?.(e);
            }
            return;
        }
        try {
            await onSave?.(e);
            if (!isMounted.current) return;
            setOriginal(e);
            setError("");
        } catch (err) {
            setError(err.toString());
        }
    }
    const doSaveDebounced = (0,react__WEBPACK_IMPORTED_MODULE_1__.useMemo)(()=>(0,lodash__WEBPACK_IMPORTED_MODULE_2__.debounce)(doSave, debounce_ms), [
        onSave
    ]);
    (0,react__WEBPACK_IMPORTED_MODULE_1__.useEffect)(()=>{
        doSaveDebounced();
        return doSaveDebounced;
    }, [
        edited
    ]);
    const same = (0,lodash__WEBPACK_IMPORTED_MODULE_2__.isEqual)(edited, original);
    return /*#__PURE__*/ (0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsxs)("div", {
        style: style,
        children: [
            /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(antd__WEBPACK_IMPORTED_MODULE_3__.Button, {
                type: "primary",
                disabled: disabled ?? (saving || same || isValid != null && !isValid(edited)),
                onClick: doSave,
                children: /*#__PURE__*/ (0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsxs)(antd__WEBPACK_IMPORTED_MODULE_3__.Space, {
                    children: [
                        /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(_cocalc_frontend_components_icon__WEBPACK_IMPORTED_MODULE_7__.Icon, {
                            name: "save"
                        }),
                        saving ? /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(components_share_loading__WEBPACK_IMPORTED_MODULE_5__/* ["default"] */ .Z, {
                            delay: 250,
                            before: "Save",
                            children: "Saving..."
                        }) : "Save"
                    ]
                })
            }),
            !same && error && /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(antd__WEBPACK_IMPORTED_MODULE_3__.Alert, {
                type: "error",
                message: error,
                style: {
                    marginTop: "15px"
                }
            })
        ]
    });
};
function preserveFields(table) {
    return (0,_cocalc_util_misc__WEBPACK_IMPORTED_MODULE_9__.keys)(_cocalc_util_schema__WEBPACK_IMPORTED_MODULE_8__.SCHEMA[table].user_query?.set?.required_fields ?? {});
}


/***/ })

};
;