/**
* edit.js
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import {
PanelBody,
SelectControl,
ToggleControl,
TextControl,
Notice
} from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
/**
* Styles
*/
import './editor.scss';
/**
* Edit function for Form Block
*/
export default function Edit({ attributes, setAttributes }) {
const {
formType,
showLabels,
customEmailTo,
turnstileEnabled
} = attributes;
const [isPreviewVisible, setIsPreviewVisible] = useState(true);
const blockProps = useBlockProps({
className: `jvb-form-block ${formType ? `jvb-form-block-${formType}` : ''}`
});
// Get form types from localized data, with fallback
const getFormTypes = () => {
if (typeof window !== 'undefined' && window.jvbFormsData && window.jvbFormsData.formTypes) {
return window.jvbFormsData.formTypes;
}
// Fallback if data isn't available
return [
{ label: __('Select a form type', 'jvb'), value: '' },
{ label: __('No forms available', 'jvb'), value: '', disabled: true }
];
};
// Get available forms configuration
const getAvailableForms = () => {
console.log(window.jvbFormsData);
if (typeof window !== 'undefined' && window.jvbFormsData && window.jvbFormsData.availableForms) {
return window.jvbFormsData.availableForms;
}
return {};
};
const formTypes = getFormTypes();
const availableForms = getAvailableForms();
// Get form configuration based on selected type
const getCurrentFormConfig = () => {
if (!formType || !availableForms[formType]) {
return null;
}
return availableForms[formType];
};
// Form labels based on the selected form type
const getFormLabels = () => {
const formConfig = getCurrentFormConfig();
if (formConfig) {
return {
title: formConfig.title || __('Form', 'jvb'),
description: Array.isArray(formConfig.description)
? formConfig.description.join(' ')
: (formConfig.description || ''),
button: formConfig.submit || __('Submit', 'jvb')
};
}
return {
title: __('Form', 'jvb'),
description: formType ? __('Loading form configuration...', 'jvb') : __('Please select a form type in the sidebar', 'jvb'),
button: __('Submit', 'jvb')
};
};
const formLabels = getFormLabels();
// Render a preview of the form in the editor
const renderFormPreview = () => {
if (!formType) {
return (
{formLabels.description}
{field.help}
)}{__('This is just a preview. The actual form will be rendered on the frontend.', 'jvb')}
{__('Form preview is hidden. Edit settings in the sidebar.', 'jvb')}