Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Fixed importing of lookup lists
Browse files Browse the repository at this point in the history
Fixes #921
  • Loading branch information
jkleinsc committed Feb 16, 2017
1 parent 9d0c479 commit cfbbb35
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
51 changes: 28 additions & 23 deletions app/admin/lookup/controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Ember from 'ember';
import BillingCategories from 'hospitalrun/mixins/billing-categories';
import csvParse from 'npm:csv-parse';
import ModalHelper from 'hospitalrun/mixins/modal-helper';
import InventoryTypeList from 'hospitalrun/mixins/inventory-type-list';
import UnitTypes from 'hospitalrun/mixins/unit-types';
import VisitTypes from 'hospitalrun/mixins/visit-types';
import { EKMixin, keyDown } from 'ember-keyboard';

const {
computed
computed, get
} = Ember;

export default Ember.Controller.extend(BillingCategories, EKMixin,
Expand Down Expand Up @@ -291,6 +292,31 @@ export default Ember.Controller.extend(BillingCategories, EKMixin,
return true;
},

_importLookupList(file) {
let fileSystem = get(this, 'fileSystem');
let lookupTypeList = get(this, 'lookupTypeList');
let lookupValues = get(lookupTypeList, 'value');
fileSystem.fileToString(file).then((values) => {
csvParse(values, { trim: true }, (err, data) =>{
data.forEach((row) => {
let [newValue] = row;
if (!lookupValues.includes(newValue)) {
lookupValues.addObject(newValue);
}
});
lookupValues.sort();
let i18n = get(this, 'i18n');
let message = i18n.t('admin.lookup.alertImportListSaveMessage');
let title = i18n.t('admin.lookup.alertImportListSaveTitle');
lookupTypeList.save().then(() => {
this.displayAlert(title, message);
this.set('importFile');
this.set('model.importFileName');
});
});
});
},

_sortValues(a, b) {
return Ember.compare(a.toLowerCase(), b.toLowerCase());
},
Expand Down Expand Up @@ -335,35 +361,14 @@ export default Ember.Controller.extend(BillingCategories, EKMixin,
}
},
importList() {
let fileSystem = this.get('fileSystem');
let fileToImport = this.get('importFile');
let lookupTypeList = this.get('lookupTypeList');
if (!fileToImport || !fileToImport.type) {
this.displayAlert(
this.get('i18n').t('admin.lookup.alertImportListTitle'),
this.get('i18n').t('admin.lookup.alertImportListMessage')
);
} else {
fileSystem.fileToDataURL(fileToImport).then(function(fileDataUrl) {
let dataUrlParts = fileDataUrl.split(',');
lookupTypeList.setProperties({
_attachments: {
file: {
content_type: fileToImport.type,
data: dataUrlParts[1]
}
},
importFile: true
});
lookupTypeList.save().then(function() {
this.displayAlert(
this.get('i18n').t('admin.lookup.alertImportListSaveTitle'),
this.get('i18n').t('admin.lookup.alertImportListSaveMessage'),
'refreshLookupLists');
this.set('importFile');
this.set('model.importFileName');
}.bind(this));
}.bind(this));
this._importLookupList(fileToImport);
}
},
updateList() {
Expand Down
3 changes: 0 additions & 3 deletions app/admin/lookup/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export default AbstractIndexRoute.extend({
actions: {
deleteValue(value) {
this.controller.send('deleteValue', value);
},
refreshLookupLists() {
this.refresh();
}
}
});
11 changes: 6 additions & 5 deletions app/models/lookup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Model } from 'ember-pouch';
import DS from 'ember-data';

const { attr } = DS;

export default Model.extend({
_attachments: DS.attr(), // Temporarily store file as attachment until it gets uploaded to the server
importFile: DS.attr('boolean', { defaultValue: false }),
value: DS.attr(''),
organizeByType: DS.attr('boolean'),
userCanAdd: DS.attr('boolean')
organizeByType: attr('boolean'),
userCanAdd: attr('boolean'),
value: attr('')
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"broccoli-manifest": "0.0.7",
"broccoli-merge-trees": "^1.1.1",
"broccoli-serviceworker": "0.1.4",
"csv-parse": "^1.2.0",
"ember-ajax": "2.5.4",
"ember-browserify": "^1.1.12",
"ember-cli": "2.10.0",
Expand Down

0 comments on commit cfbbb35

Please sign in to comment.