Skip to content

[ experimental/nav-menus ] Add/menu location management#21351

Merged
noisysocks merged 17 commits intomasterfrom
add/menu-location-management
May 13, 2020
Merged

[ experimental/nav-menus ] Add/menu location management#21351
noisysocks merged 17 commits intomasterfrom
add/menu-location-management

Conversation

@draganescu
Copy link
Copy Markdown
Contributor

Description

Closes #21283

How has this been tested?

Tested locally by:

  1. Installing the Gutenberg plugin
  2. Enabling the Navigation screen experiment
  3. From the Gutenberg menu click on Navigation (beta)
  4. Click on Menu locations tab
  5. Try to assign a menu to a location

Screenshots

Screenshot 2020-04-02 at 18 10 01

Types of changes

Non breaking additions to the edit-navigaion package.

Known issues

  • The updates are not always saved
  • Sometimes assigning a menu to a location changes other menus as well

These issues are either b/c the payload sent to the API is wrong or there is some bug in the menus endpoint. cc @spacedmonkey

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2020

Size Change: +877 B (0%)

Total Size: 825 kB

Filename Size Change
build/annotations/index.js 3.62 kB +1 B
build/api-fetch/index.js 4.08 kB -3 B (0%)
build/block-directory/index.js 6.61 kB -7 B (0%)
build/block-editor/index.js 102 kB -4 B (0%)
build/blocks/index.js 48.1 kB -1 B
build/components/index.js 180 kB +11 B (0%)
build/core-data/index.js 11.4 kB -10 B (0%)
build/data-controls/index.js 1.29 kB +2 B (0%)
build/data/index.js 8.44 kB -10 B (0%)
build/edit-navigation/index.js 5.3 kB +896 B (16%) ⚠️
build/edit-post/index.js 28 kB +2 B (0%)
build/edit-site/index.js 12.1 kB -2 B (0%)
build/editor/index.js 44.3 kB -1 B
build/hooks/index.js 2.13 kB -6 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB +1 B
build/list-reusable-blocks/index.js 3.13 kB +4 B (0%)
build/media-utils/index.js 5.29 kB +2 B (0%)
build/notices/index.js 1.79 kB +1 B
build/server-side-render/index.js 2.68 kB +2 B (0%)
build/wordcount/index.js 1.17 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/style-rtl.css 10.3 kB 0 B
build/block-editor/style.css 10.3 kB 0 B
build/block-library/editor-rtl.css 7.12 kB 0 B
build/block-library/editor.css 7.12 kB 0 B
build/block-library/index.js 115 kB 0 B
build/block-library/style-rtl.css 7.37 kB 0 B
build/block-library/style.css 7.37 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/components/style-rtl.css 17 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/style-rtl.css 618 B 0 B
build/edit-navigation/style.css 617 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/style-rtl.css 5.22 kB 0 B
build/edit-site/style.css 5.22 kB 0 B
build/edit-widgets/index.js 8.37 kB 0 B
build/edit-widgets/style-rtl.css 4.69 kB 0 B
build/edit-widgets/style.css 4.69 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 734 B 0 B
build/format-library/index.js 7.63 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B

compressed-size-action

@noisysocks noisysocks requested a review from talldan April 3, 2020 04:17
@spacedmonkey
Copy link
Copy Markdown
Member

Looking at the requests you are sending here, they seem wrong.

Requests are being sent the menus endpoint like so.
GET /__experimental/menus {locations: ["social"]}

However, this is wrong. You should be updating a menu by it's id.
This is the request you need to go.
POST /__experimental/menus/<menu_id> {locations: ["social"]}

Work noting that all locations need to be passed in the locations array. The REST API will always alright existing changes. So you want to assign main menu to all locations, the request must be

POST /__experimental/menus/<menu_id> {locations: ["social", "main"]}

Hope this is helpful.

@draganescu
Copy link
Copy Markdown
Contributor Author

draganescu commented Apr 5, 2020

Howdy @spacedmonkey this is weird since I see this in my tools:

Screenshot 2020-04-05 at 23 05 59

So it looks like I am sending the <menu_id> . And that works. What doesn't seem to work is sending quick POST to assign menus to locations:

menu-saving

In this GIF you can see that if I only set one menu it is saved right, but if I try to set two menus to two locations, they get set wrongly (one of the two is not saved). Is there anything in the API that allows me one bulk call? Also why would there be a problem to send serial requests as long as they're about different menus and different locations?

@spacedmonkey
Copy link
Copy Markdown
Member

These are the request I see.

Screenshot from 2020-04-05 21-56-51
Screenshot from 2020-04-05 21-56-39

Not what I am seeing. I can't see a id in those request. Also the id should not be passed as a query param in this case, should be pass in the url, like a post update.

Screenshot from 2020-04-05 21-59-07
As you can see, the above is not get request but a post.

Updating a term uses editable methods ( WP_REST_Server::EDITABLE ) which are POST, PUT, PATCH/

