All files / src/flow/actions add_input_labels.ts

75.38% Statements 49/65
80% Branches 4/5
75% Functions 3/4
75.38% Lines 49/65

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 66132x 1x 1x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 4x 1x 1x 1x 1x 4x 4x 132x 132x 12x 12x 12x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x                                 132x 132x 132x  
import { html } from 'lit-html';
import { ActionConfig, ACTION_GROUPS, FormData, FlowTypes } from '../types';
import { Node, AddInputLabels } from '../../store/flow-definition';
import { renderNamedObjects } from '../utils';
 
export const add_input_labels: ActionConfig = {
  name: 'Add Input Labels',
  group: ACTION_GROUPS.save,
  // Not allowed in background flows: goflow treats add_input_labels as an interactive
  // action (messaging, messaging_offline, voice only), so offering it in a
  // messaging_background flow produces a definition the backend rejects.
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE],
  render: (_node: Node, action: AddInputLabels) => {
    return html`<div>${renderNamedObjects(action.labels, 'label')}</div>`;
  },
 
  // Form-level transformations
  toFormData: (action: AddInputLabels) => {
    return {
      labels: action.labels || null,
      uuid: action.uuid
    };
  },
  form: {
    labels: {
      type: 'select',
      label: 'Labels',
      helpText:
        'Select labels to add to the input. Type a new label name to create it.',
      required: true,
      options: [],
      multi: true,
      searchable: true,
      endpoint: '/api/v2/labels.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for labels or type to create new ones...',
      allowCreate: true,
      createArbitraryOption: (input: string, options: any[]) => {
        // Check if a label with this name already exists
        const existing = options.find(
          (option) =>
            option.name.toLowerCase().trim() === input.toLowerCase().trim()
        );
        if (!existing && input.trim()) {
          return {
            name: input.trim(),
            arbitrary: true
          };
        }
        return null;
      }
    }
  },
  fromFormData: (formData: FormData): AddInputLabels => {
    return {
      uuid: formData.uuid,
      type: 'add_input_labels',
      labels: (formData.labels || []).map((l: any) => ({
        uuid: l.uuid,
        name: l.name
      }))
    };
  }
};