@botonic/plugin-flow-builder
Version:
Use Flow Builder to show your contents
84 lines • 3.92 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { EventAction } from '@botonic/core';
import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../tracking';
import { getValueFromKeyPath } from '../utils';
import { ContentFieldsBase } from './content-fields-base';
export class FlowCustomConditional extends 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 = 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 __awaiter(this, void 0, void 0, function* () {
const { flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId } = 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: EventAction.ConditionalCustom,
flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId,
flowNodeIsMeaningful: false,
conditionalVariable: this.customResult.toString(),
variableFormat: this.variableFormat,
};
const { action } = eventCustomConditional, eventArgs = __rest(eventCustomConditional, ["action"]);
yield 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 _jsx(_Fragment, {});
}
}
//# sourceMappingURL=flow-custom-conditional.js.map