ENH: Inline adfg permit and study loc creation (#79)

Fixes #71
This commit is contained in:
Matthew Ryan Dillon 2018-03-04 20:34:42 -07:00 committed by GitHub
parent 467a8d8b64
commit 2ead72c552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 17 deletions

View file

@ -1,7 +1,11 @@
import { getProperties, set } from '@ember/object';
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { debounce } from '@ember/runloop';
import RSVP from 'rsvp';
import Changeset from 'ember-changeset';
import lookupValidator from 'ember-changeset-validations';
import config from 'ccdb-web/config/environment';
export default Component.extend({
store: service(),
@ -33,6 +37,7 @@ export default Component.extend({
});
this.set('changesets', changesets);
this.set('newStudyLocationAdmin', `${config.APP.API_HOST}/admin/locations/studylocation/add/`);
},
actions: {
@ -55,8 +60,34 @@ export default Component.extend({
changesets['hasMany'][relatedName].removeObject(changesetRecord);
},
// Gross, this side-effects by saving immediately. Someday I should clean
// this up, but for now, you have been warned.
addOption(relatedModelName, optionName, collectionAttrName, relatedAttrName, term) {
const props = getProperties(this, 'store', 'options', 'changesets');
const { store, options, changesets: { model } } = props;
let payload = {};
payload[relatedAttrName] = term;
const record = store.createRecord(relatedModelName, payload)
record.save().then((record) => {
set(options, optionName, store.peekAll(relatedModelName));
set(model, collectionAttrName, record);
});
},
updateDatasheet(changeset, event) {
changeset.set('datasheet', event.target.files[0]);
},
searchStudyLocation(term) {
return new RSVP.Promise((resolve, reject) => {
debounce(this, this._performSearch, 'study-location', { page_size: 500, code: term }, resolve, reject, 400);
});
},
},
_performSearch(model, payload, resolve, reject) {
this.get('store').query(model, payload).then((results) => {
resolve(results);
}, reject);
},
});