diff --git a/app/components/patient-orders.js b/app/components/patient-orders.js index 07e824be76..fa4f2bb598 100644 --- a/app/components/patient-orders.js +++ b/app/components/patient-orders.js @@ -1,17 +1,23 @@ import Ember from 'ember'; import FilterList from 'hospitalrun/mixins/filter-list'; import UserSession from 'hospitalrun/mixins/user-session'; + +const { + computed +} = Ember; + export default Ember.Component.extend(FilterList, UserSession, { editImagingAction: 'editImaging', editLabAction: 'editLab', editMedicationAction: 'editMedication', + filterBy: null, + filterValue: null, newImagingAction: 'newImaging', newLabAction: 'newLab', newMedicationAction: 'newMedication', showDeleteImagingAction: 'showDeleteImaging', showDeleteLabAction: 'showDeleteLab', showDeleteMedicationAction: 'showDeleteMedication', - sortBy: null, sortKey: null, sortDesc: false, orderTypeFilters: Ember.computed(function() { @@ -47,17 +53,57 @@ export default Ember.Component.extend(FilterList, UserSession, { return this.currentUserCan('delete_medication'); }), - sortedOrders: Ember.computed('filteredList', 'sortBy', 'sortDesc', function() { + filteredList: computed('orderList.[]', 'filterBy', 'filterValue', function() { + let filterBy = this.get('filterBy'); + let filterValue = this.get('filterValue'); + let orderList = this.get('orderList'); + orderList = this.filterList(orderList, filterBy, filterValue); + return orderList; + }), + + orderList: computed('visit.imaging.[]', 'visit.labs.[]', 'visit.medication.[]', function() { + let i18n = this.get('i18n'); + let imaging = this.get('visit.imaging'); + let labs = this.get('visit.labs'); + let medication = this.get('visit.medication'); + let orderList = new Ember.A(); + orderList.addObjects(imaging.map((item) => { + item.set('orderType', i18n.t('components.patientOrders.labels.imagingOrderType')); + item.set('name', item.get('imagingType.name')); + item.set('dateProcessed', item.get('imagingDate')); + this._setPermissions(item, 'canAddImaging', 'canDeleteImaging'); + return item; + })); + orderList.addObjects(labs.map((item) => { + item.set('orderType', i18n.t('components.patientOrders.labels.labOrderType')); + item.set('name', item.get('labType.name')); + item.set('dateProcessed', item.get('labDate')); + this._setPermissions(item, 'canAddLab', 'canDeleteLab'); + return item; + })); + orderList.addObjects(medication.map((item) => { + item.set('orderType', i18n.t('components.patientOrders.labels.medicationOrderType')); + item.set('name', item.get('medicationName')); + item.set('dateProcessed', item.get('prescriptionDate')); + item.set('result', ''); + item.set('notes', item.get('prescription')); + this._setPermissions(item, 'canAddMedication', 'canDeleteMedication'); + return item; + })); + return orderList; + }), + + sortedOrders: Ember.computed('filteredList', 'sortKey', 'sortDesc', function() { let filteredList = this.get('filteredList'); let sortDesc = this.get('sortDesc'); - let sortBy = this.get('sortBy'); - if (Ember.isEmpty(filteredList) || Ember.isEmpty(sortBy)) { + let sortKey = this.get('sortKey'); + if (Ember.isEmpty(filteredList) || Ember.isEmpty(sortKey)) { return filteredList; } filteredList = filteredList.sort(function(a, b) { - let compareA = a.get(sortBy); - let compareB = b.get(sortBy); - if (sortBy === 'orderType') { + let compareA = a.get(sortKey); + let compareB = b.get(sortKey); + if (sortKey === 'orderType') { compareA = compareA.toString(); compareB = compareB.toString(); } @@ -67,50 +113,12 @@ export default Ember.Component.extend(FilterList, UserSession, { return Ember.compare(compareA, compareB); } }); - this.set('sortKey', sortBy); return filteredList; }), i18n: Ember.inject.service(), visit: null, - didReceiveAttrs() { - this._super(...arguments); - Ember.RSVP.hash({ - imaging: this.get('visit.imaging'), - labs: this.get('visit.labs'), - medication: this.get('visit.medication') - }).then((results) => { - let orderList = new Ember.A(); - let i18n = this.get('i18n'); - orderList.addObjects(results.imaging.map((item) => { - item.set('orderType', i18n.t('components.patientOrders.labels.imagingOrderType')); - item.set('name', item.get('imagingType.name')); - item.set('dateProcessed', item.get('imagingDate')); - this._setPermissions(item, 'canAddImaging', 'canDeleteImaging'); - return item; - })); - orderList.addObjects(results.labs.map((item) => { - item.set('orderType', i18n.t('components.patientOrders.labels.labOrderType')); - item.set('name', item.get('labType.name')); - item.set('dateProcessed', item.get('labDate')); - this._setPermissions(item, 'canAddLab', 'canDeleteLab'); - return item; - })); - orderList.addObjects(results.medication.map((item) => { - item.set('orderType', i18n.t('components.patientOrders.labels.medicationOrderType')); - item.set('name', item.get('medicationName')); - item.set('dateProcessed', item.get('prescriptionDate')); - item.set('result', ''); - item.set('notes', item.get('prescription')); - this._setPermissions(item, 'canAddMedication', 'canDeleteMedication'); - return item; - })); - this.set('orderList', orderList); - this.set('filteredList', orderList); - }); - }, - _setPermissions(item, editPerm, deletePerm) { if (item.get('canEdit')) { if (this.get(editPerm)) { @@ -124,9 +132,10 @@ export default Ember.Component.extend(FilterList, UserSession, { actions: { filter(filterBy, filterValue) { - let orderList = this.get('orderList'); - orderList = this.filterList(orderList, filterBy, filterValue); - this.set('filteredList', orderList); + this.setProperties({ + filterBy, + filterValue + }); }, newImaging() { @@ -153,7 +162,7 @@ export default Ember.Component.extend(FilterList, UserSession, { sortByKey(sortBy, sortDesc) { this.setProperties({ - sortBy, + sortKey: sortBy, sortDesc }); }