Skip to content

Commit

Permalink
Move the namespace query param up the route hierarchy
Browse files Browse the repository at this point in the history
This makes it active on all job routes
  • Loading branch information
DingoEatingFuzz authored and chelseakomlo committed Oct 25, 2017
1 parent 2272e1d commit f5bb57b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 38 deletions.
28 changes: 28 additions & 0 deletions ui/app/controllers/jobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Ember from 'ember';

const { Controller, inject, observer } = Ember;

export default Controller.extend({
system: inject.service(),

queryParams: {
jobNamespace: 'namespace',
},

jobNamespace: 'default',

// The namespace query param should act as an alias to the system active namespace.
// But query param defaults can't be CPs: https://github.com/emberjs/ember.js/issues/9819
syncNamespaceService: observer('jobNamespace', function() {
const newNamespace = this.get('jobNamespace');
const currentNamespace = this.get('system.activeNamespace.id');
const bothAreDefault =
(currentNamespace == undefined || currentNamespace === 'default') &&
(newNamespace == undefined || newNamespace === 'default');

if (currentNamespace !== newNamespace && !bothAreDefault) {
this.set('system.activeNamespace', newNamespace);
this.send('refreshRoute');
}
}),
});
37 changes: 12 additions & 25 deletions ui/app/controllers/jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember';
import Sortable from 'nomad-ui/mixins/sortable';
import Searchable from 'nomad-ui/mixins/searchable';

const { Controller, computed, observer, inject } = Ember;
const { Controller, computed, inject } = Ember;

export default Controller.extend(Sortable, Searchable, {
system: inject.service(),
Expand All @@ -16,48 +16,35 @@ export default Controller.extend(Sortable, Searchable, {
searchTerm: 'search',
sortProperty: 'sort',
sortDescending: 'desc',
jobNamespace: 'namespace',
},

currentPage: 1,
pageSize: 10,
jobNamespace: 'default',

sortProperty: 'modifyIndex',
sortDescending: true,

searchProps: computed(() => ['id', 'name']),

filteredJobs: computed('model.[]', 'jobNamespace', function() {
if (this.get('system.namespaces.length')) {
return this.get('model').filterBy('namespace.name', this.get('jobNamespace'));
} else {
return this.get('model');
filteredJobs: computed(
'model.[]',
'system.activeNamespace',
'system.namespaces.length',
function() {
if (this.get('system.namespaces.length')) {
return this.get('model').filterBy('namespace.id', this.get('system.activeNamespace.id'));
} else {
return this.get('model');
}
}
}),
),

listToSort: computed.alias('filteredJobs'),
listToSearch: computed.alias('listSorted'),
sortedJobs: computed.alias('listSearched'),

isShowingDeploymentDetails: false,

// The namespace query param should act as an alias to the system active namespace.
// But query param defaults can't be CPs: https://github.com/emberjs/ember.js/issues/9819
syncNamespaceService: observer('jobNamespace', function() {
const newNamespace = this.get('jobNamespace');
const currentNamespace = this.get('system.activeNamespace.id');
const bothAreDefault =
currentNamespace == undefined ||
(currentNamespace === 'default' && newNamespace == undefined) ||
newNamespace === 'default';

if (currentNamespace !== newNamespace && !bothAreDefault) {
this.set('system.activeNamespace', newNamespace);
this.send('refreshRoute');
}
}),

actions: {
gotoJob(job) {
this.transitionToRoute('jobs.job', job);
Expand Down
22 changes: 21 additions & 1 deletion ui/app/routes/jobs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Ember from 'ember';

const { Route, inject } = Ember;
const { Route, inject, run } = Ember;

export default Route.extend({
system: inject.service(),
Expand All @@ -14,6 +14,26 @@ export default Route.extend({
return this.get('store').findAll('job');
},

syncToController(controller) {
const namespace = this.get('system.activeNamespace.id');

// The run next is necessary to let the controller figure
// itself out before updating QPs.
// See: https://github.com/emberjs/ember.js/issues/5465
run.next(() => {
if (namespace && namespace !== 'default') {
controller.set('jobNamespace', namespace);
} else {
controller.set('jobNamespace', 'default');
}
});
},

setupController(controller) {
this.syncToController(controller);
return this._super(...arguments);
},

actions: {
refreshRoute() {
this.refresh();
Expand Down
17 changes: 5 additions & 12 deletions ui/app/routes/jobs/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import Ember from 'ember';

const { Route, inject } = Ember;
const { Route } = Ember;

export default Route.extend({
system: inject.service(),

setupController(controller) {
this._super(...arguments);

const namespace = this.get('system.activeNamespace.id');
if (namespace && namespace !== 'default') {
controller.set('jobNamespace', namespace);
} else {
controller.set('jobNamespace', 'default');
}
actions: {
refreshRoute() {
return true;
},
},
});

0 comments on commit f5bb57b

Please sign in to comment.