github.com/manicqin/nomad@v0.9.5/ui/app/components/job-editor.js (about) 1 import Component from '@ember/component'; 2 import { assert } from '@ember/debug'; 3 import { inject as service } from '@ember/service'; 4 import { computed } from '@ember/object'; 5 import { task } from 'ember-concurrency'; 6 import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error'; 7 import localStorageProperty from 'nomad-ui/utils/properties/local-storage'; 8 9 export default Component.extend({ 10 store: service(), 11 config: service(), 12 13 'data-test-job-editor': true, 14 15 job: null, 16 onSubmit() {}, 17 context: computed({ 18 get() { 19 return this._context; 20 }, 21 set(key, value) { 22 const allowedValues = ['new', 'edit']; 23 24 assert(`context must be one of: ${allowedValues.join(', ')}`, allowedValues.includes(value)); 25 26 this.set('_context', value); 27 return value; 28 }, 29 }), 30 31 _context: null, 32 parseError: null, 33 planError: null, 34 runError: null, 35 36 planOutput: null, 37 38 showPlanMessage: localStorageProperty('nomadMessageJobPlan', true), 39 showEditorMessage: localStorageProperty('nomadMessageJobEditor', true), 40 41 stage: computed('planOutput', function() { 42 return this.planOutput ? 'plan' : 'editor'; 43 }), 44 45 plan: task(function*() { 46 this.reset(); 47 48 try { 49 yield this.job.parse(); 50 } catch (err) { 51 const error = messageFromAdapterError(err) || 'Could not parse input'; 52 this.set('parseError', error); 53 this.scrollToError(); 54 return; 55 } 56 57 try { 58 const plan = yield this.job.plan(); 59 this.set('planOutput', plan); 60 } catch (err) { 61 const error = messageFromAdapterError(err) || 'Could not plan job'; 62 this.set('planError', error); 63 this.scrollToError(); 64 } 65 }).drop(), 66 67 submit: task(function*() { 68 try { 69 if (this.context === 'new') { 70 yield this.job.run(); 71 } else { 72 yield this.job.update(); 73 } 74 75 const id = this.get('job.plainId'); 76 const namespace = this.get('job.namespace.name') || 'default'; 77 78 this.reset(); 79 80 // Treat the job as ephemeral and only provide ID parts. 81 this.onSubmit(id, namespace); 82 } catch (err) { 83 const error = messageFromAdapterError(err) || 'Could not submit job'; 84 this.set('runError', error); 85 this.set('planOutput', null); 86 this.scrollToError(); 87 } 88 }), 89 90 reset() { 91 this.set('planOutput', null); 92 this.set('planError', null); 93 this.set('parseError', null); 94 this.set('runError', null); 95 }, 96 97 scrollToError() { 98 if (!this.get('config.isTest')) { 99 window.scrollTo(0, 0); 100 } 101 }, 102 });