All files / src/flow/actions set_contact_language.ts

83.72% Statements 72/86
50% Branches 5/10
100% Functions 4/4
83.72% Lines 72/86

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 75 76 77 78 79 80 81 82 83 84 85 86 87132x 1x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 4x 4x 4x 4x 4x 4x 1x 4x 4x 132x 132x 132x 132x 132x 4x 4x     4x 4x 4x 4x 4x   4x                 4x 4x 4x 132x 132x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x       132x 132x 132x 132x 132x 132x 132x 132x  
import { html } from 'lit-html';
import {
  ActionConfig,
  ACTION_GROUPS,
  FormData,
  ValidationResult,
  FlowTypes
} from '../types';
import { Node, SetContactLanguage } from '../../store/flow-definition';
import { getStore } from '../../store/Store';
import { renderClamped } from '../utils';
import { getLanguageName } from '../../languages';
 
export const set_contact_language: ActionConfig = {
  name: 'Update Language',
  group: ACTION_GROUPS.contacts,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: SetContactLanguage) => {
    const name = getLanguageName(action.language);
    return renderClamped(
      html`Set to <strong>${name}</strong>`,
      `Set to ${name}`
    );
  },
  form: {
    language: {
      type: 'select',
      label: 'Language',
      required: true,
      searchable: true,
      clearable: false,
      valueKey: 'value',
      nameKey: 'name',
      helpText: 'Select the language to set for the contact',
      getDynamicOptions: () => {
        const store = getStore();
        const workspace = store?.getState().workspace;
        if (workspace?.languages && Array.isArray(workspace.languages)) {
          return workspace.languages.map((languageCode: string) => ({
            value: languageCode,
            name: getLanguageName(languageCode)
          }));
        }
        return [];
      }
    }
  },
  toFormData: (action: SetContactLanguage) => {
    // Convert the language code back to the option object format expected by the form
    if (action.language) {
      return {
        language: [
          {
            value: action.language,
            name: getLanguageName(action.language)
          }
        ],
        uuid: action.uuid
      };
    }
    return {
      language: null,
      uuid: action.uuid
    };
  },
  fromFormData: (formData: FormData): SetContactLanguage => {
    return {
      uuid: formData.uuid,
      type: 'set_contact_language',
      language: formData.language[0].value
    };
  },
 
  validate: (formData: FormData): ValidationResult => {
    const errors: { [key: string]: string } = {};

    if (!formData.language) {
      errors.language = 'Language is required';
    }
 
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  }
};