All files / src/flow/actions set_contact_field.ts

97.29% Statements 72/74
87.5% Branches 7/8
75% Functions 3/4
97.29% Lines 72/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75133x 2x 2x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 4x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x     133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 8x 8x 8x 8x 8x 133x 133x 133x 133x 133x  
import { html } from 'lit-html';
import { ActionConfig, ACTION_GROUPS, FormData, FlowTypes } from '../types';
import { Node, SetContactField } from '../../store/flow-definition';
import { renderClamped, renderHighlightedText } from '../utils';
 
export const set_contact_field: ActionConfig = {
  name: 'Update Field',
  group: ACTION_GROUPS.contacts,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: SetContactField) => {
    if (action.value) {
      return renderClamped(
        html`Set <strong>${action.field.name}</strong> to
          ${renderHighlightedText(action.value, true)}`,
        `Set ${action.field.name} to ${action.value}`
      );
    } else {
      return renderClamped(
        html`Clear <strong>${action.field.name}</strong>`,
        `Clear ${action.field.name}`
      );
    }
  },
  form: {
    field: {
      type: 'select',
      label: 'Field',
      required: true,
      searchable: true,
      clearable: false,
      placeholder: 'Search for contact fields...',
      nameKey: 'name',
      valueKey: 'key',
      endpoint: '/api/v2/fields.json',
      helpText: 'Select the contact field to update',
      allowCreate: true,
      createArbitraryOption: (input: string) => ({
        key: input,
        name: input,
        type: 'text'
      })
    },
    value: {
      type: 'textarea',
      label: 'Value',
      placeholder: 'Enter field value...',
      evaluated: true,
      maxLength: 10000,
      helpText:
        'The new value for the contact field. You can use expressions like @contact.name'
    }
  },
  toFormData: (action: SetContactField) => {
    return {
      uuid: action.uuid,
      field: action.field ? [action.field] : null,
      value: action.value
    };
  },
  fromFormData: (formData: FormData): SetContactField => {
    const field = formData.field[0];
    return {
      uuid: formData.uuid,
      type: 'set_contact_field',
      field: { name: field.name, key: field.key },
      value: formData.value
    };
  },
  sanitize: (formData: FormData): void => {
    if (formData.value && typeof formData.value === 'string') {
      formData.value = formData.value.trim();
    }
  }
};