From f49c31f7bc5e09187c8b7fdc7a88b97e2e6d42e6 Mon Sep 17 00:00:00 2001
From: Muhsin Keloth
Date: Wed, 30 Jul 2025 16:28:37 +0400
Subject: [PATCH] chore: update whatsapp campaign
---
.../WhatsAppCampaign/WhatsAppCampaignForm.vue | 115 ++----------------
1 file changed, 11 insertions(+), 104 deletions(-)
diff --git a/app/javascript/dashboard/components-next/Campaigns/Pages/CampaignPage/WhatsAppCampaign/WhatsAppCampaignForm.vue b/app/javascript/dashboard/components-next/Campaigns/Pages/CampaignPage/WhatsAppCampaign/WhatsAppCampaignForm.vue
index df76ae901..80aafe29f 100644
--- a/app/javascript/dashboard/components-next/Campaigns/Pages/CampaignPage/WhatsAppCampaign/WhatsAppCampaignForm.vue
+++ b/app/javascript/dashboard/components-next/Campaigns/Pages/CampaignPage/WhatsAppCampaign/WhatsAppCampaignForm.vue
@@ -9,6 +9,7 @@ import Input from 'dashboard/components-next/input/Input.vue';
import Button from 'dashboard/components-next/button/Button.vue';
import ComboBox from 'dashboard/components-next/combobox/ComboBox.vue';
import TagMultiSelectComboBox from 'dashboard/components-next/combobox/TagMultiSelectComboBox.vue';
+import WhatsAppTemplateParserCore from 'dashboard/components-next/shared/WhatsAppTemplateParserCore.vue';
const emit = defineEmits(['submit', 'cancel']);
@@ -30,7 +31,7 @@ const initialState = {
};
const state = reactive({ ...initialState });
-const processedParams = ref({});
+const templateParserRef = ref(null);
const rules = {
title: { required, minLength: minLength(1) },
@@ -88,26 +89,6 @@ const selectedTemplate = computed(() => {
?.template;
});
-const templateString = computed(() => {
- if (!selectedTemplate.value) return '';
- try {
- return (
- selectedTemplate.value.components?.find(
- component => component.type === 'BODY'
- )?.text || ''
- );
- } catch (error) {
- return '';
- }
-});
-
-const processedString = computed(() => {
- if (!templateString.value) return '';
- return templateString.value.replace(/{{([^}]+)}}/g, (match, variable) => {
- return processedParams.value[variable] || `{{${variable}}}`;
- });
-});
-
const getErrorMessage = (field, errorKey) => {
const baseKey = 'CAMPAIGN.WHATSAPP.CREATE.FORM';
return v$.value[field].$error ? t(`${baseKey}.${errorKey}.ERROR`) : '';
@@ -122,8 +103,7 @@ const formErrors = computed(() => ({
}));
const hasRequiredTemplateParams = computed(() => {
- const params = Object.values(processedParams.value);
- return params.length === 0 || params.every(param => param.trim() !== '');
+ return templateParserRef.value?.v$?.$invalid === false || true;
});
const isSubmitDisabled = computed(
@@ -135,32 +115,18 @@ const formatToUTCString = localDateTime =>
const resetState = () => {
Object.assign(state, initialState);
- processedParams.value = {};
v$.value.$reset();
};
const handleCancel = () => emit('cancel');
-const generateVariables = () => {
- const matchedVariables = templateString.value.match(/{{([^}]+)}}/g);
- if (!matchedVariables) {
- processedParams.value = {};
- return;
- }
-
- const finalVars = matchedVariables.map(match => match.replace(/{{|}}/g, ''));
- processedParams.value = finalVars.reduce((acc, variable) => {
- acc[variable] = processedParams.value[variable] || '';
- return acc;
- }, {});
-};
-
const prepareCampaignDetails = () => {
// Find the selected template to get its content
const currentTemplate = selectedTemplate.value;
+ const parserData = templateParserRef.value;
// Extract template content - this should be the template message body
- const templateContent = templateString.value;
+ const templateContent = parserData?.processedString || '';
// Prepare template_params object with the same structure as used in contacts
const templateParams = {
@@ -168,7 +134,7 @@ const prepareCampaignDetails = () => {
namespace: currentTemplate?.namespace || '',
category: currentTemplate?.category || 'UTILITY',
language: currentTemplate?.language || 'en_US',
- processed_params: processedParams.value,
+ processed_params: parserData?.processedParams || {},
};
return {
@@ -198,15 +164,6 @@ watch(
() => state.inboxId,
() => {
state.templateId = null;
- processedParams.value = {};
- }
-);
-
-// Generate variables when template changes
-watch(
- () => state.templateId,
- () => {
- generateVariables();
}
);
@@ -254,62 +211,12 @@ watch(
-
-
+
-
-
- {{ selectedTemplate.name }}
-
-
- {{ t('CAMPAIGN.WHATSAPP.CREATE.FORM.TEMPLATE.LANGUAGE') }}:
- {{ selectedTemplate.language || 'en' }}
-
-
-
-
-
-
- {{ processedString }}
-
-
-
-
-
- {{ t('CAMPAIGN.WHATSAPP.CREATE.FORM.TEMPLATE.CATEGORY') }}:
- {{ selectedTemplate.category || 'UTILITY' }}
-
-
-
-
-
-
-
-
+ ref="templateParserRef"
+ :template="selectedTemplate"
+ />