/** * WordPress dependencies */ import { SelectControl, TextControl } from '@safe-wordpress/components'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import type { GeneralSettings, OnValidSubmissionSettings, } from '@nelio/forms/types'; export type GeneralSettingsProps = { readonly generalSettings: GeneralSettings; readonly setGeneralSettings: ( settings: GeneralSettings ) => void; }; export const FormGeneralSettings = ( props: GeneralSettingsProps ): JSX.Element | null => { const { generalSettings, setGeneralSettings } = props; const { onValidSubmission, submitProcessingLabel } = generalSettings; const VALID_SUBMISSION_LABELS: Record< OnValidSubmissionSettings[ 'type' ], string > = { none: _x( 'Do nothing', 'command', 'nelio-forms' ), redirect: _x( 'Redirect to another URL', 'command', 'nelio-forms' ), 'hide-form': _x( 'Hide form', 'command', 'nelio-forms' ), }; const VALID_SUBMISSION_OPTIONS = Object.keys( VALID_SUBMISSION_LABELS ).map( ( value: OnValidSubmissionSettings[ 'type' ] ) => ( { value, label: VALID_SUBMISSION_LABELS[ value ], } ) ); const DEFAULT_MESSAGE = _x( 'Form submitted successfully.', 'text', 'nelio-forms' ); return ( <> setGeneralSettings( { ...generalSettings, submitProcessingLabel: value, } ) } /> setGeneralSettings( { ...generalSettings, onValidSubmission: 'redirect' === newType ? { type: newType, redirection: '', } : { type: newType, message: DEFAULT_MESSAGE }, } ) } /> { onValidSubmission.type === 'redirect' && ( setGeneralSettings( { ...generalSettings, onValidSubmission: { ...onValidSubmission, redirection: value, }, } ) } /> ) } { onValidSubmission.type !== 'redirect' && ( setGeneralSettings( { ...generalSettings, onValidSubmission: { ...onValidSubmission, message: value, }, } ) } /> ) } ); };