/** * Custom JavaScript of a contact step (node.tracking): when the step appears, and after sending, * as the thank-you page appears or before the redirect, the place for a conversion pixel. In Free * and Pro; Pro adds code after sending as an action (customCode, with placeholders and * conditions), so there the contact step shows its own after-sending code only while it has one, * as forms from 3.x bring it. The plugin sends the code to visitors only when the site allows it * (TrackingCode), the runtime runs it once per visit and only with consent to marketing, and * saving changed code needs the right to use unfiltered HTML (SensitiveChanges). */ import React from 'react'; import type { FormDocument, FormNode } from '@funnelforms/form-schema'; import { findNode } from '../document'; import { codeSetting } from '../code'; import { euPrivacy } from '../privacy'; import { rightName } from './catalog'; import { useEnv } from './context'; import { __ } from './i18n'; import { Field, Notice, Section } from './ui'; type ContactNode = Extract; type Moment = 'onShow' | 'afterSubmit'; export function ContactCode( { node }: { node: ContactNode } ) { const { store, rights, readOnly } = useEnv(); const change = store.getState().change; const edit = ( moment: Moment ) => ( value: string ) => change( ( d ) => { const n = findNode( d, node.id ); if ( ! n || 'contact' !== n.type ) { return; } const tracking = Object.fromEntries( Object.entries( { ...n.tracking, [ moment ]: value } ).filter( ( [ , v ] ) => Boolean( v ) ) ); if ( Object.keys( tracking ).length ) { n.tracking = tracking; } else { delete n.tracking; } }, `${ node.id }:tracking.${ moment }` ); const code = ( moment: Moment, label: string, hint: string ) => ( { ( id ) => (