UNPKG

@indec/form-builder

Version:

Form builder

93 lines (92 loc) 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSubQuestions = exports["default"] = void 0; var _questionTypes = _interopRequireDefault(require("../constants/questionTypes")); var _dateTypes = _interopRequireDefault(require("../constants/dateTypes")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var getValue = function getValue(question) { switch (question.type) { case _questionTypes["default"].CHECKBOX: return []; case _questionTypes["default"].RADIO_TABLE: { var opts = question.options.reduce(function (accumulator, currentValue) { return _objectSpread(_objectSpread({}, accumulator), {}, _defineProperty({}, currentValue.name, undefined)); }, {}); return opts; } case _questionTypes["default"].DATE: if ([_dateTypes["default"].RANGE_WITHOUT_HOUR, _dateTypes["default"].RANGE_WITH_HOUR].includes(question.metadata.dateType)) { return { start: '', end: '' }; } return ''; default: return ''; } }; var getSubQuestions = exports.getSubQuestions = function getSubQuestions(subQuestions) { return Object.fromEntries(subQuestions.map(function (subQuestion) { return [subQuestion.name, { id: subQuestion.id, answer: { value: '' } }]; })); }; var buildQuestions = function buildQuestions(section) { var values = _defineProperty({}, section.name, { id: 1 }); if (section.interruption.interruptible) { values[section.name][section.interruption.name] = { id: "section-".concat(section.id), answer: { value: '' } }; } section.questions.forEach(function (question) { var id = question.id; if (question.type === _questionTypes["default"].MESSAGE) { values[section.name][question.name] = { id: id }; } else { values[section.name][question.name] = { id: id, answer: { value: getValue(question) } }; if (question.subQuestions && question.subQuestions.length > 0) { values[section.name][question.name] = _objectSpread(_objectSpread({}, values[section.name][question.name]), {}, { answer: _objectSpread(_objectSpread({}, values[section.name][question.name].answer), {}, { specifications: getSubQuestions(question.subQuestions) }) }); } if (question.multiple) { values[section.name][question.name] = _objectSpread(_objectSpread({}, values[section.name][question.name]), {}, { answer: [_objectSpread({ id: 1 }, values[section.name][question.name].answer)] }); } } }); values[section.name] = [values[section.name]]; return values; }; var _default = exports["default"] = buildQuestions;