UNPKG

@mopinion/deploy

Version:

Deploy your Mopinion feedback forms with ease

198 lines (159 loc) 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = Deployment; var _hooks = require("preact/hooks"); var _config = require("../../config"); var _api = require("../../api"); var _api2 = require("../api"); var _Box = _interopRequireDefault(require("./Box")); var _Rule = _interopRequireDefault(require("./Rule")); var _jsxRuntime = require("preact/jsx-runtime"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function contains() { var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; return (a === null || a === void 0 ? void 0 : a.toLowerCase().indexOf(b === null || b === void 0 ? void 0 : b.toLowerCase())) > -1; } function Deployment(_ref) { var _deployment$rules2; var deployment = _ref.deployment, searchValue = _ref.searchValue, toggleDrawer = _ref.toggleDrawer, refresh = _ref.refresh; var _useState = (0, _hooks.useState)({}), _useState2 = _slicedToArray(_useState, 2), surveyData = _useState2[0], setSurveyData = _useState2[1]; var _useState3 = (0, _hooks.useState)(false), _useState4 = _slicedToArray(_useState3, 2), hasDraft = _useState4[0], setHasDraft = _useState4[1]; (0, _hooks.useEffect)(function () { (0, _api.loadDraft)(deployment.id, { onSuccess: function onSuccess(response) { var _response$deployment; if (response.OK && Object.keys((_response$deployment = response.deployment) !== null && _response$deployment !== void 0 ? _response$deployment : {}).length > 1) { setHasDraft(true); } } }); }, [deployment]); (0, _hooks.useEffect)(function () { var _deployment$rules; (_deployment$rules = deployment.rules) === null || _deployment$rules === void 0 ? void 0 : _deployment$rules.forEach(function (rule) { var _rule$then$0$args, _rule$then$, _getConfig; var _ref2 = (_rule$then$0$args = (_rule$then$ = rule.then[0]) === null || _rule$then$ === void 0 ? void 0 : _rule$then$.args) !== null && _rule$then$0$args !== void 0 ? _rule$then$0$args : [], _ref3 = _slicedToArray(_ref2, 3), formKey = _ref3[0], _domain = _ref3[1], divName = _ref3[2]; var domain = (_getConfig = (0, _config.getConfig)(function (config) { var _config$mopinion$prox, _config$mopinion, _config$mopinion2; return (_config$mopinion$prox = config === null || config === void 0 ? void 0 : (_config$mopinion = config.mopinion) === null || _config$mopinion === void 0 ? void 0 : _config$mopinion.proxyCustomDomain) !== null && _config$mopinion$prox !== void 0 ? _config$mopinion$prox : config === null || config === void 0 ? void 0 : (_config$mopinion2 = config.mopinion) === null || _config$mopinion2 === void 0 ? void 0 : _config$mopinion2.proxyCacheDomain; })) !== null && _getConfig !== void 0 ? _getConfig : _domain; (0, _api2.getFormConfig)({ formKey: formKey, domain: domain }, { onSuccess: function onSuccess(data) { var _data$properties, _data$properties2, _data$sendOptions, _data$properties3, _data$properties4, _data$properties4$tri, _data$properties4$tri2; var name = data === null || data === void 0 ? void 0 : (_data$properties = data.properties) === null || _data$properties === void 0 ? void 0 : _data$properties.name; var formType = determineFormType(data); var id = data === null || data === void 0 ? void 0 : (_data$properties2 = data.properties) === null || _data$properties2 === void 0 ? void 0 : _data$properties2.id; var domain = data === null || data === void 0 ? void 0 : (_data$sendOptions = data.sendOptions) === null || _data$sendOptions === void 0 ? void 0 : _data$sendOptions.domain; var projectId = data === null || data === void 0 ? void 0 : (_data$properties3 = data.properties) === null || _data$properties3 === void 0 ? void 0 : _data$properties3.project_id; var sliderPosition = formType === 'Slide-in' ? data === null || data === void 0 ? void 0 : (_data$properties4 = data.properties) === null || _data$properties4 === void 0 ? void 0 : (_data$properties4$tri = _data$properties4.trigger) === null || _data$properties4$tri === void 0 ? void 0 : (_data$properties4$tri2 = _data$properties4$tri.slider) === null || _data$properties4$tri2 === void 0 ? void 0 : _data$properties4$tri2.position : null; setSurveyData(function (prev) { return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, data === null || data === void 0 ? void 0 : data.surveyKey, { name: name, formType: formType, id: id, domain: domain, projectId: projectId, sliderPosition: sliderPosition })); }); } }); }); }, [deployment]); return (0, _jsxRuntime.jsxs)(_Box["default"], { name: "list-container", children: [(0, _jsxRuntime.jsxs)(_Box["default"], { name: "subheader", children: [(0, _jsxRuntime.jsxs)(_Box["default"], { name: "flex", children: [(0, _jsxRuntime.jsxs)(_Box["default"], { classes: ['mr-1'], children: ["Deployment: ", deployment.name] }), (0, _jsxRuntime.jsx)(_Box["default"], { name: "chip", classes: deployment.published ? ['active'] : [], children: deployment.published ? 'Active' : 'Inactive' })] }), hasDraft && (0, _jsxRuntime.jsxs)(_Box["default"], { name: "version-container", children: [(0, _jsxRuntime.jsx)(_Box["default"], { name: "version-label", children: "Version" }), (0, _jsxRuntime.jsx)(_Box["default"], { name: "select-container", children: (0, _jsxRuntime.jsxs)("select", { value: (0, _config.getDebugVersion)(deployment.id), onChange: function onChange(e) { (0, _config.setDebugVersion)(deployment.id, e.target.value); refresh(); }, children: [(0, _jsxRuntime.jsx)("option", { value: "live", children: "Live" }), (0, _jsxRuntime.jsx)("option", { value: "draft", children: "Draft" })] }) })] })] }), (_deployment$rules2 = deployment.rules) === null || _deployment$rules2 === void 0 ? void 0 : _deployment$rules2.filter(function (rule) { var _rule$then$0$args2, _rule$then$2, _surveyData$formKey, _surveyData$formKey2; var _ref4 = (_rule$then$0$args2 = (_rule$then$2 = rule.then[0]) === null || _rule$then$2 === void 0 ? void 0 : _rule$then$2.args) !== null && _rule$then$0$args2 !== void 0 ? _rule$then$0$args2 : [], _ref5 = _slicedToArray(_ref4, 1), formKey = _ref5[0]; return !searchValue || contains((_surveyData$formKey = surveyData[formKey]) === null || _surveyData$formKey === void 0 ? void 0 : _surveyData$formKey.name, searchValue) || contains((_surveyData$formKey2 = surveyData[formKey]) === null || _surveyData$formKey2 === void 0 ? void 0 : _surveyData$formKey2.formType, searchValue); }).map(function (rule) { var _rule$then$0$args3, _rule$then$3; var _ref6 = (_rule$then$0$args3 = (_rule$then$3 = rule.then[0]) === null || _rule$then$3 === void 0 ? void 0 : _rule$then$3.args) !== null && _rule$then$0$args3 !== void 0 ? _rule$then$0$args3 : [], _ref7 = _slicedToArray(_ref6, 1), formKey = _ref7[0]; return (0, _jsxRuntime.jsx)(_Rule["default"], { rule: rule, survey: surveyData[formKey], surveyData: surveyData, toggleDrawer: toggleDrawer }); })] }); } function determineFormType() { var _data$properties5, _data$properties6, _data$properties6$tri, _data$properties6$tri2; var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; if (((_data$properties5 = data.properties) === null || _data$properties5 === void 0 ? void 0 : _data$properties5.modal) === false) { return 'Embedded'; } if ((_data$properties6 = data.properties) !== null && _data$properties6 !== void 0 && (_data$properties6$tri = _data$properties6.trigger) !== null && _data$properties6$tri !== void 0 && (_data$properties6$tri2 = _data$properties6$tri.slider) !== null && _data$properties6$tri2 !== void 0 && _data$properties6$tri2.type) { return 'Slide-in'; } return 'Modal'; } //# sourceMappingURL=Deployment.js.map