2022-06-07 12:03:33 +00:00
|
|
|
<template>
|
|
|
|
<div class="medium-12 columns">
|
|
|
|
<textarea
|
|
|
|
v-model="processedString"
|
|
|
|
rows="4"
|
|
|
|
readonly
|
|
|
|
class="template-input"
|
2022-06-10 13:59:52 +00:00
|
|
|
/>
|
2022-06-09 16:04:59 +00:00
|
|
|
<div v-if="variables" class="template__variables-container">
|
|
|
|
<p class="variables-label">
|
|
|
|
{{ $t('WHATSAPP_TEMPLATES.PARSER.VARIABLES_LABEL') }}
|
|
|
|
</p>
|
|
|
|
<div
|
|
|
|
v-for="(variable, key) in processedParams"
|
|
|
|
:key="key"
|
|
|
|
class="template__variable-item"
|
|
|
|
>
|
|
|
|
<span class="variable-label">
|
|
|
|
{{ key }}
|
|
|
|
</span>
|
|
|
|
<woot-input
|
|
|
|
v-model="processedParams[key]"
|
|
|
|
type="text"
|
|
|
|
class="variable-input"
|
|
|
|
:styles="{ marginBottom: 0 }"
|
|
|
|
/>
|
2022-06-07 12:03:33 +00:00
|
|
|
</div>
|
2022-06-09 16:04:59 +00:00
|
|
|
<p v-if="$v.$dirty && $v.$invalid" class="error">
|
|
|
|
{{ $t('WHATSAPP_TEMPLATES.PARSER.FORM_ERROR_MESSAGE') }}
|
|
|
|
</p>
|
2022-06-07 12:03:33 +00:00
|
|
|
</div>
|
|
|
|
<footer>
|
|
|
|
<woot-button variant="smooth" @click="$emit('resetTemplate')">
|
|
|
|
{{ $t('WHATSAPP_TEMPLATES.PARSER.GO_BACK_LABEL') }}
|
|
|
|
</woot-button>
|
|
|
|
<woot-button @click="sendMessage">
|
|
|
|
{{ $t('WHATSAPP_TEMPLATES.PARSER.SEND_MESSAGE_LABEL') }}
|
|
|
|
</woot-button>
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const allKeysRequired = value => {
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
return keys.every(key => value[key]);
|
|
|
|
};
|
2022-06-09 16:04:59 +00:00
|
|
|
import { requiredIf } from 'vuelidate/lib/validators';
|
2022-06-07 12:03:33 +00:00
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
template: {
|
|
|
|
type: Object,
|
|
|
|
default: () => {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validations: {
|
|
|
|
processedParams: {
|
2022-06-09 16:04:59 +00:00
|
|
|
requiredIfKeysPresent: requiredIf('variables'),
|
2022-06-07 12:03:33 +00:00
|
|
|
allKeysRequired,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
processedParams: {},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
variables() {
|
|
|
|
const variables = this.templateString.match(/{{([^}]+)}}/g);
|
|
|
|
return variables;
|
|
|
|
},
|
|
|
|
templateString() {
|
|
|
|
return this.template.components.find(
|
|
|
|
component => component.type === 'BODY'
|
|
|
|
).text;
|
|
|
|
},
|
|
|
|
processedString() {
|
|
|
|
return this.templateString.replace(/{{([^}]+)}}/g, (match, variable) => {
|
|
|
|
const variableKey = this.processVariable(variable);
|
|
|
|
return this.processedParams[variableKey] || `{{${variable}}}`;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.generateVariables();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
sendMessage() {
|
|
|
|
this.$v.$touch();
|
2022-06-09 16:04:59 +00:00
|
|
|
if (this.$v.$invalid) return;
|
|
|
|
const payload = {
|
2022-06-07 12:03:33 +00:00
|
|
|
message: this.processedString,
|
|
|
|
templateParams: {
|
|
|
|
name: this.template.name,
|
|
|
|
category: this.template.category,
|
|
|
|
language: this.template.language,
|
|
|
|
namespace: this.template.namespace,
|
|
|
|
processed_params: this.processedParams,
|
|
|
|
},
|
|
|
|
};
|
2022-06-09 16:04:59 +00:00
|
|
|
this.$emit('sendMessage', payload);
|
2022-06-07 12:03:33 +00:00
|
|
|
},
|
|
|
|
processVariable(str) {
|
|
|
|
return str.replace(/{{|}}/g, '');
|
|
|
|
},
|
|
|
|
generateVariables() {
|
2022-06-09 16:04:59 +00:00
|
|
|
const matchedVariables = this.templateString.match(/{{([^}]+)}}/g);
|
|
|
|
if (!matchedVariables) return;
|
|
|
|
|
|
|
|
const variables = matchedVariables.map(i => this.processVariable(i));
|
2022-06-07 12:03:33 +00:00
|
|
|
this.processedParams = variables.reduce((acc, variable) => {
|
|
|
|
acc[variable] = '';
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
.template__variables-container {
|
|
|
|
padding: var(--space-one);
|
|
|
|
}
|
|
|
|
|
|
|
|
.variables-label {
|
|
|
|
font-size: var(--font-size-small);
|
|
|
|
font-weight: var(--font-weight-bold);
|
|
|
|
margin-bottom: var(--space-one);
|
|
|
|
}
|
|
|
|
|
|
|
|
.template__variable-item {
|
|
|
|
align-items: center;
|
|
|
|
display: flex;
|
|
|
|
margin-bottom: var(--space-one);
|
|
|
|
|
|
|
|
.label {
|
|
|
|
font-size: var(--font-size-mini);
|
|
|
|
}
|
|
|
|
|
|
|
|
.variable-input {
|
|
|
|
flex: 1;
|
|
|
|
font-size: var(--font-size-small);
|
|
|
|
margin-left: var(--space-one);
|
|
|
|
}
|
|
|
|
|
|
|
|
.variable-label {
|
|
|
|
background-color: var(--s-75);
|
|
|
|
border-radius: var(--border-radius-normal);
|
|
|
|
display: inline-block;
|
|
|
|
font-size: var(--font-size-mini);
|
|
|
|
padding: var(--space-one) var(--space-medium);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
footer {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
|
|
button {
|
|
|
|
margin-left: var(--space-one);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.error {
|
|
|
|
background-color: var(--r-100);
|
|
|
|
border-radius: var(--border-radius-normal);
|
|
|
|
color: var(--r-800);
|
|
|
|
padding: var(--space-one);
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
.template-input {
|
|
|
|
background-color: var(--s-25);
|
|
|
|
}
|
|
|
|
</style>
|