Another issue with the PR, is that I think that javascript will need to await for the response of the apiFetch. I think if all requests are sent at the same time, that there will be a race conditions where data is overridden.

@draganescu draganescu force-pushed the add/menu-location-management branch from 6bab1cf to 635d261 Compare April 6, 2020 09:09
@draganescu
Copy link
Copy Markdown
Contributor Author

Great catch with the "await"-ing for menu location assignment @spacedmonkey, I didn't expect that to be the need since I've always sent different id/location combos so I thought they're independent. Now with awaiting, multiple assignment in one save works all right.

@draganescu
Copy link
Copy Markdown
Contributor Author

@noisysocks I've addressed your review, but still need to refactor so that assigning one menu to multiple locations works.

@draganescu
Copy link
Copy Markdown
Contributor Author

This is almost ready, but now upon changing tabs the state is not correct as the menu locations are not fetched again and the edits are not saved in the state.

@draganescu
Copy link
Copy Markdown
Contributor Author

Ok, we're good for a re-review now.

@draganescu draganescu changed the title Add/menu location management [ experimental/nav-menus ] Add/menu location management Apr 13, 2020
@spacedmonkey
Copy link
Copy Markdown
Member

Couple of tweaks to improve the code but the new changes are now sending the correct data to the api.

@draganescu draganescu force-pushed the add/menu-location-management branch 2 times, most recently from 41174fc to 993f59f Compare April 30, 2020 13:44
@draganescu draganescu marked this pull request as draft May 1, 2020 07:38
@draganescu draganescu marked this pull request as ready for review May 3, 2020 12:04
@noisysocks
Copy link
Copy Markdown
Member

noisysocks commented May 4, 2020

I really like the idea of using a hook for this! I think can we can take it a little bit further and have the hook fully encapsulate the data structure that the REST API needs and expose only an API that aligns closely with the UI of the Manage Locations tab. What do you think?

const [
	// Object that maps menu location names to an object containing menu location name, description and (possibly null) assigned menu ID.
	menuLocations, // Record<string, { name: string, description: string, assignedMenuID: number? }>
	// Function that assigns the given menu ID to the given menu location.
	assignMenuLocationMenu, // ( menuLocationName: string, menuID: number ) => void
	// Function that persists the current state of menuLocations using REST API.
	saveMenuLocations, // () => void
] = useMenuLocations();

...

<form onSubmit={ saveMenuLocations }>
	...
	{ map( menuLocations, ( menuLocation ) => (
		{ menuLocation.description }
		...
		<SelectControl
			options={ availableMenuOptions }
			value={ menuLocation.assignedMenuID }
			onChange={ ( menuID ) => {
				assignMenuLocationMenu( menuLocation.name, menuID );
			} }
		/>
	) ) }
	...
</form>

@draganescu
Copy link
Copy Markdown
Contributor Author

Hey @noisysocks your review suggestions are now baked in. Also, I've moved some of the form's onSubmit logic into the hook and re-renamed the variables in the MenuLocationsEditor as with the hook they are easier to grasp.

Unfortunately we need to keep MenuSelectControl and the tiny logic there b/c we have to know the previously selected value.

Copy link
Copy Markdown
Contributor Author

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

Ok so I've added all the review items.

@adamziel removing the api refresh, does not show the spinner, which creates the need for a notification. I would like to add notifications in a separate PR.

@noisysocks the new control is gone, so less code \o/

@adamziel
Copy link
Copy Markdown
Contributor

adamziel commented May 8, 2020

@draganescu sounds good!

Also, I think this should use a single save request in the grand scheme of things, not a request per menu location. That being said, it doesn't have to be in this PR - let's proceed as it is.

@adamziel
Copy link
Copy Markdown
Contributor

adamziel commented May 11, 2020

@draganescu it works! 👍One last note I have is that the state manipulation logic is pretty verbose. Storing state as [menuId] and {locationId: menuId} instead of {menuId: [locationIds]} could probably shave ~50 lines of code - what do you think?

@draganescu
Copy link
Copy Markdown
Contributor Author

Storing state as [menuId] and {locationId: menuId} instead of {menuId: [locationIds]} could probably shave ~50 lines of code - what do you think?

That is very tempting but: {menuId: [locationIds]} is still gonna be made at some point b/c that is how the API expects us to set locations, a list of them posted to a menu id. Most of the code currently in assignMenuToLocation is for storing unassigned menus. I may be missing the point here :)

@draganescu draganescu force-pushed the add/menu-location-management branch from d70221a to 3126b46 Compare May 11, 2020 12:50
@noisysocks noisysocks dismissed adamziel’s stale review May 13, 2020 05:01

Feedback was addressed.

@noisysocks
Copy link
Copy Markdown
Member

Let's get this in and iterate! Thanks @draganescu!

@noisysocks noisysocks merged commit 3607ce6 into master May 13, 2020
@noisysocks noisysocks deleted the add/menu-location-management branch May 13, 2020 05:02
@github-actions github-actions bot added this to the Gutenberg 8.2 milestone May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Manage Locations tab on Navigation Screen

4 participants