UNPKG

@focuson/form_components

Version:

Components that can be used by @focuson/forms

49 lines (48 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestButton = exports.RestLoadWindow = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const rest_1 = require("@focuson/rest"); const state_1 = require("@focuson/state"); const utils_1 = require("@focuson/utils"); const common_1 = require("./common"); const pages_1 = require("@focuson/pages"); const react_1 = require("react"); function RestLoadWindow(state, { msg, button, rest, action, onClose, className }) { const id = 'rest.load.window'; function onClick(e) { const txs = (0, pages_1.closeOnePageTxs)('RestLoadWindow', state, (0, utils_1.toArray)(onClose)); state.massTransform((0, state_1.reasonFor)('RestLoadWindow', "onChange", id, JSON.stringify({ rest, action })))(...txs); } return (0, jsx_runtime_1.jsxs)("div", Object.assign({ className: className ? className : 'dialog confirm-window' }, { children: [msg && (0, jsx_runtime_1.jsx)("p", { children: msg }), (0, jsx_runtime_1.jsx)("button", Object.assign({ onClick: onClick }, { children: button ? button : 'close' }))] })); } exports.RestLoadWindow = RestLoadWindow; function RestButton(props) { const { id, rest, action, result, state, text, confirm, validate, dateFn, onSuccess, enabledBy, name, buttonType, on404, onComplete, loader } = props; const debug = false; //just to stop spamming: should already have all the validations visible if debugging is on const debounceRef = (0, react_1.useRef)(null); function onClick() { const now = new Date(); const lastClick = debounceRef.current; if (lastClick !== null && (now.getTime() - lastClick.getTime()) < 1000) { console.log("stopped bounce"); return; } // @ts-ignore debounceRef.current = now; const realvalidate = validate === undefined ? true : validate; if (realvalidate && (0, pages_1.hasValidationErrorAndReport)(id, state, dateFn)) return; const restCommand = { restAction: action, name: rest, changeOnSuccess: onSuccess, on404, changeOnCompletion: onComplete }; const realRestCommand = (0, rest_1.addLoaderCommandsIfNeeded)(loader, restCommand); if ((0, pages_1.isConfirmWindow)(confirm)) (0, pages_1.openConfirmWindow)(confirm, 'justclose', [], state, 'RestButton', id, 'onClick', realRestCommand); else if ((0, pages_1.confirmIt)(state, confirm)) { const loadWindowTxs = loader ? (0, pages_1.openRestLoadWindowTxs)(Object.assign(Object.assign({}, loader), { rest, action }), state.context.pageSelectionL, state.context.dateFn) : []; const restTx = [state.context.restL, old => [...old, realRestCommand]]; state.massTransform((0, state_1.reasonFor)('RestButton', 'onClick', id))(restTx, ...loadWindowTxs); } } return (0, pages_1.wrapWithErrors)(id, enabledBy, [], (errorProps, error, errorRef, errors) => (0, jsx_runtime_1.jsx)("button", Object.assign({ ref: (0, pages_1.getRefForValidateLogicToButton)(state)(id, debug, validate, errors, errorRef) }, errorProps, { onClick: onClick, className: (0, common_1.getButtonClassName)(buttonType), disabled: error }, { children: text ? text : name }))); } exports.RestButton = RestButton;