mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-03 20:48:07 +00:00 
			
		
		
		
	This allows a user to add/update a custom regex and a cue while defining custom attributes(Only applicable for type- text). While adding/editing custom attributes, the values are validated against the attribute definition regex, and if it is incorrect, a cue message or default error message is shown and restricts invalid values from being saved. Fixes: #6866
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import i18n from 'widget/i18n/index';
 | 
						|
const defaultTranslations = Object.fromEntries(
 | 
						|
  Object.entries(i18n).filter(([key]) => key.includes('en'))
 | 
						|
).en;
 | 
						|
 | 
						|
export const standardFieldKeys = {
 | 
						|
  emailAddress: {
 | 
						|
    key: 'EMAIL_ADDRESS',
 | 
						|
    label: 'Email Id',
 | 
						|
    placeholder: 'Please enter your email address',
 | 
						|
  },
 | 
						|
  fullName: {
 | 
						|
    key: 'FULL_NAME',
 | 
						|
    label: 'Full Name',
 | 
						|
    placeholder: 'Please enter your full name',
 | 
						|
  },
 | 
						|
  phoneNumber: {
 | 
						|
    key: 'PHONE_NUMBER',
 | 
						|
    label: 'Phone Number',
 | 
						|
    placeholder: 'Please enter your phone number',
 | 
						|
  },
 | 
						|
};
 | 
						|
 | 
						|
export const getLabel = ({ key, label }) => {
 | 
						|
  return defaultTranslations.PRE_CHAT_FORM.FIELDS[key]
 | 
						|
    ? defaultTranslations.PRE_CHAT_FORM.FIELDS[key].LABEL
 | 
						|
    : label;
 | 
						|
};
 | 
						|
export const getPlaceHolder = ({ key, placeholder }) => {
 | 
						|
  return defaultTranslations.PRE_CHAT_FORM.FIELDS[key]
 | 
						|
    ? defaultTranslations.PRE_CHAT_FORM.FIELDS[key].PLACEHOLDER
 | 
						|
    : placeholder;
 | 
						|
};
 | 
						|
 | 
						|
export const getCustomFields = ({ standardFields, customAttributes }) => {
 | 
						|
  let customFields = [];
 | 
						|
  const { pre_chat_fields: preChatFields } = standardFields;
 | 
						|
  customAttributes.forEach(attribute => {
 | 
						|
    const itemExist = preChatFields.find(
 | 
						|
      item => item.name === attribute.attribute_key
 | 
						|
    );
 | 
						|
    if (!itemExist) {
 | 
						|
      customFields.push({
 | 
						|
        label: attribute.attribute_display_name,
 | 
						|
        placeholder: attribute.attribute_display_name,
 | 
						|
        name: attribute.attribute_key,
 | 
						|
        type: attribute.attribute_display_type,
 | 
						|
        values: attribute.attribute_values,
 | 
						|
        field_type: attribute.attribute_model,
 | 
						|
        regex_pattern: attribute.regex_pattern,
 | 
						|
        regex_cue: attribute.regex_cue,
 | 
						|
        required: false,
 | 
						|
        enabled: false,
 | 
						|
      });
 | 
						|
    }
 | 
						|
  });
 | 
						|
  return customFields;
 | 
						|
};
 | 
						|
 | 
						|
export const getFormattedPreChatFields = ({ preChatFields }) => {
 | 
						|
  return preChatFields.map(item => {
 | 
						|
    return {
 | 
						|
      ...item,
 | 
						|
      label: getLabel({
 | 
						|
        key: item.name,
 | 
						|
        label: item.label ? item.label : item.name,
 | 
						|
      }),
 | 
						|
      placeholder: getPlaceHolder({
 | 
						|
        key: item.name,
 | 
						|
        placeholder: item.placeholder ? item.placeholder : item.name,
 | 
						|
      }),
 | 
						|
    };
 | 
						|
  });
 | 
						|
};
 | 
						|
 | 
						|
export const getPreChatFields = ({
 | 
						|
  preChatFormOptions = {},
 | 
						|
  customAttributes = [],
 | 
						|
}) => {
 | 
						|
  const { pre_chat_message, pre_chat_fields } = preChatFormOptions;
 | 
						|
  let customFields = {};
 | 
						|
  let preChatFields = {};
 | 
						|
 | 
						|
  const formattedPreChatFields = getFormattedPreChatFields({
 | 
						|
    preChatFields: pre_chat_fields,
 | 
						|
  });
 | 
						|
 | 
						|
  customFields = getCustomFields({
 | 
						|
    standardFields: { pre_chat_fields: formattedPreChatFields },
 | 
						|
    customAttributes,
 | 
						|
  });
 | 
						|
  preChatFields = [...formattedPreChatFields, ...customFields];
 | 
						|
 | 
						|
  return {
 | 
						|
    pre_chat_message,
 | 
						|
    pre_chat_fields: preChatFields,
 | 
						|
  };
 | 
						|
};
 |