Skip to content

Commit

Permalink
DataViews: Preserve filters when switching layouts in templates datav…
Browse files Browse the repository at this point in the history
…iews (WordPress#67744)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
4 people authored and yogeshbhutkar committed Dec 18, 2024
1 parent fdcbdc1 commit fc2f2fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
39 changes: 22 additions & 17 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { authorField, descriptionField, previewField } from './fields';
import { useEvent } from '@wordpress/compose';

const { usePostActions } = unlock( editorPrivateApis );
const { useHistory, useLocation } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -92,11 +93,19 @@ export default function PageTemplates() {
};
}, [ layout, activeView ] );
const [ view, setView ] = useState( defaultView );

// Sync the layout from the URL to the view state.
useEffect( () => {
setView( ( currentView ) => ( {
...currentView,
type: layout ?? DEFAULT_VIEW.type,
} ) );
}, [ setView, layout ] );

// Sync the active view from the URL to the view state.
useEffect( () => {
const usedType = layout ?? DEFAULT_VIEW.type;
setView( ( currentView ) => ( {
...currentView,
type: usedType,
filters:
activeView !== 'all'
? [
Expand All @@ -108,7 +117,7 @@ export default function PageTemplates() {
]
: [],
} ) );
}, [ activeView, layout ] );
}, [ setView, activeView ] );

const { records, isResolving: isLoadingData } =
useEntityRecordsWithPermissions( 'postType', TEMPLATE_POST_TYPE, {
Expand Down Expand Up @@ -170,20 +179,16 @@ export default function PageTemplates() {
[ postTypeActions, editAction ]
);

const onChangeView = useCallback(
( newView ) => {
if ( newView.type !== view.type ) {
history.navigate(
addQueryArgs( path, {
layout: newView.type,
} )
);
}

setView( newView );
},
[ view.type, setView, history, path ]
);
const onChangeView = useEvent( ( newView ) => {
setView( newView );
if ( newView.type !== layout ) {
history.navigate(
addQueryArgs( path, {
layout: newView.type,
} )
);
}
} );

return (
<Page
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/specs/site-editor/templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,27 @@ test.describe( 'Templates', () => {
)
).toBeVisible();
} );

test( 'Persists filter/search when switching layout', async ( {
page,
admin,
} ) => {
await admin.visitSiteEditor();
await page.getByRole( 'button', { name: 'Templates' } ).click();

// Search templates
await page.getByRole( 'searchbox', { name: 'Search' } ).fill( 'Index' );

// Switch layout
await page.getByRole( 'button', { name: 'Layout' } ).click();
await page.getByRole( 'menuitemradio', { name: 'Table' } ).click();

// Confirm the table is visible
await expect( page.getByRole( 'table' ) ).toContainText( 'Index' );

// The search should still contain the search term
await expect(
page.getByRole( 'searchbox', { name: 'Search' } )
).toHaveValue( 'Index' );
} );
} );

0 comments on commit fc2f2fa

Please sign in to comment.