All files / src/flow/actions request_optin.ts

72.58% Statements 45/62
50% Branches 1/2
33.33% Functions 1/3
72.58% Lines 45/62

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 6398x     98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x                               98x 1x 1x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x  
import { html } from 'lit-html';
import { ActionConfig, ACTION_GROUPS, FormData } from '../types';
import { Node, RequestOptin } from '../../store/flow-definition';
import { renderClamped } from '../utils';
 
export const request_optin: ActionConfig = {
  name: 'Request Opt-In',
  group: ACTION_GROUPS.send,
  flowTypes: [],
  render: (_node: Node, action: RequestOptin) => {
    const optinName = action.optin?.name || 'Unknown opt-in';
    return renderClamped(
      html`Request <strong>${optinName}</strong>`,
      `Request ${optinName}`
    );
  },
  toFormData: (action: RequestOptin) => {
    return {
      uuid: action.uuid,
      optin: action.optin ? [action.optin] : null
    };
  },
  form: {
    optin: {
      type: 'select',
      label: 'Opt-In',
      required: true,
      searchable: true,
      endpoint: '/api/v2/optins.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for opt-ins or type to create one...',
      helpText:
        'Select an existing opt-in to request, or type a name to create a new one.',
      allowCreate: true,
      createArbitraryOption: (input: string, options: any[]) => {
        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): RequestOptin => {
    const optin = formData.optin?.[0];
    return {
      uuid: formData.uuid,
      type: 'request_optin',
      optin: {
        uuid: optin.uuid || optin.value,
        name: optin.name
      }
    };
  }
};