diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/Edit.tsx b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/Edit.tsx index 51f7295207..3bb16ba17c 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/Edit.tsx +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/Edit.tsx @@ -6,21 +6,41 @@ import DonationFormSelector from './components/DonationFormSelector'; import useFormOptions from './hooks/useFormOptions'; import DonationFormBlockControls from './components/DonationFormBlockControls'; import DonationFormBlockPreview from './components/DonationFormBlockPreview'; +import type {BlockPreviewProps} from './components/DonationFormBlockPreview'; import './styles/index.scss'; +import { __ } from '@wordpress/i18n'; /** + * @unreleased + * + * @see 'class-give-block-donation-form.php' + */ +type DonationFormBlockAttributes = { + id: number; + prevId: number; + blockId: string; + displayStyle: BlockPreviewProps['formFormat']; + continueButtonTitle: string; + showTitle: boolean; + showGoal: boolean; + showContent: boolean; + contentDisplay: string; +} + +/** + * @unreleased added isResolving loading state to prevent forms from prematurely being rendered. * @since 3.2.0 updated to handle v2 forms. * @since 3.0.0 */ export default function Edit({attributes, isSelected, setAttributes, className, clientId}: BlockEditProps) { - const {id, blockId, displayStyle, continueButtonTitle} = attributes; + const {id, blockId, displayStyle, continueButtonTitle} = attributes as DonationFormBlockAttributes; const {formOptions, isResolving} = useFormOptions(); const [showPreview, setShowPreview] = useState(!!id); const handleSelect = (id) => { - setShowPreview(true); setAttributes({id}); + setShowPreview(true); }; useEffect(() => { @@ -34,11 +54,17 @@ export default function Edit({attributes, isSelected, setAttributes, className, }, []); const [isLegacyForm, isLegacyTemplate, link] = (() => { - const form = formOptions.find((form) => form.value == id); + const form = formOptions.find((form) => form.value === id); return [form?.isLegacyForm, form?.isLegacyTemplate, form?.link]; })(); + if (isResolving !== false) { + return
+

{__('Loading...', 'give')}

+
+ } + return (
{id && showPreview ? ( diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockControls.tsx b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockControls.tsx index 9e03c3a437..f22ff10431 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockControls.tsx +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockControls.tsx @@ -1,19 +1,19 @@ import {ExternalLink, PanelBody, PanelRow, SelectControl, TextControl, ToggleControl} from '@wordpress/components'; import {__} from '@wordpress/i18n'; import {InspectorControls} from '@wordpress/block-editor'; -import {Option} from '../types'; +import type {FormOption} from '../hooks/useFormOptions'; interface DonationFormBlockControls { attributes: Readonly; setAttributes: (newAttributes: Record) => void; - formOptions: Option[]; + formOptions: FormOption[]; isResolving: boolean; isLegacyTemplate: boolean; isLegacyForm: boolean; } /** - * @unreleased Revert the display style value of "fullForm" to "onpage". + * @unreleased Updated setAttributes ID to be a number and formOptions to return select options. Revert the display style value of "fullForm" to "onpage". * @since 3.2.0 */ export default function DonationFormBlockControls({ @@ -39,7 +39,7 @@ export default function DonationFormBlockControls({ - {!isResolving && formOptions.length === 0 ? ( + {isResolving === false && formOptions.length === 0 ? (

{__('No forms were found using the GiveWP form builder.', 'give')}

) : ( ({label: form.label, value: String(form.value)})), ]} onChange={(newFormId) => { - setAttributes({id: newFormId}); + setAttributes({id: Number(newFormId)}); }} /> )} diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockPreview.tsx b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockPreview.tsx index 58e705e214..f8551151f7 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockPreview.tsx +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormBlockPreview.tsx @@ -9,7 +9,7 @@ import '../styles/index.scss'; * @since 3.1.2 Add typing for formFormat * @since */ -interface BlockPreviewProps { +export interface BlockPreviewProps { formId: number; clientId: string; formFormat: 'onpage' | 'modal' | 'newTab' | 'reveal'; diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormSelector.tsx b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormSelector.tsx index 3765715582..38411643a7 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormSelector.tsx +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/components/DonationFormSelector.tsx @@ -7,16 +7,26 @@ import {reactSelectStyles, reactSelectThemeStyles} from '../styles/reactSelectSt import logo from '../images/givewp-logo.svg'; import '../styles/index.scss'; +import type {FormOption} from '../hooks/useFormOptions'; // @ts-ignore const savePost = () => dispatch('core/editor').savePost(); +/** + * @unreleased + */ +type DonationFormSelectorProps = { + formOptions: FormOption[]; + isResolving: boolean; + handleSelect: (id: number) => void; +} + /** * @since 3.2.0 */ -export default function DonationFormSelector({formOptions, isResolving, handleSelect}) { - const [selectedForm, setSelectedForm] = useState(null); - const form = formOptions.find(form => form.value == selectedForm); +export default function DonationFormSelector({formOptions, isResolving, handleSelect}: DonationFormSelectorProps) { + const [selectedForm, setSelectedForm] = useState(null); + const form = formOptions.find(form => form.value === selectedForm); const {isSaving, isDisabled} = usePostState(); return ( diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/useFormOptions.ts b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/useFormOptions.ts index 3bbca96eba..96a66d9ddc 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/useFormOptions.ts +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/useFormOptions.ts @@ -1,12 +1,18 @@ import {__} from '@wordpress/i18n'; import {useSelect} from '@wordpress/data'; import type {Post} from '@wordpress/core-data/src/entity-types'; -import type {Form, Option} from '../types'; +import type {Form} from '../types'; -type FormOption = Form & Option; +/** + * @unreleased + */ +export interface FormOption extends Form { + label: string; + value: number; +} /** - * unreleased include isLegacyForm, isLegacyFormTemplate & link. + * @since 3.2.0 include isLegacyForm, isLegacyFormTemplate & link. * @since 3.0.0 */ export default function useFormOptions(): { diff --git a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/usePostState.ts b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/usePostState.ts index 73cef541af..517027d948 100644 --- a/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/usePostState.ts +++ b/src/DonationForms/Blocks/DonationFormBlock/resources/editor/hooks/usePostState.ts @@ -1,6 +1,7 @@ import {useSelect} from '@wordpress/data'; /** + * @unreleased updated isDisabled to use isEditedPostSaveable * @since 3.0.0 */ export default function usePostState(): { isSaving: boolean, isDisabled: boolean } { @@ -11,7 +12,7 @@ export default function usePostState(): { isSaving: boolean, isDisabled: boolean const isDisabled = useSelect((select) => { // @ts-ignore - return !select('core/editor').isEditedPostPublishable(); + return !select('core/editor').isEditedPostSaveable(); }, []); return {