/** * New form: pick a type (each drawn where it sits on a page), a template * (each drawn as it looks) and a name; the form is made with that template * (`POST forms`), off, and opens in the builder. */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { useUi } from '../../runtime'; import { useCreateForm, useFormTemplates } from '../../hooks/use-forms'; import { types } from '../../lib/forms'; import { errorText } from '../../lib/format'; import { cn } from '../../kit'; import TemplateGrid from '../form-builder/TemplateGrid'; import TypeArt from './TypeArt'; /** * The type cards, also shown on the empty Forms page. * * @param {Object} props * @param {string} props.value The type picked. * @param {Function} props.onChange Called with a type. * @param {boolean} props.compact One row of small cards (the dialog). * @return {Element} The cards. */ export function TypePicker( { value, onChange, compact = false } ) { return (
{ types().map( ( type ) => ( ) ) }
); } /** * @param {Object} props * @param {boolean} props.open Shown. * @param {Function} props.onClose Called to close it. * @param {Function} props.onCreate Called with the new form. * @param {string} props.initial The type picked to start. * @return {Element} The dialog. */ export default function NewFormDialog( { open, onClose, onCreate, initial = 'popup', } ) { const { ui } = useUi(); const create = useCreateForm(); const { data: templates = [] } = useFormTemplates(); const [ type, setType ] = useState( initial ); const [ template, setTemplate ] = useState( '' ); const [ name, setName ] = useState( '' ); const picked = templates.find( ( t ) => t.id === template )?.id || templates[ 0 ]?.id || ''; const submit = ( event ) => { event?.preventDefault(); create.mutate( { type, name, ...( picked ? { template: picked } : {} ) }, { onSuccess: ( form ) => { setName( '' ); onCreate( form ); }, onError: ( err ) => ui.toast.error( errorText( err, __( 'Could not make the form.', 'mailpuff' ) ) ), } ); }; return ( ! next && onClose() } title={ __( 'New form', 'mailpuff' ) } description={ __( 'Pick where it shows and how it looks. You can change everything after.', 'mailpuff' ) } size="xl" footer={ <> { __( 'Cancel', 'mailpuff' ) } { __( 'Create form', 'mailpuff' ) } } >
{ templates.length > 0 && (

{ __( 'Template', 'mailpuff' ) }

) } { ( a11y ) => ( setName( e.target.value ) } /> ) }
); }