diff --git a/app/admin/lookup/controller.js b/app/admin/lookup/controller.js index bb3d8143ed..87dba32530 100644 --- a/app/admin/lookup/controller.js +++ b/app/admin/lookup/controller.js @@ -1,5 +1,6 @@ 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'; @@ -7,7 +8,7 @@ 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, @@ -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()); }, @@ -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() { diff --git a/app/admin/lookup/route.js b/app/admin/lookup/route.js index 7676703b14..59449a8fb9 100644 --- a/app/admin/lookup/route.js +++ b/app/admin/lookup/route.js @@ -14,9 +14,6 @@ export default AbstractIndexRoute.extend({ actions: { deleteValue(value) { this.controller.send('deleteValue', value); - }, - refreshLookupLists() { - this.refresh(); } } }); diff --git a/app/models/lookup.js b/app/models/lookup.js index 4f0d21c6dc..9c0433770f 100644 --- a/app/models/lookup.js +++ b/app/models/lookup.js @@ -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('') }); diff --git a/package.json b/package.json index 5264ab99bd..0ce269bc24 100644 --- a/package.json +++ b/package.json @@ -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",