<commit_before>import Ember from 'ember';
import moment from 'moment';
import { validator, buildValidations } from 'ember-cp-validations';
import ValidationErrorDisplay from 'ilios/mixins/validation-error-display';

const { Component, inject, isPresent } = Ember;
const { service } = inject;

const Validations = buildValidations({
  title: [
    validator('presence', true),
    validator('length', {
      min: 3,
      max: 200
    }),
  ],
  selectedYear: [
    validator('presence', true)
  ],
  currentSchool: [
    validator('presence', true)
  ],
});

export default Component.extend(Validations, ValidationErrorDisplay, {
  didReceiveAttrs(){
    this._super(...arguments);

    //build a list of years to seelct from
    let thisYear = parseInt(moment().format('YYYY'));
    let years = [
      thisYear-2,
      thisYear-1,
      thisYear,
      thisYear+1,
      thisYear+2
    ];

    this.set('years', years);

    const currentYear = this.get('currentYear');
    if (isPresent(currentYear) && years.contains(parseInt(currentYear.get('title')))) {
      this.set('selectedYear', currentYear.get('title'));
    } else {
      this.set('selectedYear', thisYear);
    }
  },
  tagName: 'section',
  classNames: ['new-course', 'new-result', 'form-container'],

  i18n: service(),
  store: service(),

  years: [],

  currentYear: null,
  currentSchool: null,
  title: null,
  selectedYear: null,
  isSaving: false,

  actions: {
    setYear(year) {
      this.set('selectedYear', year);
    },
    save() {
      this.set('isSaving', true);
      this.send('addErrorDisplayFor', 'title');
      this.validate().then(({validations}) => {
        if (validations.get('isValid')) {
          this.send('removeErrorDisplayFor', 'title');
          let course = this.get('store').createRecord('course', {
            title: this.get('title'),
            school: this.get('currentSchool'),
            year: this.get('selectedYear'),
            level: 1,
          });

          this.get('save')(course).finally(() =>{
            this.set('isSaving', true);
          });
        }
      });
    },

    cancel() {
      this.sendAction('cancel', this.get('course'));
    },

    changeValue(value) {
      this.set('title', value);
    }<commit_msg>reset isSaving flag if form validation fails.<commit_after>import Ember from 'ember';
import moment from 'moment';
import { validator, buildValidations } from 'ember-cp-validations';
import ValidationErrorDisplay from 'ilios/mixins/validation-error-display';

const { Component, inject, isPresent } = Ember;
const { service } = inject;

const Validations = buildValidations({
  title: [
    validator('presence', true),
    validator('length', {
      min: 3,
      max: 200
    }),
  ],
  selectedYear: [
    validator('presence', true)
  ],
  currentSchool: [
    validator('presence', true)
  ],
});

export default Component.extend(Validations, ValidationErrorDisplay, {
  didReceiveAttrs(){
    this._super(...arguments);

    //build a list of years to seelct from
    let thisYear = parseInt(moment().format('YYYY'));
    let years = [
      thisYear-2,
      thisYear-1,
      thisYear,
      thisYear+1,
      thisYear+2
    ];

    this.set('years', years);

    const currentYear = this.get('currentYear');
    if (isPresent(currentYear) && years.contains(parseInt(currentYear.get('title')))) {
      this.set('selectedYear', currentYear.get('title'));
    } else {
      this.set('selectedYear', thisYear);
    }
  },
  tagName: 'section',
  classNames: ['new-course', 'new-result', 'form-container'],

  i18n: service(),
  store: service(),

  years: [],

  currentYear: null,
  currentSchool: null,
  title: null,
  selectedYear: null,
  isSaving: false,

  actions: {
    setYear(year) {
      this.set('selectedYear', year);
    },
    save() {
      this.set('isSaving', true);
      this.send('addErrorDisplayFor', 'title');
      this.validate().then(({validations}) => {
        if (validations.get('isValid')) {
          this.send('removeErrorDisplayFor', 'title');
          let course = this.get('store').createRecord('course', {
            title: this.get('title'),
            school: this.get('currentSchool'),
            year: this.get('selectedYear'),
            level: 1,
          });

          this.get('save')(course).finally(() =>{
            this.set('isSaving', true);
          });
        } else {
          this.set('isSaving', false);
        }
      });
    },

    cancel() {
      this.sendAction('cancel', this.get('course'));
    },

    changeValue(value) {
      this.set('title', value);
    }
{'fraction_non_alphanumeric': 0.16420503909643788, 'fraction_numerical': 0.0047784535186794095, 'mean_word_length': 2.7870065789473686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410034', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1398, 'n_words': 277}