/** * Phone fields: the country picker (start country, countries on top, countries accepted) and, in * contact steps, the confirmation by text message (Funnelforms Pro) with its SMS connection. * The country data is the runtime's (plugin/assets/phone/countries.json). */ import React, { useEffect, useMemo, useState } from 'react'; import type { PhoneCountries, PhoneSms, Text } from '@funnelforms/form-schema'; import { localeTag } from '@funnelforms/form-core'; import data from '../../../../plugin/assets/phone/countries.json'; import type { Connection } from '../api'; import { CONNECTION_TYPES } from '../actions'; import { ConnectionPicker } from './Actions'; import { Icon } from './Frame'; import { useEnv } from './context'; import { put, TextProp } from './texts'; import { __, sprintf } from './i18n'; import { Notice, ProBadge, Select, TextInput, Toggle } from './ui'; type Props = Record & { country?: PhoneCountries; sms?: PhoneSms }; type Edit = ( recipe: ( props: Props ) => void, key?: string ) => void; const CODES = ( data as unknown as { countries: Record } ).countries; /** Names of the countries in the language of the admin, sorted. */ export function useCountries(): Array<{ iso: string; name: string; code: string }> { return useMemo( () => { const lang = localeTag( document.documentElement.lang ) ?? 'en'; let names: Intl.DisplayNames | null = null; try { names = new Intl.DisplayNames( [ lang, 'en' ], { type: 'region' } ); } catch { names = null; } return Object.keys( CODES ) .map( ( iso ) => ( { iso, name: names?.of( iso ) ?? iso, code: CODES[ iso ]?.[ 0 ] ?? '' } ) ) .sort( ( a, b ) => a.name.localeCompare( b.name, lang ) ); }, [] ); } /** Countries as removable chips and a list to add one. */ function CountryList( { label, hint, value, onChange }: { label: string; hint: string; value: string[]; onChange: ( value: string[] ) => void } ) { const { readOnly } = useEnv(); const countries = useCountries(); const name = ( iso: string ): string => countries.find( ( c ) => c.iso === iso )?.name ?? iso; return (
! allowed.length || allowed.includes( c.iso ) ).map( ( c ) => ( { value: c.iso, label: `${ c.name } +${ c.code }` } ) ) ] } onChange={ ( v ) => set( 'default', v ) } /> set( 'preferred', v ) } /> set( 'allowed', v ) } /> { picker?.default && allowed.length && ! allowed.includes( picker.default ) ? { __( 'The start country is not among the countries accepted.', 'funnelforms-pro' ) } : null } ) : null } ); } /** Confirmation by text message of a contact phone field (Funnelforms Pro). */ export function PhoneSmsProps( { props, edit, always }: { props: Props; edit: Edit; always: boolean } ) { const { api, ctx, lang, readOnly } = useEnv(); const [ connections, setConnections ] = useState( null ); const sms = props.sms; const free = 'free' === process.env.FUNNELFORMS_EDITION; const on = undefined !== sms; useEffect( () => { let live = true; if ( on && ! free ) { api.connections().then( ( list ) => live && setConnections( list ), () => live && setConnections( [] ) ); } return () => { live = false; }; }, [ api, free, on ] ); const set = ( key: keyof PhoneSms, value: string ): void => edit( ( p ) => void ( ( p.sms as unknown as Record )[ key ] = value ), 'props.sms.' + key ); const none = null !== connections && ! connections.some( ( c ) => CONNECTION_TYPES.sms.includes( c.type ) ); if ( free && always ) { return { __( 'This field type is part of Funnelforms Pro. Delete the question to publish the form.', 'funnelforms-pro' ) }; } if ( free && ! on ) { return (

{ __( 'Confirming numbers by text message comes with Funnelforms Pro.', 'funnelforms-pro' ) }

); } return ( <> edit( ( p ) => { if ( checked ) { p.country ??= {}; p.sms = { connectionRef: '', sender: '', text: { [ ctx.locale ]: '' } }; } else { delete p.sms; } }, 'props.sms' ) } /> { sms && free ? { __( 'Confirming numbers by text message is part of Funnelforms Pro. Switch it off to publish the form.', 'funnelforms-pro' ) } : null } { sms && ! free ? ( <> { none ? { __( 'There is no SMS connection yet. Add one with Twilio or Bird; without it the form cannot go live.', 'funnelforms-pro' ) } : null } { null === connections ? null : ( set( 'connectionRef', v ) } onConnection={ ( c ) => setConnections( [ ...connections, c ] ) } onConnections={ setConnections } test={ { sender: sms.sender ?? '' } } /> ) } set( 'sender', v ) } hint={ __( 'Name (up to 11 letters) or number shown as sender; some countries only allow numbers.', 'funnelforms-pro' ) } /> edit( ( p ) => put( p.sms as unknown as Record, 'text', lang, v, ctx.locale ), 'props.sms.text' ) } multiline hint={ __( 'Write {{code}} where the code goes. Left empty, the message is "Your confirmation code:" with the code.', 'funnelforms-pro' ) } /> ) : null } ); }