Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow saving in FSE for Form Options #1834

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/blocks/blocks/form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import classnames from 'classnames';

import { get, isEqual } from 'lodash';
import { debounce, get, isEqual } from 'lodash';

import hash from 'object-hash';

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

const { attributes: defaultAttributes } = metadata;

Expand Down Expand Up @@ -143,17 +142,24 @@ const Edit = ({
moveBlockToPosition
} = useDispatch( 'core/block-editor' );

const {
unlockPostSaving
} = useDispatch( 'core/editor' );

const setFormOption = option => {
setFormOptions( options => ({ ...options, ...option }) );
};

/**
* 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.
*
* @type {DebouncedFunc<(function(): void)|*>}
*/
const enableSaveBtn = debounce( () => {
const dummyBlock = createBlock( 'core/spacer', { height: '0px' });
insertBlock( dummyBlock, 0, clientId, false );
removeBlock( dummyBlock.clientId, false );
}, 3000 );

const setFormOptionAndSaveUnlock = option => {
setFormOption( option );
unlockPostSaving?.();
enableSaveBtn();
};

const [ savedFormOptions, setSavedFormOptions ] = useState( true );
Expand Down
2 changes: 2 additions & 0 deletions src/blocks/helpers/block-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,5 @@ export function useTabSwitch( key, defaultValue ) {

return [ tab, setTab ];
}


45 changes: 45 additions & 0 deletions src/blocks/test/e2e/blocks/form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,49 @@ test.describe( 'Form Block', () => {
// check for a element with the attribute data-redirect-url
await expect( await page.$( `[data-redirect="${REDIRECT_URL}"]` ) ).toBeTruthy();
});

test( 'enable post save button on options changed', async({ page, editor }) => {
const ccValue = '[email protected]';

/*
* Create a form block and insert the CC value using the Inspector Controls.
*/

await editor.insertBlock({ name: 'themeisle-blocks/form' });

let formBlock = ( await editor.getBlocks() ).find( ( block ) => 'themeisle-blocks/form' === block.name );

expect( formBlock ).toBeTruthy();

const { clientId } = formBlock;

await page.click( `#block-${clientId} > div > fieldset > ul > li:nth-child(1) > button` );

// Open the options panel
await page.getByRole( 'button', { name: 'Form Options options' }).click();

// activate the option
await page.getByRole( 'menuitemcheckbox', { name: 'Show CC' }).click();

// Close the options panel
await page.getByRole( 'button', { name: 'Form Options options' }).click();

const cc = page.getByPlaceholder( 'Send copies to' );

await cc.fill( ccValue );

await editor.publishPost();

await page.getByLabel( 'Close panel' ).click();

await page.getByPlaceholder( 'Default is to admin site' ).fill( ccValue );

const saveBtn = page.getByRole( 'button', { name: 'Update', disabled: false });

await saveBtn.waitFor({
timeout: 4000
});

expect( await saveBtn.isEnabled() ).toBeTruthy();
});
});