UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

88 lines 4.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowCustomConditional = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const core_1 = require("@botonic/core"); const tracking_1 = require("../tracking"); const utils_1 = require("../utils"); const content_fields_base_1 = require("./content-fields-base"); class FlowCustomConditional extends content_fields_base_1.ContentFieldsBase { constructor() { super(...arguments); this.arguments = []; this.conditionalResult = undefined; this.customResult = false; this.variableFormat = ''; } static fromHubtypeCMS(component, request) { const newCustomConditional = new FlowCustomConditional(component.id); newCustomConditional.code = component.code; newCustomConditional.arguments = component.content.arguments; newCustomConditional.resultMapping = component.content.result_mapping; newCustomConditional.setConditionalResult(request); newCustomConditional.variableFormat = component.content.arguments[0].type; return newCustomConditional; } setConditionalResult(request) { const functionArgument = this.arguments.find(arg => { if ('name' in arg) { return arg.name === 'keyPath'; } return false; }); let keyPath = ''; if (functionArgument && 'value' in functionArgument) { keyPath = functionArgument.value; } else { throw new Error(`Key path not found for node ${this.code}`); } const botVariable = (0, utils_1.getValueFromKeyPath)(request, keyPath); let conditionalResult = this.resultMapping.find(rMap => rMap.result === botVariable) || this.resultMapping.find(rMap => rMap.result === 'default'); if (this.isBooleanConditional(this.resultMapping) && typeof botVariable !== 'boolean') { conditionalResult = this.resultMapping.find(rMap => rMap.result === (botVariable !== undefined && botVariable !== null)) || this.resultMapping.find(rMap => rMap.result === 'default'); } if (!conditionalResult) { throw new Error(`No conditional result found for node ${this.code} with key path: ${keyPath}`); } this.conditionalResult = conditionalResult; this.customResult = conditionalResult.result; this.followUp = conditionalResult.target; } trackFlow(request) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const { flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId } = (0, tracking_1.getCommonFlowContentEventArgsForContentId)(request, this.id); if (!((_a = this.conditionalResult) === null || _a === void 0 ? void 0 : _a.result)) { console.warn(`Tracking event for node ${this.code} but no conditional result found`); } const eventCustomConditional = { action: core_1.EventAction.ConditionalCustom, flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId, flowNodeIsMeaningful: false, conditionalVariable: this.customResult.toString(), variableFormat: this.variableFormat, }; const { action } = eventCustomConditional, eventArgs = tslib_1.__rest(eventCustomConditional, ["action"]); yield (0, tracking_1.trackEvent)(request, action, eventArgs); }); } isBooleanConditional(resultMapping) { return (resultMapping.some(rMap => rMap.result === true) && resultMapping.some(rMap => rMap.result === false) && resultMapping.some(rMap => rMap.result === 'default')); } toBotonic() { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } } exports.FlowCustomConditional = FlowCustomConditional; //# sourceMappingURL=flow-custom-conditional.js.map