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

Revisions #5: Add support for page postTypes #14928

Merged
merged 1 commit into from
Aug 22, 2017
Merged

Conversation

bperson
Copy link
Contributor

@bperson bperson commented Jun 8, 2017

Note: This builds on top of #14927, so it's not merge-able yet

5th chunk coming from my WIP PR to introduce post revisions in calypso: #13367. This simply adds support for pages. It's mostly code to hit a different endpoint based on the postType.

testing

  • The unit tests for the data-layer part have been updated to test this use case as well
  • Manual testing: Tap on "x revisions" in the post editor sidebar of a page (shown only on pages with multiple revisions)

@bperson bperson added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jun 8, 2017
@matticbot matticbot added OSS Citizen [Size] M Medium sized issue labels Jun 8, 2017
Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Does the revisions endpoint not support custom post types? Ideally we'd not be tied to posts and pages only. These are mistakes we'd made early in both the REST API and Calypso that would be good to avoid moving forward. Would GET /sites/%s/posts/%d/revisions return anything if the ID passed was one for a custom post type?

dispatch( http( {
path: `/sites/${ siteId }/posts/${ postId }/revisions`,
path: `/sites/${ siteId }/${ ressourceName }/${ postId }/revisions`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: ressourceName -> resourceName

Copy link
Contributor Author

Choose a reason for hiding this comment

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

french sneaking in, it should be fixed ;)

@bperson
Copy link
Contributor Author

bperson commented Jun 13, 2017

Does the revisions endpoint not support custom post types? Ideally we'd not be tied to posts and pages only.

it looks like there is support for some post types.

Would GET /sites/%s/posts/%d/revisions return anything if the ID passed was one for a custom post type?

Nope, but it does work (for some post types) if you use the normalised post type resource name [0]: /sites/%s/<normalised post type>/%d/revisions. For instance, it works with jetpack-testimonial but not with media (it's the normalised post type resource name for the attachment post type).

[0] I can't find logic in calypso that would do what's done in the REST API to compute the resource name though:

$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;

The issue comes from the fact we populate postTypes in Redux from old endpoints which won't return rest_base which is used to compute the resource name.

I ideally would like to not upgrade the code fetching post types to v2, as it sounds like a deep rabbit hole, so there are 2 options I think:
1- disable the revisions UI for custom post types
2- play it risky: const resourceName = get( { page: pages, post: posts }, postType, postType )

@aduth
Copy link
Contributor

aduth commented Jun 13, 2017

Interesting observations. I wonder how common it is to rename rest_base. Regardless, you're right to point out that it's something we'd want to account for. Throwing out a few other ideas:

  • Include optional flag on <QueryPostTypes /> that invokes an additional request to the wp/v2 post types endpoint to determine rest_base
    • State entity would include data from multiple sources; in fairness we don't want to tie entities to particular API responses anyways. Multiple API requests, complex logic to manage
  • Include rest_base in the WordPress.com post types response
    • Might require Jetpack changes and therefore version minimum, awkward to have the .com endpoints returning information about the .org equivalents
  • "play it risky", but at least account for the failure case gracefully
    • Detect the 404 and disable revisions UI when encountered?

Porting post types to wp/v2 would indeed be a rabbit hole, since the .com endpoints use a different criteria for determining whether a post type should be made available on API responses.

Enabling the UI only for posts and pages is an option, though not ideal per my previous comment.

@bperson
Copy link
Contributor Author

bperson commented Jun 13, 2017

Detect the 404 and disable revisions UI when encountered?

Right now, with #14927, we fetch the revisions when the UI gets rendered which makes it impossible to disable the revision UI as a result of a 404, at best, we can display a button redirecting to wp-admin or an error message.

That said, I think it's safer for now to only enable it for posts/pages, leave a NOTE/TODO comment (or a GH issue?) to revisit it with a bit more mileage on the revisions UI. It makes the UX consistent for all custom types, without having to explain in an error message why some are failing and redirecting the user to wp-admin due to an hidden parameter.

@@ -87,9 +87,10 @@ export const receiveSuccess = ( { dispatch }, { siteId, postId }, next, revision
* @param {Object} action Redux action
*/
export const fetchPostRevisions = ( { dispatch }, action ) => {
const { siteId, postId } = action;
const { siteId, postId, postType } = action;
const resourceName = postType === 'page' ? 'pages' : 'posts';
Copy link
Contributor

Choose a reason for hiding this comment

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

Even if we don't support custom post types quite yet, is there a way we could express this to be less tied to posts and pages? If we hypothetically had access to rest_base, how would we want this data handler and the associated action creator to be written?

Copy link
Contributor Author

@bperson bperson Jun 16, 2017

Choose a reason for hiding this comment

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

  • associated action creator: nothing? postType is the most suited parameter to transmit that data in the action imho: using the rest_base in the action would leak a "data-layer" concept + postType is widely used in the codebase.

  • data handler:

export const fetchPostRevisions = ( { dispatch, getState }, action ) => {
	const { siteId, postId } = action;
	const { siteId, postId, postType } = action;
	const resourceName = getPostType( getState(), siteId, postType ).rest_base
	(...)
} );

@bperson bperson force-pushed the add/revisions-list-diff-viewer branch from 27b769d to 4ca1184 Compare June 19, 2017 16:49
@bperson bperson force-pushed the add/revisions-page-support branch from ad22062 to 6cf088b Compare June 19, 2017 16:54
@bperson
Copy link
Contributor Author

bperson commented Jun 19, 2017

Now that #14943 is merged, CircleCI is green, it's also rebased on the latest master

Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Minor note, but this is looking good.

@@ -77,6 +77,7 @@ const EditorSidebar = ( {
selectedRevisionId={ selectedRevisionId }
selectRevision={ selectRevision }
siteId={ site.ID }
type={ type }
Copy link
Contributor

Choose a reason for hiding this comment

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

Passing this as a prop is arguably a bit redundant, because siteId and postId props should be sufficient for the component to be able to look up this information from state on its own via connect. EditorDocumentHead is similar here (though more extreme in that it doesn't accept any props) where it determines the type using the getEditedPostValue selector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interesting, it's updated (I also updated #14927 to load siteId and postId from state).

@matticbot
Copy link
Contributor

@bperson This PR needs a rebase

@bperson bperson force-pushed the add/revisions-list-diff-viewer branch from 5122f83 to 0d1618b Compare July 4, 2017 04:25
@bperson bperson force-pushed the add/revisions-page-support branch from e54feed to dcd095a Compare July 11, 2017 13:02
@mcsf mcsf force-pushed the add/revisions-list-diff-viewer branch from 1ecf66b to d532714 Compare August 12, 2017 11:23
@mcsf mcsf force-pushed the add/revisions-page-support branch from dcd095a to ff6fdae Compare August 12, 2017 17:16
@mcsf mcsf self-assigned this Aug 13, 2017
@mcsf mcsf force-pushed the add/revisions-list-diff-viewer branch from 6de7aa2 to ad4121b Compare August 21, 2017 15:57
@matticbot
Copy link
Contributor

@bperson This PR needs a rebase

@mcsf mcsf force-pushed the add/revisions-page-support branch from ff6fdae to 327585d Compare August 21, 2017 22:38
@matticbot matticbot added [Size] XL Probably needs to be broken down into multiple smaller issues and removed [Size] M Medium sized issue labels Aug 21, 2017
@mcsf mcsf changed the base branch from add/revisions-list-diff-viewer to master August 21, 2017 22:39
@mcsf mcsf merged commit 6f258fd into master Aug 22, 2017
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 22, 2017
@alisterscott alisterscott deleted the add/revisions-page-support branch February 1, 2018 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OSS Citizen [Size] XL Probably needs to be broken down into multiple smaller issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants