UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

93 lines (92 loc) 6.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = useHandleExtraProps; var _hooks = require("preact/hooks"); var _BlockStore = require("../stores/BlockStore"); var _ConfigStore = require("../stores/ConfigStore"); function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function getDataField(_ref) { var _block$properties2, _block$properties5; var block = _ref.block, extra1 = _ref.extra1, extra2 = _ref.extra2; if (extra1 === 'extra' && extra2) { var _block$properties; return (_block$properties = block.properties) === null || _block$properties === void 0 || (_block$properties = _block$properties.elements_extra) === null || _block$properties === void 0 || (_block$properties = _block$properties[extra2]) === null || _block$properties === void 0 ? void 0 : _block$properties.data_field; } if (extra1 === 'name' && !((_block$properties2 = block.properties) !== null && _block$properties2 !== void 0 && (_block$properties2 = _block$properties2.elements) !== null && _block$properties2 !== void 0 && (_block$properties2 = _block$properties2.name) !== null && _block$properties2 !== void 0 && _block$properties2.combine)) { var _block$properties3; return (_block$properties3 = block.properties) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.elements) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.name) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.subelements) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.lastname) === null || _block$properties3 === void 0 ? void 0 : _block$properties3.data_field; } if (extra1 === 'firstname') { var _block$properties4; return (_block$properties4 = block.properties) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.elements) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.name) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.subelements) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.firstname) === null || _block$properties4 === void 0 ? void 0 : _block$properties4.data_field; } return (_block$properties5 = block.properties) === null || _block$properties5 === void 0 || (_block$properties5 = _block$properties5.elements) === null || _block$properties5 === void 0 || (_block$properties5 = _block$properties5[extra1]) === null || _block$properties5 === void 0 ? void 0 : _block$properties5.data_field; } function useHandleExtraProps() { var dispatch = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {}; var _useFormProps = (0, _ConfigStore.useFormProps)(), extra = _useFormProps.extra; var _useBlockStore = (0, _BlockStore.useBlockStore)(), actions = _useBlockStore.actions; var _useConfigStore = (0, _ConfigStore.useConfigStore)(), layout = _useConfigStore.layout, blocks = _useConfigStore.blocks, blockRules = _useConfigStore.blockRules; (0, _hooks.useEffect)(function () { if (extra !== null && extra !== void 0 && extra.values) { var _extra$values; Object.keys((_extra$values = extra.values) !== null && _extra$values !== void 0 ? _extra$values : {}).forEach(function () { var field = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var _field$replace$split = field.replace(/([^1-9]+)/, '').split('_'), _field$replace$split2 = _slicedToArray(_field$replace$split, 3), blockId = _field$replace$split2[0], extra1 = _field$replace$split2[1], extra2 = _field$replace$split2[2]; if (blockId && !extra1 && !extra2) { actions.dispatch({ type: 'set_value', payload: { block: blocks[blockId], value: extra.values[field], rules: blockRules[blockId], layout: layout } }); } else if (blockId) { actions.dispatch({ type: 'set_nested_value', payload: { block: blocks[blockId], datafieldId: getDataField({ block: blocks[blockId], extra1: extra1, extra2: extra2 }), value: extra.values[field], rules: blockRules[blockId], layout: layout } }); } }); } }, [extra, blocks, blockRules, layout]); (0, _hooks.useEffect)(function () { if (extra !== null && extra !== void 0 && extra.page) { dispatch({ type: 'to_page', payload: extra.page }); } }, [extra]); } //# sourceMappingURL=useHandleExtraProps.js.map