open-nomad/ui/app/components/job-editor.js

111 lines
2.6 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
2018-08-21 23:43:46 +00:00
import { assert } from '@ember/debug';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { task } from 'ember-concurrency';
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
import classic from 'ember-classic-decorator';
@classic
export default class JobEditor extends Component {
@service store;
@service config;
'data-test-job-editor' = true;
job = null;
onSubmit() {}
2018-08-21 23:43:46 +00:00
@computed('_context')
get context() {
return this._context;
}
2018-08-21 23:43:46 +00:00
set context(value) {
const allowedValues = ['new', 'edit'];
2021-12-28 16:08:12 +00:00
assert(
`context must be one of: ${allowedValues.join(', ')}`,
allowedValues.includes(value)
);
this.set('_context', value);
}
_context = null;
parseError = null;
planError = null;
runError = null;
planOutput = null;
@localStorageProperty('nomadMessageJobPlan', true) showPlanMessage;
@localStorageProperty('nomadMessageJobEditor', true) showEditorMessage;
@computed('planOutput')
get stage() {
2019-03-26 07:46:44 +00:00
return this.planOutput ? 'plan' : 'editor';
}
2021-12-28 14:45:20 +00:00
@(task(function* () {
this.reset();
try {
2019-03-26 07:46:44 +00:00
yield this.job.parse();
} catch (err) {
const error = messageFromAdapterError(err) || 'Could not parse input';
this.set('parseError', error);
this.scrollToError();
return;
}
try {
2019-03-26 07:46:44 +00:00
const plan = yield this.job.plan();
this.set('planOutput', plan);
} catch (err) {
const error = messageFromAdapterError(err) || 'Could not plan job';
this.set('planError', error);
this.scrollToError();
}
}).drop())
plan;
2021-12-28 14:45:20 +00:00
@task(function* () {
try {
2019-03-26 07:46:44 +00:00
if (this.context === 'new') {
yield this.job.run();
} else {
2019-03-26 07:46:44 +00:00
yield this.job.update();
}
const id = this.get('job.plainId');
const namespace = this.get('job.namespace.name') || 'default';
this.reset();
// Treat the job as ephemeral and only provide ID parts.
2019-03-26 07:46:44 +00:00
this.onSubmit(id, namespace);
} catch (err) {
const error = messageFromAdapterError(err) || 'Could not submit job';
this.set('runError', error);
this.set('planOutput', null);
this.scrollToError();
}
})
submit;
reset() {
this.set('planOutput', null);
this.set('planError', null);
this.set('parseError', null);
this.set('runError', null);
}
scrollToError() {
if (!this.get('config.isTest')) {
window.scrollTo(0, 0);
}
}
}