Skip to content

Commit

Permalink
Control automatically when hierarchical sort is active
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 12, 2024
1 parent afad9c8 commit 70c097d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 19 additions & 3 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ function getItemId( item ) {

export default function PostList( { postType } ) {
const [ view, setView ] = useView( postType );
const [ isDirty, setIsDirty ] = useState( false );
const defaultViews = useDefaultViews( { postType } );
const history = useHistory();
const location = useLocation();
Expand All @@ -215,11 +216,27 @@ export default function PostList( { postType } ) {
},
[ location.path, location.query.isCustom, history ]
);

const getActiveViewFilters = ( views, match ) => {
const found = views.find( ( { slug } ) => slug === match );
return found?.filters ?? [];
};
useEffect( () => {
if ( isDirty ) {
return;
}

// The sort defaults are set in DEFAULT_POST_BASE.
if ( view.sort.field !== 'title' || view.sort.direction !== 'asc' ) {
setIsDirty( true );
setView( {
...view,
layout: {
...( view?.layout || {} ),
hierarchicalSort: false,
},
} );
}
}, [ isDirty, view ] );

const { isLoading: isLoadingFields, fields: _fields } = usePostFields( {
postType,
Expand Down Expand Up @@ -296,8 +313,7 @@ export default function PostList( { postType } ) {
_embed: 'author',
order: view.sort?.direction,
orderby: view.sort?.field,
orderby_hierarchy:
view.type === 'table' && !! view.layout?.hierarchicalSort,
orderby_hierarchy: !! view.layout?.hierarchicalSort,
search: view.search,
...filters,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
} from '../../utils/constants';

export const defaultLayouts = {
[ LAYOUT_TABLE ]: {},
[ LAYOUT_TABLE ]: {
layout: {
hierarchicalSort: true,
},
},
[ LAYOUT_GRID ]: {},
[ LAYOUT_LIST ]: {},
};
Expand Down

0 comments on commit 70c097d

Please sign in to comment.