All files / temba-components/src/flow/actions remove_contact_groups.ts

100% Statements 66/66
71.42% Branches 5/7
100% Functions 5/5
100% Lines 66/66

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 6760x 60x 60x 60x 60x 60x 60x 60x 60x 1x 1x 1x 1x 1x 60x 1x 1x 1x 1x 1x 1x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 1x 1x 1x 1x 1x 1x 1x 60x  
import { html } from 'lit-html';
import { ActionConfig, COLORS, ValidationResult } from '../types';
import { Node, RemoveFromGroup } from '../../store/flow-definition';
import { renderNamedObjects } from '../utils';
 
export const remove_contact_groups: ActionConfig = {
  name: 'Remove from Group',
  color: COLORS.update,
  render: (_node: Node, action: RemoveFromGroup) => {
    if (action.all_groups) {
      return html`<div>Remove from all groups</div>`;
    }
    return html`<div>${renderNamedObjects(action.groups, 'group')}</div>`;
  },
  toFormData: (action: RemoveFromGroup) => {
    return {
      uuid: action.uuid,
      all_groups: action.all_groups || false,
      groups: action.groups || []
    };
  },
  form: {
    groups: {
      type: 'select',
      label: 'Groups',
      helpText: 'Select the groups to remove the contact from',
      options: [],
      multi: true,
      searchable: true,
      endpoint: '/api/v2/groups.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for groups...',
      conditions: {
        visible: (formData) => !formData.all_groups
      }
    },
    all_groups: {
      type: 'checkbox',
      label: 'Remove from All Groups',
      helpText:
        'Check this to remove the contact from all groups instead of specific ones'
    }
  },
  validate: (action: RemoveFromGroup): ValidationResult => {
    const errors: { [key: string]: string } = {};
 
    if (!action.all_groups && (!action.groups || action.groups.length === 0)) {
      errors.groups =
        'At least one group must be selected or check "Remove from All Groups"';
    }
 
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  },
  fromFormData: (formData: any): RemoveFromGroup => {
    return {
      uuid: formData.uuid,
      type: 'remove_contact_groups',
      groups: formData.all_groups ? [] : formData.groups || [],
      all_groups: formData.all_groups || false
    };
  }
};