site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Implement Idea Hub widget support for creating idea draft posts

Open felixarntz opened this issue 5 years ago • 11 comments

The Idea Hub widget functionality for creating a new draft post based on an idea should be added. As part of it, the REST data point for getting new ideas should be updated to filter out all ideas that already have a draft or published post, so that they are no longer included within the new ideas (despite that endpoint for now using mock data).


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The actual functionality for when the user clicks the button to create a new WordPress draft post based on a specific idea (scaffolded as part of #3276, also see Figma UI) should be implemented, relying on the modules/idea-hub action createIdeaDraftPost (implemented in #3269).
  • This should work from both the "New" and "Saved" tab.
  • After completion the action of creating the new draft post, both the data for the current tab ("New" or "Saved") as well as the data for the "Drafts" tab should be (force-)refreshed, to reflect the updated state. Based on that, the numbers on the tab labels should also update accordingly (e.g. number on "Drafts" increases by 1, number on "Saved" decreases by 1 if the post was created from a saved idea).
  • Since it's not clear what a potential loading indicator should look like here, for now none should be displayed. This is being explored for a potential follow-up issue.

Implementation Brief

  • Using assets/js/modules/idea-hub/datastore/draft-ideas.js,
    • Update the reducerCallback to remove the new created draft idea from the savedIdeas and newIdeas state variables, based on the idea name property so that the data for the "New" and "Saved" tabs are updated automatically.
  • Using assets/js/modules/idea-hub/components/dashboard/DashboardIdeasWidget/Idea.js,
    • Add a new state variable processing with false as default value.
    • Edit the handleCreate function to create a draft post via the createIdeaDraftPost action of the modules/idea-hub data store.
    • The createIdeaDraftPost needs an object as parameter with the following keys:
      • name
      • text
      • topics
    • The values from the keys above can be sourced from the name, text and topics props respectively.
    • Prior to calling createIdeaDraftPost, processing should be set to true and then false once createIdeaDraftPost has been called.
    • Set all the buttons in the googlesitekit-idea-hub__idea--actions container disabled attribute if processing is true, except for the View draft button.
    • The data for the "Drafts" tab will automatically be refreshed since the new draft will be added to the draftPostIdeas state variable.

Test Coverage

  • No new tests to be added.

Visual Regression Changes

  • N/A

QA Brief

Set up

  • You will need the widgets.dashboard and ideaHubModule features enabled.
  • You will likely need to hack assets/js/modules/idea-hub/components/dashboard/DashboardIdeasWidget/index.js so that the widget is rendered - if you comment out the whenActive block at the end of the file and instead just export DashboardIdeasWidget as the default, you should see the widget appear on the dashboard.

QA

  • Using the widget, verify that when you click the 'draft' button (icon looks like a page and pencil), a new empty draft post is created. It should be visible in the 'Drafts' tab of the widget and also under the 'Posts' Wordpress section.
  • Verify that the object passed by createIdeaDraftPost is the correct shape
  • Verify that the idea is removed from the savedIdeas and newIdeas stores in redux, and is added to draftPostIdeas.
  • Verify that the tabs on the widget reflect this ('New' should decrement; 'Drafts' should increment)

Changelog entry

  • Add Idea Hub widget support for creating idea draft posts

felixarntz avatar May 13 '21 22:05 felixarntz

@Pratheep-lab Since the interaction of creating the draft post may take a (split-)second (since we need to make an API request), should there be any loading indicator for that action? If so, what could that look like without disrupting the visual experience (would need to be incorporated into this Figma design)?

We don't need to block the work on this issue for this though; based on the outcome of the above we can take care of the loading indicator later.

felixarntz avatar May 14 '21 16:05 felixarntz

@asvinb IB needs more details for the following criteria:

  • After completion the action of creating the new draft post, both the data for the current tab ("New" or "Saved") as well as the data for the "Drafts" tab should be (force-)refreshed, to reflect the updated state. Based on that, the numbers on the tab labels should also update accordingly (e.g. number on "Drafts" increases by 1, number on "Saved" decreases by 1 if the post was created from a saved idea).
  • Since it's not clear what a potential loading indicator should look like here, for now none should be displayed. This is being explored for a potential follow-up issue.

eugene-manuilov avatar Jun 02 '21 16:06 eugene-manuilov

Hey @eugene-manuilov Can you let me know exactly what's missing? For the refreshed data, it's mentioned here: "The data for the "Drafts" tab will automatically be refreshed since the new draft will be added to the draftPostIdeas state variable." And not sure what to add more about the loading indicator since there is none and there is a followup issue as per the AC. Can you advise?

Thanks!

asvinb avatar Jun 03 '21 09:06 asvinb

Can you let me know exactly what's missing? For the refreshed data, it's mentioned here: "The data for the "Drafts" tab will automatically be refreshed since the new draft will be added to the draftPostIdeas state variable."

After completion the action of creating the new draft post, both the data for the current tab ("New" or "Saved") ... should be (force-)refreshed, to reflect the updated state.

And not sure what to add more about the loading indicator since there is none and there is a followup issue as per the AC. Can you advise?

I think we should either hide all buttons or disable them for the idea for which we are creating a new draft post. To make sure that a user can't click on the delete idea button while a draft post is being created.

eugene-manuilov avatar Jun 03 '21 09:06 eugene-manuilov

@eugene-manuilov IB updated, can you check?

asvinb avatar Jun 03 '21 11:06 asvinb

IB looks good now. Thanks, @asvinb!

eugene-manuilov avatar Jun 03 '21 12:06 eugene-manuilov

Hi @felixarntz - just wanted to flag some things with you that I ran into while working on this issue.

Should the idea draft endpoint return an array? The POST:create-idea-draft-post call returns an array containing the idea, so the ideaDraftPost argument in the reducerCallback is an array of length 1. This means that the current code adds a nested array to the draftPostIdeas store:

https://github.com/google/site-kit-wp/blob/ac48a7aa0df1b2951df52d5065f50afd23191706/assets/js/modules/idea-hub/datastore/draft-ideas.js#L37-L40

I've assumed in my PR that it will always be length 1 and am just adding that single item to the store as directed (rather than the array). However, it looks like the reason an array is returned might be because the logic for that endpoint got combined here. I'm not sure whether that was the intended behaviour, so just wanted to flag it.

The draft posts cannot be deleted Once the empty draft posts are created, they cannot be deleted. Possibly because they are empty and WP is trying to update them in order to move them to the trash? I discussed this with @aaemnnosttv and we wondered whether it might be an idea to force the wp_insert_post_empty_content filter to __return_false in all operations, rather than just when creating the post.

Should this work from the 'Saved' tab? Lastly, this point in the AC:

This should work from both the "New" and "Saved" tab.

We don't seem to currently have the functionality to add a new idea into the saved tab in order to do this, so are we missing a step here? I wasn't sure whether this was missed in the IB, whether that's out of scope here, or whether I'm just confused 😄

johnPhillips avatar Jun 08 '21 10:06 johnPhillips

@johnPhillips

The POST:create-idea-draft-post call returns an array containing the idea, so the ideaDraftPost argument in the reducerCallback is an array of length 1.

Good catch! This is actually something that is slightly incorrect in #3269 - the POST:create-idea-draft-post endpoint should return the associative array (object in JS) of the idea itself, not an array containing it (since, as you're saying, it'll always be one anyway). I've left a comment in https://github.com/google/site-kit-wp/issues/3269#issuecomment-856986873 and reopened this issue to fix it. So what should happen is that ideaDraftPost is an object.

Once the empty draft posts are created, they cannot be deleted.

Also a great point. Let's ignore this as part of this issue though - I'll create a follow-up issue.

We don't seem to currently have the functionality to add a new idea into the saved tab in order to do this, so are we missing a step here?

Indeed we don't have the functionality to move something from "New" to "Saved" yet. Still, this issue should ensure that, if an idea is in "Saved", a draft post can created for it from that tab as well. In other words, the functionality should work in both tabs. For testing that it actually does, you'll for now need to manually populate the "Saved" tab with an idea, for example by manually populate the data store via the console, or quickly temporarily hard-code some PHP in the respective endpoint so that the current hard-coded array is not empty. Does that make sense?

felixarntz avatar Jun 08 '21 18:06 felixarntz

@johnPhillips #3515 now fixed the endpoint response to be an object, so you should be good to go once you refresh your PR against develop.

felixarntz avatar Jun 08 '21 19:06 felixarntz

@felixarntz Thanks a lot for the clarification, much appreciated. I've updated my PR now 👍

johnPhillips avatar Jun 09 '21 10:06 johnPhillips

QA Eng ✅ I hacked the dashboard widget as described and observed the UI and store behaviour as briefed.

Hazlitte avatar Jun 14 '21 09:06 Hazlitte