Skip to content

Commit

Permalink
Merge pull request #11590 from hashicorp/e-ui/breadcrumbs-service
Browse files Browse the repository at this point in the history
Refactor:  Breadcrumbs Service
  • Loading branch information
ChaiWithJai committed Jan 5, 2022
2 parents 0ff0fa1 + 3bdf661 commit 38a3759
Show file tree
Hide file tree
Showing 73 changed files with 1,492 additions and 791 deletions.
3 changes: 3 additions & 0 deletions .changelog/11590.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Add titles to breadcrumb labels in app navigation bar
```
13 changes: 0 additions & 13 deletions ui/app/components/app-breadcrumbs.js

This file was deleted.

27 changes: 27 additions & 0 deletions ui/app/components/breadcrumb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import Component from '@glimmer/component';

export default class Breadcrumb extends Component {
@service breadcrumbs;

constructor() {
super(...arguments);
assert('Provide a valid breadcrumb argument', this.args.crumb);
this.register();
}

@action register() {
this.breadcrumbs.registerBreadcrumb(this);
}

@action deregister() {
this.breadcrumbs.deregisterBreadcrumb(this);
}

willDestroy() {
super.willDestroy();
this.deregister();
}
}
1 change: 1 addition & 0 deletions ui/app/components/breadcrumbs.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{yield this.crumbs}}
10 changes: 10 additions & 0 deletions ui/app/components/breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class Breadcrumbs extends Component {
@service breadcrumbs;

get crumbs() {
return this.breadcrumbs.crumbs;
}
}
16 changes: 16 additions & 0 deletions ui/app/components/breadcrumbs/default.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<li data-test-breadcrumb-default>
<LinkTo @params={{@crumb.args}} data-test-breadcrumb={{@crumb.args.firstObject}}>
{{#if @crumb.title}}
<dl>
<dt>
{{@crumb.title}}
</dt>
<dd>
{{@crumb.label}}
</dd>
</dl>
{{else}}
{{@crumb.label}}
{{/if}}
</LinkTo>
</li>
49 changes: 49 additions & 0 deletions ui/app/components/breadcrumbs/job.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Trigger @onError={{action this.onError}} @do={{this.fetchParent}} as |trigger|>
{{did-insert trigger.fns.do}}
{{#if trigger.data.isBusy}}
<li>
<a href="#" aria-label="loading" data-test-breadcrumb="loading">
</a>
</li>
{{/if}}
{{#if trigger.data.isSuccess}}
{{#if trigger.data.result}}
<li>
<LinkTo
@route="jobs.job.index"
@model={{trigger.data.result.plainId}}
@query={{hash namespace=(or trigger.data.result.namespace.name "default")}}
data-test-breadcrumb={{"jobs.job.index"}}
>
<dl>
<dt>
Parent Job
</dt>
<dd>
{{trigger.data.result.trimmedName}}
</dd>
</dl>
</LinkTo>
</li>
{{/if}}
<li>
<LinkTo
@route="jobs.job.index"
@model={{this.job.plainId}}
@query={{hash namespace=(or this.job.namespace.name "default")}}
data-test-breadcrumb={{"jobs.job.index"}}
data-test-job-breadcrumb
>
<dl>
<dt>
{{if this.job.hasChildren "Parent Job" "Job"}}
</dt>
<dd>
{{this.job.trimmedName}}
</dd>
</dl>
</LinkTo>
</li>
{{/if}}
</Trigger>
22 changes: 22 additions & 0 deletions ui/app/components/breadcrumbs/job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import Component from '@glimmer/component';

export default class BreadcrumbsJob extends Component {
get job() {
return this.args.crumb.job;
}

@action
onError(err) {
assert(`Error: ${err.message}`);
}

@action
fetchParent() {
const hasParent = !!this.job.belongsTo('parent').id();
if (hasParent) {
return this.job.get('parent');
}
}
}
1 change: 1 addition & 0 deletions ui/app/components/trigger.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{yield (hash data=this.data fns=this.fns)}}
68 changes: 68 additions & 0 deletions ui/app/components/trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { action } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { task } from 'ember-concurrency';

const noOp = () => undefined;

export default class Trigger extends Component {
@tracked error = null;
@tracked result = null;

get isBusy() {
return this.triggerTask.isRunning;
}

get isIdle() {
return this.triggerTask.isIdle;
}

get isSuccess() {
return this.triggerTask.last?.isSuccessful;
}

get isError() {
return !!this.error;
}

get fns() {
return {
do: this.onTrigger,
};
}

get onError() {
return this.args.onError ?? noOp;
}

get onSuccess() {
return this.args.onSuccess ?? noOp;
}

get data() {
const { isBusy, isIdle, isSuccess, isError, result } = this;
return { isBusy, isIdle, isSuccess, isError, result };
}

_reset() {
this.result = null;
this.error = null;
}

@task(function*() {
this._reset();
try {
this.result = yield this.args.do();
this.onSuccess(this.result);
} catch (e) {
this.error = { Error: e };
this.onError(this.error);
}
})
triggerTask;

@action
onTrigger() {
this.triggerTask.perform();
}
}
47 changes: 47 additions & 0 deletions ui/app/controllers/allocations/allocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { qpBuilder } from 'nomad-ui/utils/classes/query-params';

export default class AllocationsAllocationController extends Controller {
@service store;

get allocation() {
return this.model;
}

get job() {
const allocation = this.model;
const jobId = allocation.belongsTo('job').id();
const job = this.store.peekRecord('job', jobId);
return job;
}

get jobNamespace() {
const jobNamespaceId = this.job.belongsTo('namespace').id();

return jobNamespaceId || 'default';
}
// Allocation breadcrumbs extend from job / task group breadcrumbs
// even though the route structure does not.
get breadcrumbs() {
const { allocation, job, jobNamespace } = this;
const jobQueryParams = qpBuilder({
jobNamespace,
});

return [
{ label: 'Jobs', args: ['jobs.index', jobQueryParams] },
{ type: 'job', job: job },
{
title: 'Task Group',
label: allocation.taskGroupName,
args: ['jobs.job.task-group', job.plainId, allocation.taskGroupName, jobQueryParams],
},
{
title: 'Allocation',
label: allocation.shortId,
args: ['allocations.allocation', allocation],
},
];
}
}
15 changes: 15 additions & 0 deletions ui/app/controllers/allocations/allocation/task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Controller from '@ember/controller';

export default class AllocationsAllocationTaskController extends Controller {
get task() {
return this.model;
}

get breadcrumb() {
return {
title: 'Task',
label: this.task.get('name'),
args: ['allocations.allocation.task', this.task.get('allocation'), this.task],
};
}
}
15 changes: 15 additions & 0 deletions ui/app/controllers/clients/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Controller from '@ember/controller';

export default class ClientsClientController extends Controller {
get client() {
return this.model;
}

get breadcrumb() {
return {
title: 'Client',
label: this.client.get('shortId'),
args: ['clients.client', this.client.get('id')],
};
}
}
21 changes: 21 additions & 0 deletions ui/app/controllers/csi/plugins/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Controller from '@ember/controller';

export default class CsiPluginsPluginController extends Controller {
get plugin() {
return this.model;
}

get breadcrumbs() {
const { plainId } = this.plugin;
return [
{
label: 'Plugins',
args: ['csi.plugins'],
},
{
label: plainId,
args: ['csi.plugins.plugin', plainId],
},
];
}
}
26 changes: 26 additions & 0 deletions ui/app/controllers/csi/volumes/volume.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action, computed } from '@ember/object';
import { qpBuilder } from 'nomad-ui/utils/classes/query-params';

export default class VolumeController extends Controller {
// Used in the template
Expand All @@ -13,6 +14,31 @@ export default class VolumeController extends Controller {
];
volumeNamespace = 'default';

get volume() {
return this.model;
}

get breadcrumbs() {
const volume = this.volume;
return [
{
label: 'Volumes',
args: [
'csi.volumes',
qpBuilder({ volumeNamespace: volume.get('namespace.name') || 'default' }),
],
},
{
label: volume.name,
args: [
'csi.volumes.volume',
volume.plainId,
qpBuilder({ volumeNamespace: volume.get('namespace.name') || 'default' }),
],
},
];
}

@computed('model.readAllocations.@each.modifyIndex')
get sortedReadAllocations() {
return this.model.readAllocations.sortBy('modifyIndex').reverse();
Expand Down
1 change: 1 addition & 0 deletions ui/app/controllers/jobs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Controller from '@ember/controller';

// The WithNamespaceResetting Mixin uses Controller Injection and requires us to keep this controller around
export default class JobsController extends Controller {}
4 changes: 4 additions & 0 deletions ui/app/controllers/jobs/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ export default class JobController extends Controller {
},
];
jobNamespace = 'default';

get job() {
return this.model;
}
}
4 changes: 4 additions & 0 deletions ui/app/controllers/jobs/job/dispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Controller from '@ember/controller';

// This may be safe to remove but we can't be sure, some route may try access this directly using this.controllerFor
export default class JobsJobDispatchController extends Controller {}
Loading

0 comments on commit 38a3759

Please sign in to comment.