Implement Idea Hub widget support for creating idea draft posts
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-hubactioncreateIdeaDraftPost(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
reducerCallbackto remove the new created draft idea from thesavedIdeasandnewIdeasstate variables, based on the ideanameproperty so that the data for the "New" and "Saved" tabs are updated automatically.
- Update the
- Using
assets/js/modules/idea-hub/components/dashboard/DashboardIdeasWidget/Idea.js,- Add a new state variable
processingwithfalseas default value. - Edit the
handleCreatefunction to create a draft post via thecreateIdeaDraftPostaction of themodules/idea-hubdata store. - The
createIdeaDraftPostneeds an object as parameter with the following keys:nametexttopics
- The values from the keys above can be sourced from the
name,textandtopicsprops respectively. - Prior to calling
createIdeaDraftPost,processingshould be set totrueand thenfalseoncecreateIdeaDraftPosthas been called. - Set all the buttons in the
googlesitekit-idea-hub__idea--actionscontainerdisabledattribute ifprocessingistrue, except for theView draftbutton. - The data for the "Drafts" tab will automatically be refreshed since the new draft will be added to the
draftPostIdeasstate variable.
- Add a new 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.jsso that the widget is rendered - if you comment out thewhenActiveblock at the end of the file and instead just exportDashboardIdeasWidgetas 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
createIdeaDraftPostis the correct shape - Verify that the idea is removed from the
savedIdeasandnewIdeasstores in redux, and is added todraftPostIdeas. - 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
@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.
@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.
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!
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 IB updated, can you check?
IB looks good now. Thanks, @asvinb!
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
The
POST:create-idea-draft-postcall returns an array containing the idea, so theideaDraftPostargument in thereducerCallbackis 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?
@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 Thanks a lot for the clarification, much appreciated. I've updated my PR now 👍
QA Eng ✅ I hacked the dashboard widget as described and observed the UI and store behaviour as briefed.