Skip to content

Commit

Permalink
ui: When certain menus are selected reset either nspace/partition (#1…
Browse files Browse the repository at this point in the history
…1479)

For our dc, nspace and partition 'bucket' menus, sometimes when selecting one 'bucket' we need to reset a different 'bucket' back to the one that your token has by default (or the default if not). For example when switching to a different partition whilst you are in a non-default namespace of another partition, we need to switch you to the token default namespace of the partition you are switching to.
  • Loading branch information
johncowen authored Nov 17, 2021
1 parent eddd648 commit 2ef9ea0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .changelog/11479.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: When switching partitions reset the namespace back to the tokens default namespace or default
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{#if (can "use partitions")}}
{{#if (can "choose partitions" dc=@dc)}}
{{#let
(or @partition 'default')
as |partition|}}
<li
class="partitions"
data-test-partition-menu
Expand All @@ -9,7 +12,7 @@
@position="left"
as |components api|>
<BlockSlot @name="trigger">
{{@partition}}
{{partition}}
</BlockSlot>
<BlockSlot @name="menu">
{{#let components.MenuItem components.MenuSeparator as |MenuItem MenuSeparator|}}
Expand All @@ -24,10 +27,10 @@
/>
{{#each (reject-by 'DeletedAt' @partitions) as |item|}}
<MenuItem
class={{if (eq @partition item.Name) 'is-active'}}
class={{if (eq partition item.Name) 'is-active'}}
@href={{href-to '.' params=(hash
partition=item.Name
nspace=(if (gt @nspace.length 0) @nspace undefined)
nspace=undefined
)}}
>
<BlockSlot @name="label">
Expand Down Expand Up @@ -55,8 +58,9 @@
class="partition"
aria-label="Admin Partition"
>
{{@partition}}
{{partition}}
</li>
{{/let}}
{{/if}}
{{/if}}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
(if item.Local ' is-local')
(if item.Primary ' is-primary')
}}
@href={{href-to '.' params=(hash dc=item.Name)}}
@href={{href-to '.' params=(hash
dc=item.Name
partition=undefined
nspace=(if (gt @nspace.length 0) @nspace undefined)
)}}
>
<BlockSlot @name="label">
{{item.Name}}
Expand Down
44 changes: 22 additions & 22 deletions ui/packages/consul-ui/app/locations/fsm-with-optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,19 @@ export default class FSMWithOptionalLocation {
withOptional = false;
break;
}
const router = this.router._routerMicrolib;
let url;
try {
url = router.generate(routeName, ...params, {
queryParams: {},
});
} catch(e) {
if(
!(this.router.currentRouteName.startsWith('docs') &&
e.message.startsWith('There is no route named ')
)
) {
if(this.router.currentRouteName.startsWith('docs') && routeName.startsWith('dc')) {
params.unshift('dc-1');
url = router.generate(routeName, ...params, {
queryParams: {},
});
} else {
throw e;
}
if(this.router.currentRouteName.startsWith('docs.')) {
// If we are in docs, then add a default dc as there won't be one in the
// URL
params.unshift(env('CONSUL_DATACENTER_PRIMARY'));
if(routeName.startsWith('dc')) {
// if its an app URL replace it with debugging instead of linking
return `console://${routeName} <= ${JSON.stringify(params)}`;
}
return `console://${routeName} <= ${JSON.stringify(params)}`;
}
const router = this.router._routerMicrolib;
const url = router.generate(routeName, ...params, {
queryParams: {},
});
return this.formatURL(url, hash, withOptional);
}

Expand All @@ -241,13 +231,23 @@ export default class FSMWithOptionalLocation {
console.log(`location.transitionTo: ${url.substr(10)}`);
return true;
}
const previousOptional = Object.entries(this.optionalParams());
const transitionURL = this.getURLForTransition(url);
if (this._previousURL === transitionURL) {
// probably an optional parameter change
// probably an optional parameter change as the Ember URLs are the same
// whereas the entire URL is different
this.dispatch('push', url);
return Promise.resolve();
// this.setURL(url);
} else {
const currentOptional = this.optionalParams();
if(previousOptional.some(([key, value]) => currentOptional[key] !== value)) {
// an optional parameter change and a normal param change as the Ember
// URLs are different and we know the optional params changed
// TODO: Consider changing the above previousURL === transitionURL to
// use the same 'check the optionalParams' approach
this.dispatch('push', url);
}
// use ember to transition, which will eventually come around to use location.setURL
return this.container.lookup('router:main').transitionTo(transitionURL);
}
Expand Down
7 changes: 3 additions & 4 deletions ui/packages/consul-ui/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ as |source|>

{{! Make sure we guess and default to the right params when not found }}
{{#let
(if (can "use partitions") (or route.params.partition notfound.partition token.Partition 'default') 'default')
(if (can "use nspaces") (or route.params.nspace notfound.nspace token.Namespace 'default') 'default')
(if (can "use partitions") (or route.params.partition notfound.partition token.Partition '') '')
(if (can "use nspaces") (or route.params.nspace notfound.nspace token.Namespace '') '')
as |partition nspace|}}

{{! Make sure we have enough to show the app chrome}}
{{! Don't show anything until we have a list of DCs }}
<DataSource
@src={{uri '/*/*/*/datacenters'}}
as |dcs|>

{{! Once we have a list of DCs make sure the DC we are asking for exists }}
{{! If not use the DC that the UI is running in }}
{{#let
Expand Down Expand Up @@ -103,7 +102,7 @@ as |dcs|>
dcs.data

as |dc dcs|}}
{{#if (and (gt dc.Name.length 0) dcs nspace partition)}}
{{#if (and (gt dc.Name.length 0) dcs)}}

{{! figure out our current DC and convert it to a model }}
<DataSource
Expand Down
8 changes: 1 addition & 7 deletions ui/packages/consul-ui/lib/startup/templates/body.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ ${
function(get, obj) {
Object.entries(obj).forEach(([key, value]) => {
if(get(key)) {
const appName = '${appName}';
const appNameJS = appName.split('-').map((item, i) => i ? \`\${item.substr(0, 1).toUpperCase()}\${item.substr(1)}\` : item).join('');
const $script = document.createElement('script');
$script.setAttribute('data-app-name', '${appName}');
$script.setAttribute('data-${appName}-routing', '');
$script.setAttribute('src', \`${rootURL}assets/\${value}/routes.js\`);
document.body.appendChild($script);
document.write(\`\\x3Cscript data-app-name="${appName}" data-${appName}-routing src="${rootURL}assets/\${value}/routes.js">\\x3C/script>\`);
}
});
}
Expand Down

0 comments on commit 2ef9ea0

Please sign in to comment.