<commit_before>/* eslint ember/order-in-components: 0 */
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { sort } from '@ember/object/computed';
import { validator, buildValidations } from 'ember-cp-validations';
import ValidationErrorDisplay from 'ilios-common/mixins/validation-error-display';

const Validations = buildValidations({
  title: [
    validator('presence', true),
    validator('length', {
      max: 60,
      descriptionKey: 'general.title'
    }),
  ],
  iliosAdministratorEmail: [
    validator('presence', true),
    validator('length', {
      max: 100
    }),
    validator('format', {
      type: 'email'
    }),
  ],
});

export default Component.extend(ValidationErrorDisplay, Validations, {
  currentUser: service(),
  store: service(),
  init(){
    this._super(...arguments);
    this.set('newSchools', []);
    this.set('sortSchoolsBy', ['title']);
  },
  classNames: ['school-list'],
  tagName: 'section',
  schools: null,
  newSchools: null,
  title: null,
  iliosAdministratorEmail: null,
  isSavingNewSchool: false,

  sortSchoolsBy: null,
  sortedSchools: sort('schools', 'sortSchoolsBy'),
  showNewSchoolForm: false,

  keyUp(event) {
    const keyCode = event.keyCode;
    const target = event.target;

    if ('text' !== target.type) {
      return;
    }

    if (13 === keyCode) {
      this.send('createNewSchool');
      return;
    }

    if(27 === keyCode) {
      this.send('hideNewSchoolForm');
    }
  },
  actions: {
    toggleNewSchoolForm(){
      this.set('showNewSchoolForm', !this.get('showNewSchoolForm'));
    },
    hideNewSchoolForm(){
      this.set('showNewSchoolForm', false);
      this.set('title', null);
    },
    createNewSchool() {
      this.set('isSavingNewSchool', true);
      this.send('addErrorDisplayFor', 'title');
      this.send('addErrorDisplayFor', 'iliosAdministratorEmail');
      this.validate().then(({validations}) => {
        if (validations.get('isValid')) {
          const title = this.get('title');
          const iliosAdministratorEmail = this.get('iliosAdministratorEmail');
          let newSchool = this.get('store').createRecord('school', {title, iliosAdministratorEmail});
          newSchool.save().then(school => {
            this.get('newSchools').pushObject(school);
          }).finally(() => {
            this.send('clearErrorDisplay');
            this.set('title', null);
            this.set('showNewSchoolForm', false);
            this.set('isSavingNewSchool', false);

          });
        }
      });
    },
  }
});<commit_msg>un-set "is saving" state if validation fails.<commit_after>/* eslint ember/order-in-components: 0 */
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { sort } from '@ember/object/computed';
import { validator, buildValidations } from 'ember-cp-validations';
import ValidationErrorDisplay from 'ilios-common/mixins/validation-error-display';

const Validations = buildValidations({
  title: [
    validator('presence', true),
    validator('length', {
      max: 60,
      descriptionKey: 'general.title'
    }),
  ],
  iliosAdministratorEmail: [
    validator('presence', true),
    validator('length', {
      max: 100
    }),
    validator('format', {
      type: 'email'
    }),
  ],
});

export default Component.extend(ValidationErrorDisplay, Validations, {
  currentUser: service(),
  store: service(),
  init(){
    this._super(...arguments);
    this.set('newSchools', []);
    this.set('sortSchoolsBy', ['title']);
  },
  classNames: ['school-list'],
  tagName: 'section',
  schools: null,
  newSchools: null,
  title: null,
  iliosAdministratorEmail: null,
  isSavingNewSchool: false,

  sortSchoolsBy: null,
  sortedSchools: sort('schools', 'sortSchoolsBy'),
  showNewSchoolForm: false,

  keyUp(event) {
    const keyCode = event.keyCode;
    const target = event.target;

    if ('text' !== target.type) {
      return;
    }

    if (13 === keyCode) {
      this.send('createNewSchool');
      return;
    }

    if(27 === keyCode) {
      this.send('hideNewSchoolForm');
    }
  },
  actions: {
    toggleNewSchoolForm(){
      this.set('showNewSchoolForm', !this.get('showNewSchoolForm'));
    },
    hideNewSchoolForm(){
      this.set('showNewSchoolForm', false);
      this.set('title', null);
    },
    createNewSchool() {
      this.set('isSavingNewSchool', true);
      this.send('addErrorDisplayFor', 'title');
      this.send('addErrorDisplayFor', 'iliosAdministratorEmail');
      this.validate().then(({validations}) => {
        if (validations.get('isValid')) {
          const title = this.get('title');
          const iliosAdministratorEmail = this.get('iliosAdministratorEmail');
          let newSchool = this.get('store').createRecord('school', {title, iliosAdministratorEmail});
          newSchool.save().then(school => {
            this.get('newSchools').pushObject(school);
          }).finally(() => {
            this.send('clearErrorDisplay');
            this.set('title', null);
            this.set('showNewSchoolForm', false);
            this.set('isSavingNewSchool', false);
          });
        } else {
          this.set('isSavingNewSchool', false);
        }
      });
    },
  }
});
{'fraction_non_alphanumeric': 0.160734570238546, 'fraction_numerical': 0.003786444528587656, 'mean_word_length': 3.082689335394127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4599351', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1593, 'n_words': 289}