<commit_before>// will eventually hook into data loading/saving using the
// derived model's identifiedBy strings

import { observable, computed } from 'mobx';
import { find, isNil } from 'lodash';
import { session } from 'mobx-decorated-models';

const FLUX_NEW = /_CREATING_/;

export class BaseModel {

  constructor(attrs) {
    if (attrs) { this.update(attrs); }
  }

  apiRequestsInProgress = observable.map();

  @computed get hasApiRequestPending() {
    return !!this.apiRequestsInProgress.size;
  }

  @computed get isNew() {
    const idField = find(this.constructor.$schema.values(), { type: 'identifier' });
    const id = this[idField.name];
    return isNil(id) || FLUX_NEW.test(id);
  }

  loaded(req) {
    this.update(req.data);
  }
}

// export decorators so they can be easily imported into model classes
export {
  identifiedBy, identifier, hasMany, belongsTo, field, session,
} from 'mobx-decorated-models';

export {
  computed,
  observable,
} from 'mobx';<commit_msg>Make a few api helper methods on base model<commit_after>// will eventually hook into data loading/saving using the
// derived model's identifiedBy strings

import { observable, computed, action } from 'mobx';
import { find, isNil, get } from 'lodash';


const FLUX_NEW = /_CREATING_/;

export class BaseModel {

  constructor(attrs) {
    if (attrs) { this.update(attrs); }
  }

  apiRequestsInProgress = observable.map();
  @observable apiErrors;

  @computed get hasApiRequestPending() {
    return !!this.apiRequestsInProgress.size;
  }

  @computed get isNew() {
    const idField = find(this.constructor.$schema.values(), { type: 'identifier' });
    const id = this[idField.name];
    return isNil(id) || FLUX_NEW.test(id);
  }

  loaded(req) {
    this.update(req.data);
  }

  onApiRequestComplete({ data }) {
    this.update(data);
  }

  @action
  setApiErrors(error) {
    const errors = get(error, 'response.data.errors');
    if (errors) {
      this.apiErrors = {};
      errors.forEach(e => this.apiErrors[e.code] = e.message);
      error.isRecorded = true;
    } else {
      this.apiErrors = null;
    }
  }

}

// export decorators so they can be easily imported into model classes
export {
  identifiedBy, identifier, hasMany, belongsTo, field, session,
} from 'mobx-decorated-models';

export {
  computed,
  observable,
} from 'mobx';
{'fraction_non_alphanumeric': 0.12856534695615154, 'fraction_numerical': 0.0, 'mean_word_length': 3.6534653465346536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '324301', 'n_tokens_mistral': 765, 'n_tokens_neox': 747, 'n_words': 197}