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 (#67744)

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
4 people authored Dec 10, 2024
1 parent 20a9a81 commit 8e048a8
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' );
} );
} );

1 comment on commit 8e048a8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8e048a8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12251117005
📝 Reported issues:

Please sign in to comment.