UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

154 lines (153 loc) 5.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = useBlock; var _hooks = require("preact/hooks"); var _BlockStore = require("../stores/BlockStore"); var _ConfigStore = require("../stores/ConfigStore"); var _constants = require("../../../constants"); function getErrorMessage(_ref) { var _text$errors, _text$defaultErrors, _text$errors2, _text$defaultErrors2; var reason = _ref.reason, _ref$text = _ref.text, text = _ref$text === void 0 ? {} : _ref$text, _ref$block = _ref.block, block = _ref$block === void 0 ? {} : _ref$block; if ((_text$errors = text.errors) !== null && _text$errors !== void 0 && _text$errors[reason]) { return text.errors[reason]; } if ((_text$defaultErrors = text.defaultErrors) !== null && _text$defaultErrors !== void 0 && _text$defaultErrors[reason]) { return text.defaultErrors[reason]; } if ((_text$errors2 = text.errors) !== null && _text$errors2 !== void 0 && _text$errors2.required) { return text.errors.required; } if ((_text$defaultErrors2 = text.defaultErrors) !== null && _text$defaultErrors2 !== void 0 && _text$defaultErrors2.required) { return text.defaultErrors.required; } return 'This field is required'; } function useBlock(_ref2) { var id = _ref2.id, typeName = _ref2.typeName, _ref2$typeOf = _ref2.typeOf, typeOf = _ref2$typeOf === void 0 ? 0 : _ref2$typeOf; var blockState = (0, _BlockStore.useBlockStore)(); var _useConfigStore = (0, _ConfigStore.useConfigStore)(), _useConfigStore$block = _useConfigStore.blocks, blocks = _useConfigStore$block === void 0 ? {} : _useConfigStore$block, _useConfigStore$text = _useConfigStore.text, text = _useConfigStore$text === void 0 ? {} : _useConfigStore$text, _useConfigStore$block2 = _useConfigStore.blockRules, blockRules = _useConfigStore$block2 === void 0 ? {} : _useConfigStore$block2, layout = _useConfigStore.layout; var errorMessagesRef = (0, _hooks.useRef)({}); var block = (0, _hooks.useMemo)(function () { if (id === 'recaptcha') { return { id: 'recaptcha', typeName: 'recaptcha', properties: {} }; } if (id === _constants.honeyBotId) { return { id: _constants.honeyBotId, typeName: _constants.honeyBotTypeName, title: 'What is your opinion?', properties: {} }; } if (id) { var _blocks$id; return (_blocks$id = blocks[id]) !== null && _blocks$id !== void 0 ? _blocks$id : {}; } if (typeName) { var _blocks$idFromTypeNam; var idFromTypeName = layout === null || layout === void 0 ? void 0 : layout.filter(function (id) { var _blocks$id2; return ((_blocks$id2 = blocks[id]) === null || _blocks$id2 === void 0 ? void 0 : _blocks$id2.typeName) === typeName; })[typeOf]; return (_blocks$idFromTypeNam = blocks[idFromTypeName]) !== null && _blocks$idFromTypeNam !== void 0 ? _blocks$idFromTypeNam : {}; } return {}; }, [blocks, id, typeName, typeOf, layout]); var state = (0, _hooks.useMemo)(function () { var _blockState$block$id; return (_blockState$block$id = blockState[block.id]) !== null && _blockState$block$id !== void 0 ? _blockState$block$id : {}; }, [block === null || block === void 0 ? void 0 : block.id, blockState]); var actions = (0, _hooks.useMemo)(function () { return { setValue: function setValue(value) { return blockState.actions.dispatch({ type: 'set_value', payload: { value: value, block: block, rules: blockRules[block.id], layout: layout } }); }, setNestedValue: function setNestedValue(datafieldId, value) { return blockState.actions.dispatch({ type: 'set_nested_value', payload: { value: value, block: block, datafieldId: datafieldId, rules: blockRules[block.id], layout: layout } }); }, setHoneyBotValue: function setHoneyBotValue(value) { blockState.actions.dispatch({ type: 'set_honeybot_value', payload: { value: value } }); }, skipQuestion: function skipQuestion() { return blockState.actions.dispatch({ type: 'set_skip', payload: { block: block, blocks: blocks, layout: layout, blockRules: blockRules } }); } }; }, [layout, blockState.actions, blockRules, block, blocks]); var errorMessage = state.showError ? getErrorMessage({ reason: state.errorReason, text: text, block: block }) : ''; var errorMessages = (0, _hooks.useMemo)(function () { if (block.typeName !== 'contact') { return errorMessagesRef.current; } return Object.keys(state).reduce(function (dataFields, currentPropKey) { if (!isNaN(Number(currentPropKey))) { dataFields[currentPropKey] = getErrorMessage({ reason: state[currentPropKey].errorReason, text: text }); } return dataFields; }, {}); }, [block.typeName, state, text]); return { block: block, state: state, errorMessage: errorMessage, errorMessages: errorMessages, actions: actions }; } //# sourceMappingURL=useBlock.js.map