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

ui kv routing fix #5650

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ui/app/components/navigate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export default Component.extend(FocusOnInsertMixin, {
return `cert/${key}`;
},
onEnter: function(val) {
let baseKey = this.get('baseKey');
let mode = this.get('mode');
let { baseKey, mode } = this;

Choose a reason for hiding this comment

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

So jealous of your native getters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I realized we could do destructuring like this now, it was a bit of a 🤯.

let extraParams = this.get('extraNavParams');
if (mode.startsWith('secrets') && (!val || val === baseKey)) {
return;
Expand All @@ -78,7 +77,7 @@ export default Component.extend(FocusOnInsertMixin, {
if (baseKey) {
this.transitionToRoute(route, this.keyForNav(baseKey), {
queryParams: {
initialKey: val.replace(this.keyForNav(baseKey), ''),
initialKey: val,
},
});
} else {
Expand Down
5 changes: 3 additions & 2 deletions ui/app/routes/vault/cluster/secrets/backend/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import Route from '@ember/routing/route';

export default Route.extend({
beforeModel() {
let { secret } = this.paramsFor(this.routeName);
let { secret, initialKey } = this.paramsFor(this.routeName);
let qp = initialKey || secret;
return this.transitionTo('vault.cluster.secrets.backend.create-root', {
queryParams: { initialKey: secret },
queryParams: { initialKey: qp },
});
},
});
11 changes: 9 additions & 2 deletions ui/app/routes/vault/cluster/secrets/backend/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export default Route.extend({
return model;
})
.catch(err => {
// if we're at the root we don't want to throw
if (backendModel && err.httpStatus === 404 && secret === '') {
return [];
} else {
// else we're throwing and dealing with this in the error action
throw err;
}
}),
Expand Down Expand Up @@ -109,6 +111,11 @@ export default Route.extend({
let { backend } = this.paramsFor('vault.cluster.secrets.backend');
let backendModel = this.store.peekRecord('secret-engine', backend);
let has404 = this.get('has404');
// only clear store cache if this is a new model
if (secret !== controller.get('baseKey.id')) {
this.store.clearAllDatasets();
}

controller.set('hasModel', true);
controller.setProperties({
model,
Expand Down Expand Up @@ -153,9 +160,9 @@ export default Route.extend({
if (hasModel && error.httpStatus === 404) {
this.set('has404', true);
transition.abort();
} else {
return true;
return false;
}
return true;
},

willTransition(transition) {
Expand Down
80 changes: 80 additions & 0 deletions ui/app/services/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Service from '@ember/service';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';
export function extractRouteArgs(args) {
args = args.slice();
let possibleQueryParams = args[args.length - 1];

let queryParams;
if (possibleQueryParams && possibleQueryParams.hasOwnProperty('queryParams')) {
queryParams = args.pop().queryParams;
} else {
queryParams = {};
}

let routeName = args.shift();

return { routeName, models: args, queryParams };
}
//https://github.com/emberjs/ember.js/blob/abf753a3d494830dc9e95b1337b3654b671b11be/packages/ember-routing/lib/utils.js#L210
export function shallowEqual(a, b) {
let k;
let aCount = 0;
let bCount = 0;
for (k in a) {
if (a.hasOwnProperty(k)) {
if (a[k] !== b[k]) {
return false;
}
aCount++;
}
}

for (k in b) {
if (b.hasOwnProperty(k)) {
bCount++;
}
}

return aCount === bCount;
}

export default Service.extend({
routing: service('-routing'),
router: alias('routing.router'),
transitionTo() {
let r = this.router;
return r.transitionTo.call(r, ...arguments);
},
replaceWith() {
let r = this.router;
return r.replaceWith.call(r, ...arguments);
},
urlFor() {
let r = this.router;
return r.generate.call(r, ...arguments);
},
currentURL: alias('router.currentURL'),
currentRouteName: alias('router.currentRouteName'),
rootURL: alias('router.rootURL'),
location: alias('router.location'),

//adapted from:
// https://github.com/emberjs/ember.js/blob/abf753a3d494830dc9e95b1337b3654b671b11be/packages/ember-routing/lib/services/router.js#L220
isActive(...args) {
let { routeName, models, queryParams } = extractRouteArgs(args);
let routerMicrolib = this.router._routerMicrolib;

if (!routerMicrolib.isActiveIntent(routeName, models, null)) {
return false;
}
let hasQueryParams = Object.keys(queryParams).length > 0;

if (hasQueryParams) {
this.router._prepareQueryParams(routeName, models, queryParams, true /* fromRouterService */);
return shallowEqual(queryParams, routerMicrolib.state.queryParams);
}

return true;
},
});

Choose a reason for hiding this comment

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

This file makes me full of sads...but I get it, bugs are bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah same ☹️ - I may peak a little bit at the router service if I get some time.

1 change: 1 addition & 0 deletions ui/app/templates/components/navigate-input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
disabled={{disabled}}
value={{@filter}}
placeholder={{ or @placeholder "Filter keys" }}
type="text"

oninput={{action "handleInput" value="target.value"}}
onkeyup={{action "handleKeyUp" }}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/components/secret-list-header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{{#secret-link
mode="create"
secret=''
queryParams=(query-params initialKey=baseKey.id)
queryParams=(query-params initialKey=(or filter baseKey.id))
class="button has-icon-right is-ghost is-compact"
data-test-secret-create=true
}}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/vault/cluster/secrets/backend/list.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{secret-list-header isCertTab=(eq tab "certs") model=backendModel baseKey=baseKey backendCrumb=backendCrumb}}
{{secret-list-header isCertTab=(eq tab "certs") model=backendModel baseKey=baseKey backendCrumb=backendCrumb filter=filter}}

{{#with (options-for-backend backendType tab) as |options|}}
<div class="box is-sideless has-background-grey-lighter has-short-padding is-marginless">
Expand Down