Skip to content

Commit e1fa507

Browse files
Merge pull request #1834 from Codeinwp/fix/form-saving-fse
Allow saving in FSE for Form Options
2 parents 3d002a8 + e77fe2d commit e1fa507

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/blocks/blocks/form/edit.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import classnames from 'classnames';
55

6-
import { get, isEqual } from 'lodash';
6+
import { debounce, get, isEqual } from 'lodash';
77

88
import hash from 'object-hash';
99

@@ -53,7 +53,6 @@ import Inspector from './inspector.js';
5353
import Placeholder from './placeholder.js';
5454
import { useResponsiveAttributes } from '../../helpers/utility-hooks';
5555
import { renderBoxOrNumWithUnit, _cssBlock, _px, findInnerBlocks } from '../../helpers/helper-functions';
56-
import { Notice } from '@wordpress/components';
5756

5857
const { attributes: defaultAttributes } = metadata;
5958

@@ -107,7 +106,7 @@ const Edit = ({
107106
* @param {import('../../common').SyncAttrs<import('./type').FormAttrs>} field
108107
* @returns
109108
*/
110-
const getSyncValue = field =>{
109+
const getSyncValue = field => {
111110
if ( attributes?.isSynced?.includes( field ) ) {
112111
return getDefaultValueByField({ name, field, defaultAttributes, attributes });
113112
}
@@ -143,17 +142,24 @@ const Edit = ({
143142
moveBlockToPosition
144143
} = useDispatch( 'core/block-editor' );
145144

146-
const {
147-
unlockPostSaving
148-
} = useDispatch( 'core/editor' );
149-
150145
const setFormOption = option => {
151146
setFormOptions( options => ({ ...options, ...option }) );
152147
};
153148

149+
/**
150+
* This mark the block as dirty which allow us to use the save button to trigger the update of the form options tied to WP Options.
151+
*
152+
* @type {DebouncedFunc<(function(): void)|*>}
153+
*/
154+
const enableSaveBtn = debounce( () => {
155+
const dummyBlock = createBlock( 'core/spacer', { height: '0px' });
156+
insertBlock( dummyBlock, 0, clientId, false );
157+
removeBlock( dummyBlock.clientId, false );
158+
}, 3000 );
159+
154160
const setFormOptionAndSaveUnlock = option => {
155161
setFormOption( option );
156-
unlockPostSaving?.();
162+
enableSaveBtn();
157163
};
158164

159165
const [ savedFormOptions, setSavedFormOptions ] = useState( true );
@@ -204,9 +210,10 @@ const Edit = ({
204210
const isPublishingPost = select( 'core/editor' )?.isPublishingPost();
205211
const isAutosaving = select( 'core/editor' )?.isAutosavingPost();
206212
const widgetSaving = select( 'core/edit-widgets' )?.isSavingWidgetAreas();
213+
const nonPostEntitySaving = select( 'core/editor' )?.isSavingNonPostEntityChanges();
207214

208215
return {
209-
canSaveData: ( ! isAutosaving && ( isSavingPost || isPublishingPost ) ) || widgetSaving
216+
canSaveData: ( ! isAutosaving && ( isSavingPost || isPublishingPost || nonPostEntitySaving ) ) || widgetSaving
210217
};
211218
});
212219

src/blocks/helpers/block-utility.js

+2
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,5 @@ export function useTabSwitch( key, defaultValue ) {
537537

538538
return [ tab, setTab ];
539539
}
540+
541+

src/blocks/test/e2e/blocks/form.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,49 @@ test.describe( 'Form Block', () => {
295295

296296
await expect( page.getByLabel( 'Dismiss this notice' ) ).toBeVisible();
297297
});
298+
299+
test( 'enable post save button on options changed', async({ page, editor }) => {
300+
const ccValue = '[email protected]';
301+
302+
/*
303+
* Create a form block and insert the CC value using the Inspector Controls.
304+
*/
305+
306+
await editor.insertBlock({ name: 'themeisle-blocks/form' });
307+
308+
let formBlock = ( await editor.getBlocks() ).find( ( block ) => 'themeisle-blocks/form' === block.name );
309+
310+
expect( formBlock ).toBeTruthy();
311+
312+
const { clientId } = formBlock;
313+
314+
await page.click( `#block-${clientId} > div > fieldset > ul > li:nth-child(1) > button` );
315+
316+
// Open the options panel
317+
await page.getByRole( 'button', { name: 'Form Options options' }).click();
318+
319+
// activate the option
320+
await page.getByRole( 'menuitemcheckbox', { name: 'Show CC' }).click();
321+
322+
// Close the options panel
323+
await page.getByRole( 'button', { name: 'Form Options options' }).click();
324+
325+
const cc = page.getByPlaceholder( 'Send copies to' );
326+
327+
await cc.fill( ccValue );
328+
329+
await editor.publishPost();
330+
331+
await page.getByLabel( 'Close panel' ).click();
332+
333+
await page.getByPlaceholder( 'Default is to admin site' ).fill( ccValue );
334+
335+
const saveBtn = page.getByRole( 'button', { name: 'Update', disabled: false });
336+
337+
await saveBtn.waitFor({
338+
timeout: 4000
339+
});
340+
341+
expect( await saveBtn.isEnabled() ).toBeTruthy();
342+
});
298343
});

0 commit comments

Comments
 (0)