/** * The "CRM or newsletter" action: which tool and connection receive the entry, and which field of * the contact step goes where. The choices a tool offers (lists, tags, pipelines, deal stages) are * read from the account through the connection, as Funnelforms 3 did while editing. */ import React, { useCallback, useEffect, useMemo, useState } from 'react'; import type { Action, FormDocument } from '@funnelforms/form-schema'; import { choicesFor, contactFieldName, contactFields, contactToken, mappingGaps, suggestMapping, type CrmChoice, type CrmConnection, type CrmMappingField, type CrmOptions, type CrmProvider } from '../connect'; import { useEnv } from './context'; import { __, sprintf } from './i18n'; import { CrmConnectionDialog, providerOptions, useCrmCatalog } from './CrmConnections'; import { Button, Dialog, icons, Notice, Select, Spinner, TextInput } from './ui'; type CrmActionOf = Extract; type Choices = { state: 'idle' } | { state: 'loading' } | { state: 'ready'; data: CrmOptions } | { state: 'failed'; text: string }; /** What the account of a connection offers; read again when the connection changes. */ function useChoices( provider: CrmProvider | undefined, ref: string, known: boolean ): [ Choices, () => void, React.Dispatch> ] { const { api } = useEnv(); const [ choices, setChoices ] = useState( { state: 'idle' } ); const wanted = Boolean( api.connect && provider?.lookups && ref && known && provider.mapping.some( ( m ) => 'choice' === m.kind ) ); const [ tick, setTick ] = useState( 0 ); useEffect( () => { if ( ! wanted || ! api.connect ) { setChoices( { state: 'idle' } ); return undefined; } let live = true; setChoices( { state: 'loading' } ); api.connect.crmOptions( ref ).then( ( data ) => live && setChoices( { state: 'ready', data } ), ( e: unknown ) => live && setChoices( { state: 'failed', text: e instanceof Error ? e.message : String( e ) } ) ); return () => { live = false; }; }, [ api, ref, wanted, tick ] ); return [ choices, useCallback( () => setTick( ( n ) => n + 1 ), [] ), setChoices ]; } export function CrmAction( { doc, action, common }: { doc: FormDocument; action: CrmActionOf; common: React.ReactNode } ) { const { api, rights, store, readOnly } = useEnv(); const change = store.getState().change; const catalog = useCrmCatalog( api.connect ); const [ adding, setAdding ] = useState( false ); const locale = doc.locales.default; const { provider, connectionRef, mapping } = action.config; const spec = catalog.providers?.find( ( p ) => p.provider === provider ); const own: CrmConnection[] = useMemo( () => ( catalog.connections ?? [] ).filter( ( c ) => c.provider === provider ), [ catalog.connections, provider ] ); const known = own.some( ( c ) => c.ref === connectionRef ); const fields = useMemo( () => contactFields( doc, action.node ), [ doc, action.node ] ); const [ choices, retry, setChoices ] = useChoices( spec, connectionRef, known ); const [ sheet, setSheet ] = useState( false ); const edit = ( recipe: ( config: CrmActionOf[ 'config' ] ) => void, key?: string ): void => change( ( d ) => { const a = d.actions.find( ( x ) => x.id === action.id ); if ( a && 'crm' === a.kind ) { recipe( a.config ); } }, key ? `action:${ action.id }:${ key }` : undefined ); const pickProvider = ( next: string ): void => { const target = catalog.providers?.find( ( p ) => p.provider === next ); const only = ( catalog.connections ?? [] ).filter( ( c ) => c.provider === next ); edit( ( config ) => { config.provider = next; config.connectionRef = 1 === only.length ? only[ 0 ]?.ref ?? '' : ''; config.mapping = target ? suggestMapping( target, fields ) : {}; } ); }; const setValue = ( name: string, value: string ): void => edit( ( config ) => { // A value that depends on this one (deal stage on pipeline) no longer fits. const dropped = new Set( [ ...( '' === value ? [ name ] : [] ), ...( spec?.mapping ?? [] ).filter( ( m ) => m.dependsOn === name ).map( ( m ) => m.name ) ] ); config.mapping = Object.fromEntries( Object.entries( { ...config.mapping, [ name ]: value } ).filter( ( [ k ] ) => ! dropped.has( k ) ) ); }, `crm:${ name }` ); if ( ! api.connect ) { return ( <>

{ __( 'This connection comes from Funnelforms 3 and keeps working as it is. Its settings are not edited here yet.', 'funnelforms-pro' ) }

{ common } ); } if ( catalog.error ) { return ( <> void catalog.reload() }>{ __( 'Try again', 'funnelforms-pro' ) } }> { catalog.error } { common } ); } if ( ! catalog.providers || ! catalog.connections ) { return ; } const connectionOptions = [ { value: '', label: __( 'Choose…', 'funnelforms-pro' ) }, ...own.map( ( c ) => ( { value: c.ref, label: c.legacy ? `${ c.label } (${ __( 'From Funnelforms 3', 'funnelforms-pro' ) })` : c.label } ) ), ...( connectionRef && ! known ? [ { value: connectionRef, label: __( 'Deleted connection', 'funnelforms-pro' ) } ] : [] ), ]; const gaps = spec ? mappingGaps( spec, mapping ) : []; const fieldInput = ( item: CrmMappingField ) => { const current = 'string' === typeof mapping[ item.name ] ? ( mapping[ item.name ] as string ) : ''; const label = item.required ? sprintf( __( '%s (required)', 'funnelforms-pro' ), item.label ) : item.label; if ( 'contact' === item.kind ) { const options = [ { value: '', label: __( 'Not sent', 'funnelforms-pro' ) }, ...fields.map( ( f ) => ( { value: contactToken( f.key ), label: contactFieldName( f, locale ) } ) ) ]; if ( current && ! options.some( ( o ) => o.value === current ) ) { options.push( { value: current, label: sprintf( __( 'Unknown field %s', 'funnelforms-pro' ), current ) } ); } return undefined } /> ) : ( setValue( item.name, v.trim() ) } /> ); } const waiting = Boolean( item.dependsOn ) && '' === ( mapping[ item.dependsOn ?? '' ] ?? '' ); const options = [ { value: '', label: waiting ? __( 'Choose the value above first', 'funnelforms-pro' ) : __( 'Choose…', 'funnelforms-pro' ) }, ...list.map( ( c ) => ( { value: c.value, label: c.label } ) ) ]; if ( current && ! options.some( ( o ) => o.value === current ) ) { options.push( { value: current, label: sprintf( __( 'Current value (%s)', 'funnelforms-pro' ), current ) } ); } const select = { spec ? ( <>