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