-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Posts DataViews: Refactor the router to use route registration #67160
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/edit-site/src/components/posts-app-routes/home.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Editor from '../editor'; | ||
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
function HomeMobileView() { | ||
const { params = {} } = useLocation(); | ||
const { canvas = 'view' } = params; | ||
|
||
return canvas === 'edit' ? ( | ||
<Editor isPostsList /> | ||
) : ( | ||
<SidebarNavigationScreenMain /> | ||
); | ||
} | ||
|
||
export const homeRoute = { | ||
name: 'home', | ||
match: () => { | ||
return true; | ||
}, | ||
areas: { | ||
sidebar: <SidebarNavigationScreenMain />, | ||
preview: <Editor isPostsList />, | ||
mobile: HomeMobileView, | ||
}, | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/edit-site/src/components/posts-app-routes/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRegistry, useDispatch } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
import { store as siteEditorStore } from '../../store'; | ||
import { homeRoute } from './home'; | ||
import { postsListViewQuickEditRoute } from './posts-list-view-quick-edit'; | ||
import { postsListViewRoute } from './posts-list-view'; | ||
import { postsViewQuickEditRoute } from './posts-view-quick-edit'; | ||
import { postsViewRoute } from './posts-view'; | ||
import { postsEditRoute } from './posts-edit'; | ||
|
||
const routes = [ | ||
postsListViewQuickEditRoute, | ||
postsListViewRoute, | ||
postsViewQuickEditRoute, | ||
postsViewRoute, | ||
postsEditRoute, | ||
homeRoute, | ||
]; | ||
|
||
export function useRegisterPostsAppRoutes() { | ||
const registry = useRegistry(); | ||
const { registerRoute } = unlock( useDispatch( siteEditorStore ) ); | ||
useEffect( () => { | ||
registry.batch( () => { | ||
routes.forEach( registerRoute ); | ||
} ); | ||
}, [ registry, registerRoute ] ); | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/edit-site/src/components/posts-app-routes/posts-edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostList from '../post-list'; | ||
import DataViewsSidebarContent from '../sidebar-dataviews'; | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
import Editor from '../editor'; | ||
|
||
export const postsEditRoute = { | ||
name: 'posts-edit', | ||
match: ( params ) => { | ||
return params.postType === 'post' && params.canvas === 'edit'; | ||
}, | ||
areas: { | ||
sidebar: ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Posts' ) } | ||
isRoot | ||
content={ <DataViewsSidebarContent /> } | ||
/> | ||
), | ||
content: <PostList postType="post" />, | ||
mobile: <Editor isPostsList />, | ||
preview: <Editor isPostsList />, | ||
}, | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/edit-site/src/components/posts-app-routes/posts-list-view-quick-edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostList from '../post-list'; | ||
import DataViewsSidebarContent from '../sidebar-dataviews'; | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
import { unlock } from '../../lock-unlock'; | ||
import { PostEdit } from '../post-edit'; | ||
import Editor from '../editor'; | ||
|
||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
function PostQuickEdit() { | ||
const { params } = useLocation(); | ||
return <PostEdit postType="post" postId={ params.postId } />; | ||
} | ||
|
||
export const postsListViewQuickEditRoute = { | ||
name: 'posts-list-view-quick-edit', | ||
match: ( params ) => { | ||
return ( | ||
params.isCustom !== 'true' && | ||
( params.layout ?? 'list' ) === 'list' && | ||
!! params.quickEdit && | ||
params.postType === 'post' && | ||
params.canvas !== 'edit' | ||
); | ||
}, | ||
areas: { | ||
sidebar: ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Posts' ) } | ||
isRoot | ||
content={ <DataViewsSidebarContent /> } | ||
/> | ||
), | ||
content: <PostList postType="post" />, | ||
mobile: <PostList postType="post" />, | ||
preview: <Editor />, | ||
edit: <PostQuickEdit />, | ||
}, | ||
widths: { | ||
content: 380, | ||
edit: 380, | ||
}, | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/edit-site/src/components/posts-app-routes/posts-list-view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostList from '../post-list'; | ||
import DataViewsSidebarContent from '../sidebar-dataviews'; | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
import Editor from '../editor'; | ||
|
||
export const postsListViewRoute = { | ||
name: 'posts-list-view', | ||
match: ( params ) => { | ||
return ( | ||
params.isCustom !== 'true' && | ||
( params.layout ?? 'list' ) === 'list' && | ||
! params.quickEdit && | ||
params.postType === 'post' && | ||
params.canvas !== 'edit' | ||
); | ||
}, | ||
areas: { | ||
sidebar: ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Posts' ) } | ||
isRoot | ||
content={ <DataViewsSidebarContent /> } | ||
/> | ||
), | ||
content: <PostList postType="post" />, | ||
preview: <Editor isPostsList />, | ||
mobile: <PostList postType="post" />, | ||
}, | ||
widths: { | ||
content: 380, | ||
}, | ||
}; |
49 changes: 49 additions & 0 deletions
49
packages/edit-site/src/components/posts-app-routes/posts-view-quick-edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostList from '../post-list'; | ||
import DataViewsSidebarContent from '../sidebar-dataviews'; | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
import { unlock } from '../../lock-unlock'; | ||
import { PostEdit } from '../post-edit'; | ||
|
||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
function PostQuickEdit() { | ||
const { params } = useLocation(); | ||
return <PostEdit postType="post" postId={ params.postId } />; | ||
} | ||
|
||
export const postsViewQuickEditRoute = { | ||
name: 'posts-view-quick-edit', | ||
match: ( params ) => { | ||
return ( | ||
( params.isCustom === 'true' || | ||
( params.layout ?? 'list' ) !== 'list' ) && | ||
!! params.quickEdit && | ||
params.postType === 'post' && | ||
params.canvas !== 'edit' | ||
); | ||
}, | ||
areas: { | ||
sidebar: ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Posts' ) } | ||
isRoot | ||
content={ <DataViewsSidebarContent /> } | ||
/> | ||
), | ||
content: <PostList postType="post" />, | ||
mobile: <PostList postType="post" />, | ||
edit: <PostQuickEdit />, | ||
}, | ||
widths: { | ||
edit: 380, | ||
}, | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/edit-site/src/components/posts-app-routes/posts-view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostList from '../post-list'; | ||
import DataViewsSidebarContent from '../sidebar-dataviews'; | ||
import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
|
||
export const postsViewRoute = { | ||
name: 'posts-view', | ||
match: ( params ) => { | ||
return ( | ||
( params.isCustom === 'true' || | ||
( params.layout ?? 'list' ) !== 'list' ) && | ||
! params.quickEdit && | ||
params.postType === 'post' && | ||
params.canvas !== 'edit' | ||
); | ||
}, | ||
areas: { | ||
sidebar: ( | ||
<SidebarNavigationScreen | ||
title={ __( 'Posts' ) } | ||
isRoot | ||
content={ <DataViewsSidebarContent /> } | ||
/> | ||
), | ||
content: <PostList postType="post" />, | ||
mobile: <PostList postType="post" />, | ||
}, | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,15 @@ import { privateApis as routerPrivateApis } from '@wordpress/router'; | |
* Internal dependencies | ||
*/ | ||
import Layout from '../layout'; | ||
import useActiveRoute from './router'; | ||
import { useRegisterPostsAppRoutes } from '../posts-app-routes'; | ||
import { unlock } from '../../lock-unlock'; | ||
import useActiveRoute from '../layout/router'; | ||
|
||
const { RouterProvider } = unlock( routerPrivateApis ); | ||
const { GlobalStylesProvider } = unlock( editorPrivateApis ); | ||
|
||
function PostsLayout() { | ||
useRegisterPostsAppRoutes(); | ||
const route = useActiveRoute(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One interesting thing here, is that now this hook is the exact same one used in the site editor. |
||
return <Layout route={ route } />; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another interesting thing is that all these routes are the exact same ones that exist in the site editor, there are just very small differences:
postType
is "post" instead of "page"backPath={}
(this is just temporary because the posts page doesn't have categories and tags yet so it's top level)Editor
component.This is a good thing and an indication that we can easily support custom post types with the same routes (by making the post type dynamic)