From 08ac50d44266a6c2cc3e23a3b999444ea9f76b80 Mon Sep 17 00:00:00 2001 From: Jacob Thomason Date: Mon, 9 Oct 2023 16:50:49 -0400 Subject: [PATCH] Added new addChoices util function --- lib/utils/index.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/utils/index.js b/lib/utils/index.js index 7bd2278..e4244e7 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -36,6 +36,38 @@ const addDynamicDropdowns = (inputFields, triggerMapping) => { } +/** + * Adds a choices dropdown for an input field. This can be useful to use in substitution for a + * trigger. It's also often used with enums. + * + * @param {Array} inputFields The inputFields array from the operation + * @param {Object} choices An object of key/value pairs where the key is to be passed in + * the bundle with the request and the value is the human friendly + * + * @return {Array} The inputFields array with the choices added + */ +const addChoices = (inputFields, choices) => { + for (const [key, value] of Object.entries(choices)) { + inputFields.map((inputField) => { + if (inputField.key === key) { + inputField.choices = `${value}`; + } + + // Zapier only supports one level of depth (should be refactored to recursive for more depth) + if (inputField.children && inputField.children.length) { + inputField.children.map((child) => { + if (child.key === key) { + child.choices = `${value}`; + } + }); + } + }); + } + + return inputFields; +} + + /** * Will quote a string, but leave other types alone * @@ -54,5 +86,6 @@ const quote = (value) => { module.exports = { addDynamicDropdowns, + addChoices, quote, };