2021-02-15 18:44:13 +00:00
|
|
|
<template>
|
2022-04-19 07:17:29 +00:00
|
|
|
<FormulateForm
|
|
|
|
v-model="formValues"
|
2021-02-15 19:28:57 +00:00
|
|
|
class="flex flex-1 flex-col p-6 overflow-y-auto"
|
2022-04-19 07:17:29 +00:00
|
|
|
@submit="onSubmit"
|
2021-02-15 18:44:13 +00:00
|
|
|
>
|
2021-11-11 13:32:16 +00:00
|
|
|
<div
|
|
|
|
v-if="shouldShowHeaderMessage"
|
2022-04-19 07:17:29 +00:00
|
|
|
class="mb-4 text-sm leading-5"
|
2022-04-01 15:29:03 +00:00
|
|
|
:class="$dm('text-black-800', 'dark:text-slate-50')"
|
2021-11-11 13:32:16 +00:00
|
|
|
>
|
|
|
|
{{ headerMessage }}
|
2021-02-15 18:44:13 +00:00
|
|
|
</div>
|
2022-04-19 07:17:29 +00:00
|
|
|
<FormulateInput
|
|
|
|
v-for="item in enabledPreChatFields"
|
|
|
|
:key="item.name"
|
|
|
|
:name="item.name"
|
|
|
|
:type="item.type"
|
|
|
|
:label="getLabel(item)"
|
|
|
|
:placeholder="getPlaceHolder(item)"
|
|
|
|
:validation="getValidation(item)"
|
|
|
|
:options="getOptions(item)"
|
|
|
|
:label-class="context => labelClass(context)"
|
|
|
|
:input-class="context => inputClass(context)"
|
|
|
|
:validation-messages="{
|
|
|
|
isPhoneE164OrEmpty: $t('PRE_CHAT_FORM.FIELDS.PHONE_NUMBER.VALID_ERROR'),
|
|
|
|
email: $t('PRE_CHAT_FORM.FIELDS.EMAIL_ADDRESS.VALID_ERROR'),
|
|
|
|
required: getRequiredErrorMessage(item),
|
|
|
|
}"
|
2022-03-15 11:24:33 +00:00
|
|
|
/>
|
2022-04-19 07:17:29 +00:00
|
|
|
<FormulateInput
|
2022-01-12 10:55:27 +00:00
|
|
|
v-if="!hasActiveCampaign"
|
2022-04-19 07:17:29 +00:00
|
|
|
name="message"
|
|
|
|
type="textarea"
|
|
|
|
:label-class="context => labelClass(context)"
|
|
|
|
:input-class="context => inputClass(context)"
|
2021-02-15 18:44:13 +00:00
|
|
|
:label="$t('PRE_CHAT_FORM.FIELDS.MESSAGE.LABEL')"
|
|
|
|
:placeholder="$t('PRE_CHAT_FORM.FIELDS.MESSAGE.PLACEHOLDER')"
|
2022-04-19 07:17:29 +00:00
|
|
|
validation="required"
|
2021-02-15 18:44:13 +00:00
|
|
|
/>
|
2022-04-19 07:17:29 +00:00
|
|
|
|
2021-04-07 06:13:16 +00:00
|
|
|
<custom-button
|
2021-11-11 13:32:16 +00:00
|
|
|
class="font-medium my-5"
|
2021-02-15 18:44:13 +00:00
|
|
|
block
|
|
|
|
:bg-color="widgetColor"
|
|
|
|
:text-color="textColor"
|
|
|
|
:disabled="isCreating"
|
|
|
|
>
|
|
|
|
<spinner v-if="isCreating" class="p-0" />
|
|
|
|
{{ $t('START_CONVERSATION') }}
|
2021-04-07 06:13:16 +00:00
|
|
|
</custom-button>
|
2022-04-19 07:17:29 +00:00
|
|
|
</FormulateForm>
|
2021-02-15 18:44:13 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-04-07 06:13:16 +00:00
|
|
|
import CustomButton from 'shared/components/Button';
|
2021-02-15 18:44:13 +00:00
|
|
|
import Spinner from 'shared/components/Spinner';
|
|
|
|
import { mapGetters } from 'vuex';
|
2021-05-24 07:13:03 +00:00
|
|
|
import { getContrastingTextColor } from '@chatwoot/utils';
|
2022-04-19 07:17:29 +00:00
|
|
|
|
2021-11-11 13:32:16 +00:00
|
|
|
import { isEmptyObject } from 'widget/helpers/utils';
|
2022-01-12 10:55:27 +00:00
|
|
|
import routerMixin from 'widget/mixins/routerMixin';
|
2022-04-01 15:29:03 +00:00
|
|
|
import darkModeMixin from 'widget/mixins/darkModeMixin';
|
2021-02-15 18:44:13 +00:00
|
|
|
export default {
|
|
|
|
components: {
|
2021-04-07 06:13:16 +00:00
|
|
|
CustomButton,
|
2021-02-15 18:44:13 +00:00
|
|
|
Spinner,
|
|
|
|
},
|
2022-04-01 15:29:03 +00:00
|
|
|
mixins: [routerMixin, darkModeMixin],
|
2021-02-15 18:44:13 +00:00
|
|
|
props: {
|
|
|
|
options: {
|
|
|
|
type: Object,
|
2022-04-19 07:17:29 +00:00
|
|
|
default: () => {},
|
2021-02-15 18:44:13 +00:00
|
|
|
},
|
2022-03-03 07:06:31 +00:00
|
|
|
disableContactFields: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2021-02-15 18:44:13 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2022-04-19 07:17:29 +00:00
|
|
|
locale: this.$root.$i18n.locale,
|
2021-02-15 18:44:13 +00:00
|
|
|
message: '',
|
2022-04-19 07:17:29 +00:00
|
|
|
formValues: {},
|
|
|
|
labels: {
|
|
|
|
emailAddress: 'EMAIL_ADDRESS',
|
|
|
|
fullName: 'FULL_NAME',
|
|
|
|
phoneNumber: 'PHONE_NUMBER',
|
|
|
|
},
|
2021-02-15 18:44:13 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
widgetColor: 'appConfig/getWidgetColor',
|
|
|
|
isCreating: 'conversation/getIsCreating',
|
2021-11-11 13:32:16 +00:00
|
|
|
activeCampaign: 'campaign/getActiveCampaign',
|
2022-04-19 07:17:29 +00:00
|
|
|
currentUser: 'contacts/getCurrentUser',
|
2021-02-15 18:44:13 +00:00
|
|
|
}),
|
|
|
|
textColor() {
|
|
|
|
return getContrastingTextColor(this.widgetColor);
|
|
|
|
},
|
2022-01-12 10:55:27 +00:00
|
|
|
hasActiveCampaign() {
|
2021-11-11 13:32:16 +00:00
|
|
|
return !isEmptyObject(this.activeCampaign);
|
|
|
|
},
|
|
|
|
shouldShowHeaderMessage() {
|
2022-01-12 10:55:27 +00:00
|
|
|
return this.hasActiveCampaign || this.options.preChatMessage;
|
2021-11-11 13:32:16 +00:00
|
|
|
},
|
|
|
|
headerMessage() {
|
2022-01-12 10:55:27 +00:00
|
|
|
if (this.hasActiveCampaign) {
|
2021-11-11 13:32:16 +00:00
|
|
|
return this.$t('PRE_CHAT_FORM.CAMPAIGN_HEADER');
|
|
|
|
}
|
|
|
|
return this.options.preChatMessage;
|
|
|
|
},
|
2022-04-19 07:17:29 +00:00
|
|
|
preChatFields() {
|
|
|
|
return this.disableContactFields ? [] : this.options.preChatFields;
|
|
|
|
},
|
|
|
|
filteredPreChatFields() {
|
|
|
|
const isUserEmailAvailable = !!this.currentUser.email;
|
|
|
|
const isUserPhoneNumberAvailable = !!this.currentUser.phone_number;
|
|
|
|
return this.preChatFields.filter(field => {
|
|
|
|
if (
|
|
|
|
(isUserEmailAvailable && field.name === 'emailAddress') ||
|
|
|
|
(isUserPhoneNumberAvailable && field.name === 'phoneNumber')
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
enabledPreChatFields() {
|
|
|
|
return this.filteredPreChatFields
|
|
|
|
.filter(field => field.enabled)
|
|
|
|
.map(field => ({
|
|
|
|
...field,
|
|
|
|
type: this.findFieldType(field.type),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
conversationCustomAttributes() {
|
|
|
|
let conversationAttributes = {};
|
|
|
|
this.enabledPreChatFields.forEach(field => {
|
|
|
|
if (field.field_type === 'conversation_attribute') {
|
|
|
|
conversationAttributes = {
|
|
|
|
...conversationAttributes,
|
|
|
|
[field.name]: this.getValue(field),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return conversationAttributes;
|
|
|
|
},
|
|
|
|
contactCustomAttributes() {
|
|
|
|
let contactAttributes = {};
|
|
|
|
this.enabledPreChatFields.forEach(field => {
|
|
|
|
if (field.field_type === 'contact_attribute') {
|
|
|
|
contactAttributes = {
|
|
|
|
...contactAttributes,
|
|
|
|
[field.name]: this.getValue(field),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return contactAttributes;
|
|
|
|
},
|
|
|
|
inputStyles() {
|
|
|
|
return `mt-2 border rounded w-full py-2 px-3 text-slate-700 outline-none`;
|
|
|
|
},
|
|
|
|
isInputDarkOrLightMode() {
|
|
|
|
return `${this.$dm('bg-white', 'dark:bg-slate-600')} ${this.$dm(
|
|
|
|
'text-slate-700',
|
|
|
|
'dark:text-slate-50'
|
|
|
|
)}`;
|
|
|
|
},
|
|
|
|
inputBorderColor() {
|
|
|
|
return `${this.$dm('border-black-200', 'dark:border-black-500')}`;
|
2022-03-03 07:06:31 +00:00
|
|
|
},
|
2021-02-15 18:44:13 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2022-04-19 07:17:29 +00:00
|
|
|
labelClass(context) {
|
|
|
|
const { hasErrors } = context;
|
|
|
|
if (!hasErrors) {
|
|
|
|
return `text-xs font-medium ${this.$dm(
|
|
|
|
'text-black-800',
|
|
|
|
'dark:text-slate-50'
|
|
|
|
)}`;
|
|
|
|
}
|
|
|
|
return `text-xs font-medium ${this.$dm(
|
|
|
|
'text-red-400',
|
|
|
|
'dark:text-red-400'
|
|
|
|
)}`;
|
|
|
|
},
|
|
|
|
inputClass(context) {
|
|
|
|
const { hasErrors, classification, type } = context;
|
|
|
|
if (classification === 'box' && type === 'checkbox') {
|
|
|
|
return '';
|
2021-02-15 18:44:13 +00:00
|
|
|
}
|
2022-04-19 07:17:29 +00:00
|
|
|
if (!hasErrors) {
|
|
|
|
return `${this.inputStyles} hover:border-black-300 focus:border-black-300 ${this.isInputDarkOrLightMode} ${this.inputBorderColor}`;
|
|
|
|
}
|
|
|
|
return `${this.inputStyles} border-red-200 hover:border-red-300 focus:border-red-300 ${this.isInputDarkOrLightMode}`;
|
|
|
|
},
|
|
|
|
isContactFieldRequired(field) {
|
|
|
|
return this.preChatFields.find(option => option.name === field).required;
|
|
|
|
},
|
|
|
|
getLabel({ name, label }) {
|
|
|
|
if (this.labels[name])
|
|
|
|
return this.$t(`PRE_CHAT_FORM.FIELDS.${this.labels[name]}.LABEL`);
|
|
|
|
return label;
|
|
|
|
},
|
|
|
|
getPlaceHolder({ name, placeholder }) {
|
|
|
|
if (this.labels[name])
|
|
|
|
return this.$t(`PRE_CHAT_FORM.FIELDS.${this.labels[name]}.PLACEHOLDER`);
|
|
|
|
return placeholder;
|
|
|
|
},
|
|
|
|
getValue({ name, type }) {
|
|
|
|
if (type === 'select') {
|
|
|
|
return this.enabledPreChatFields.find(option => option.name === name)
|
|
|
|
.values[this.formValues[name]];
|
|
|
|
}
|
|
|
|
return this.formValues[name] || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getRequiredErrorMessage({ name, label }) {
|
|
|
|
if (this.labels[name])
|
|
|
|
return this.$t(
|
|
|
|
`PRE_CHAT_FORM.FIELDS.${this.labels[name]}.REQUIRED_ERROR`
|
|
|
|
);
|
|
|
|
return `${label} ${this.$t('PRE_CHAT_FORM.IS_REQUIRED')}`;
|
|
|
|
},
|
|
|
|
getValidation({ type, name }) {
|
|
|
|
if (!this.isContactFieldRequired(name)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const validations = {
|
|
|
|
emailAddress: 'email',
|
|
|
|
phoneNumber: 'isPhoneE164OrEmpty',
|
|
|
|
url: 'url',
|
|
|
|
date: 'date',
|
|
|
|
text: null,
|
|
|
|
select: null,
|
|
|
|
number: null,
|
|
|
|
};
|
|
|
|
const validationKeys = Object.keys(validations);
|
|
|
|
const validation = 'bail|required';
|
|
|
|
if (validationKeys.includes(name) || validationKeys.includes(type)) {
|
|
|
|
const validationType = validations[type] || validations[name];
|
|
|
|
return validationType ? `${validation}|${validationType}` : validation;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
findFieldType(type) {
|
|
|
|
if (type === 'link') {
|
|
|
|
return 'url';
|
|
|
|
}
|
|
|
|
if (type === 'list') {
|
|
|
|
return 'select';
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
},
|
|
|
|
getOptions(item) {
|
|
|
|
if (item.type === 'select') {
|
|
|
|
let values = {};
|
|
|
|
item.values.forEach((value, index) => {
|
|
|
|
values = {
|
|
|
|
...values,
|
|
|
|
[index]: value,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
onSubmit() {
|
|
|
|
const { emailAddress, fullName, phoneNumber, message } = this.formValues;
|
|
|
|
const { email } = this.currentUser;
|
2022-01-12 10:55:27 +00:00
|
|
|
this.$emit('submit', {
|
2022-04-19 07:17:29 +00:00
|
|
|
fullName,
|
|
|
|
phoneNumber,
|
|
|
|
emailAddress: emailAddress || email,
|
|
|
|
message,
|
2022-01-12 10:55:27 +00:00
|
|
|
activeCampaignId: this.activeCampaign.id,
|
2022-04-19 07:17:29 +00:00
|
|
|
conversationCustomAttributes: this.conversationCustomAttributes,
|
|
|
|
contactCustomAttributes: this.contactCustomAttributes,
|
2022-01-12 10:55:27 +00:00
|
|
|
});
|
2021-02-15 18:44:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
2022-04-19 07:17:29 +00:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
::v-deep {
|
|
|
|
.wrapper[data-type='checkbox'] {
|
|
|
|
.formulate-input-wrapper {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
label {
|
|
|
|
margin-left: 0.2rem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media (prefers-color-scheme: dark) {
|
|
|
|
.wrapper {
|
|
|
|
.formulate-input-element--date,
|
|
|
|
.formulate-input-element--checkbox {
|
|
|
|
input {
|
|
|
|
color-scheme: dark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.wrapper[data-type='textarea'] {
|
|
|
|
.formulate-input-element--textarea {
|
|
|
|
textarea {
|
|
|
|
min-height: 8rem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|