/** * Settings of an end, what visitors see: a thank-you message (heading, text, picture, animation, * the visitor's answers, downloads, a button), a redirect to a page of the site or any address * with answers passed along, or the message first and the redirect after a wait. */ import React, { useEffect, useState } from 'react'; import type { EndSuccess, FormNode, Text } from '@funnelforms/form-schema'; import type { SitePage } from '../api'; import { findNode, text } from '../document'; import { editText, setText } from '../languages'; import { AnswerInsert } from './AnswerInsert'; import { useEnv } from './context'; import { __, sprintf } from './i18n'; import { TextProp } from './texts'; import { Button, Dialog, IconButton, icons, Notice, NumberInput, ProBadge, Select, TextInput, Toggle } from './ui'; type EndNode = Extract; type Mode = 'message' | 'redirect' | 'both'; export function EndProps( { node }: { node: EndNode } ) { const { api, ctx, lang, readOnly, store } = useEnv(); const [ picking, setPicking ] = useState( false ); const end = ( recipe: ( n: EndNode ) => void, key?: string ): void => store.getState().change( ( d ) => { const n = findNode( d, node.id ); if ( n && 'end' === n.type ) { recipe( n ); } }, key ); const success = ( recipe: ( s: EndSuccess ) => void, key?: string ): void => end( ( n ) => { if ( n.success ) { recipe( n.success ); } }, key ); /** Writes the edited language of a text, creating the text first. */ const write = ( get: ( s: EndSuccess ) => Text | undefined, create: ( s: EndSuccess ) => Text, key: string ) => ( value: string ): void => success( ( s ) => setText( get( s ) ?? create( s ), lang, value, ctx.locale ), `${ node.id }:${ key }` ); const s = node.success; const pickFile = api.pickFile?.bind( api ); const pickImage = api.pickImage?.bind( api ); const mode: Mode | null = s?.show ? ( node.redirect ? 'both' : 'message' ) : node.redirect ? 'redirect' : null; const setMode = ( next: Mode ): void => end( ( n ) => { const show = 'redirect' !== next; n.success = n.success ? { ...n.success, show } : { show, text: { [ ctx.locale ]: '' } }; if ( 'message' === next ) { n.redirect = null; return; } if ( ! n.redirect ) { n.redirect = { url: { [ ctx.locale ]: '' }, newTab: false, delayMs: 0, params: [] }; } if ( 'both' === next && n.redirect.delayMs < 1000 ) { // Time to read the message, with the seconds shown and a link to go on at once. n.redirect.delayMs = 5000; n.redirect.countdown = true; } if ( 'redirect' === next ) { n.redirect.delayMs = 0; delete n.redirect.countdown; } } ); const modes: Array<[ Mode, string ]> = [ [ 'message', __( 'Message', 'funnelforms-pro' ) ], [ 'redirect', __( 'Redirect', 'funnelforms-pro' ) ], [ 'both', __( 'Both', 'funnelforms-pro' ) ], ]; return ( <>

{ __( 'What visitors see', 'funnelforms-pro' ) }

{ modes.map( ( [ id, label ] ) => ( ) ) }

{ 'both' === mode ? __( 'The message first, then the page after the wait set below.', 'funnelforms-pro' ) : 'redirect' === mode ? __( 'Visitors go straight to the page.', 'funnelforms-pro' ) : 'message' === mode ? __( 'A thank-you message in place of the form.', 'funnelforms-pro' ) : __( 'Choose what visitors see at this end.', 'funnelforms-pro' ) }

{ s?.show ? ( <> x.heading, ( x ) => ( x.heading = {} ), 'heading' ) } /> x.text, ( x ) => ( x.text = {} ), 'success' ) } /> write( ( x ) => x.text, ( x ) => ( x.text = {} ), 'success' )( `${ editText( s.text, lang, ctx.locale ) } ${ token }`.trim() ) } />

{ __( 'Picture', 'funnelforms-pro' ) }

{ s.image ? : null }
{ pickImage ? ( ) : null } { s.image ? ( ) : null }
{ pickImage ? null : success( ( x ) => { if ( v.trim() ) { x.image = v.trim(); } else { delete x.image; } }, `${ node.id }:image` ) } /> }