diff --git a/app/javascript/dashboard/components-next/NewConversation/components/WhatsappTemplateParser.vue b/app/javascript/dashboard/components-next/NewConversation/components/WhatsappTemplateParser.vue index f3b8a26da..0dfd53d3a 100644 --- a/app/javascript/dashboard/components-next/NewConversation/components/WhatsappTemplateParser.vue +++ b/app/javascript/dashboard/components-next/NewConversation/components/WhatsappTemplateParser.vue @@ -24,10 +24,43 @@ const templateName = computed(() => { return props.template?.name || ''; }); +const templateComponents = computed(() => { + return props.template?.components || []; +}); + const templateString = computed(() => { - return props.template?.components?.find( - component => component.type === 'BODY' - ).text; + return ( + props.template?.components?.find(component => component.type === 'BODY') + ?.text || '' + ); +}); + +const headerComponent = computed(() => { + return templateComponents.value.find( + component => component.type === 'HEADER' + ); +}); + +const footerComponent = computed(() => { + return templateComponents.value.find( + component => component.type === 'FOOTER' + ); +}); + +const buttonComponents = computed(() => { + return templateComponents.value.filter( + component => component.type === 'BUTTONS' + ); +}); + +const legacyParams = computed(() => { + const params = {}; + Object.keys(processedParams.value).forEach(key => { + if (!['header', 'body', 'footer', 'buttons'].includes(key)) { + params[key] = processedParams.value[key]; + } + }); + return params; }); const processVariable = str => { @@ -72,14 +105,76 @@ const getFieldErrorType = key => { }; const generateVariables = () => { - const matchedVariables = templateString.value.match(/{{([^}]+)}}/g); - if (!matchedVariables) return; + const allVariables = {}; - const finalVars = matchedVariables.map(i => processVariable(i)); - processedParams.value = finalVars.reduce((acc, variable) => { - acc[variable] = ''; - return acc; - }, {}); + // Process body variables + const bodyVars = templateString.value.match(/{{([^}]+)}}/g) || []; + bodyVars.forEach(variable => { + const key = processVariable(variable); + if (!allVariables.body) allVariables.body = {}; + allVariables.body[key] = ''; + }); + + // Process header variables + if (headerComponent.value?.text) { + const headerVars = headerComponent.value.text.match(/{{([^}]+)}}/g) || []; + headerVars.forEach(variable => { + const key = processVariable(variable); + if (!allVariables.header) allVariables.header = {}; + allVariables.header[key] = ''; + }); + } + + // Process footer variables + if (footerComponent.value?.text) { + const footerVars = footerComponent.value.text.match(/{{([^}]+)}}/g) || []; + footerVars.forEach(variable => { + const key = processVariable(variable); + if (!allVariables.footer) allVariables.footer = {}; + allVariables.footer[key] = ''; + }); + } + + // Process button variables + buttonComponents.value.forEach(buttonComponent => { + if (buttonComponent.buttons) { + buttonComponent.buttons.forEach((button, index) => { + // Handle URL buttons with variables + if (button.url && button.url.includes('{{')) { + const buttonVars = button.url.match(/{{([^}]+)}}/g) || []; + buttonVars.forEach(() => { + if (!allVariables.buttons) allVariables.buttons = []; + if (!allVariables.buttons[index]) allVariables.buttons[index] = {}; + allVariables.buttons[index].type = 'url'; + allVariables.buttons[index].parameter = ''; + }); + } + + // Handle copy code buttons + if (button.type === 'COPY_CODE') { + if (!allVariables.buttons) allVariables.buttons = []; + if (!allVariables.buttons[index]) allVariables.buttons[index] = {}; + allVariables.buttons[index].type = 'copy_code'; + allVariables.buttons[index].parameter = ''; + } + }); + } + }); + + processedParams.value = allVariables; +}; + +const validateButtonParameter = (button, index) => { + const parameter = processedParams.value.buttons[index].parameter; + + if (button.type === 'copy_code') { + if (parameter && parameter.length > 15) { + processedParams.value.buttons[index].parameter = parameter.substring( + 0, + 15 + ); + } + } }; const sendMessage = async () => { @@ -117,8 +212,8 @@ onMounted(() => { }}
{ }} + +