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

Commit

Permalink
Fix allergies bug (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeolabadmus authored and jkleinsc committed Mar 31, 2017
1 parent 7453611 commit 7d961ec
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 112 deletions.
70 changes: 70 additions & 0 deletions app/allergy/edit/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Ember from 'ember';
import AbstractEditController from 'hospitalrun/controllers/abstract-edit-controller';

const {
computed,
computed: {
alias
},
get,
inject,
set
} = Ember;

export default AbstractEditController.extend({
i18n: inject.service(),
editController: alias('model.editController'),
newAllergy: false,

additionalButtons: computed('model.isNew', function() {
let model = get(this, 'model');
let btn = get(this, 'i18n').t('buttons.delete');
let isNew = get(model, 'isNew');
if (!isNew) {
return [{
class: 'btn btn-default warning',
buttonAction: 'deleteAllergy',
buttonIcon: 'octicon octicon-x',
buttonText: btn
}];
}
}),

title: Ember.computed('model', function() {
let model = get(this, 'model');
let i18n = get(this, 'i18n');
let isNew = get(model, 'isNew');
if (!isNew) {
return i18n.t('allergies.titles.editAllergy');
} else {
return i18n.t('allergies.titles.addAllergy');
}
}),

beforeUpdate() {
let allergy = get(this, 'model');
set(this, 'newAllergy', get(allergy, 'isNew'));
return Ember.RSVP.Promise.resolve();
},

afterUpdate(allergy) {
let newAllergy = get(this, 'newAllergy');
if (newAllergy) {
get(this, 'editController').send('addAllergy', allergy);
set(this, 'name', '');
} else {
this.send('closeModal');
}
},

actions: {
cancel() {
this.send('closeModal');
},

deleteAllergy() {
let allergy = get(this, 'model');
get(this, 'editController').send('deleteAllergy', allergy);
}
}
});
16 changes: 16 additions & 0 deletions app/allergy/edit/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#modal-dialog
title=title
updateButtonText=updateButtonText
hideCancelButton=true
isUpdateDisabled=isUpdateDisabled
updateButtonAction=updateButtonAction
additionalButtons=additionalButtons
}}

{{#em-form model=model submitButton=false }}
<div class="row">
{{em-input class="col-xs-12 form-group required test-allergy" label=(t 'allergies.labels.allergyName') property="name"}}
</div>
{{/em-form}}

{{/modal-dialog}}
92 changes: 6 additions & 86 deletions app/components/medication-allergy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,11 @@ const {

export default Ember.Component.extend({
classNames: 'ps-info-group long-form',
store: Ember.inject.service(),
i18n: Ember.inject.service(),
patient: null,
displayModal: false,
currentAllergy: false,

buttonConfirmText: computed('currentAllergy', function() {
let i18n = this.get('i18n');
let currentAllergy = this.get('currentAllergy');
if (currentAllergy) {
return i18n.t('buttons.update');
} else {
return i18n.t('buttons.add');
}
}),

additionalButtons: computed('currentAllergy', function() {
let currentAllergy = this.get('currentAllergy');
let btn = this.get('i18n').t('buttons.delete');
if (currentAllergy) {
return [{
class: 'btn btn-default warning',
buttonAction: 'deleteAllergy',
buttonIcon: 'octicon octicon-x',
buttonText: btn
}];
}
}),
canAddAllergy: null,
patient: null,
editAllergyAction: 'editAllergy',
showAddAllergyAction: 'showAddAllergy',

showAllergies: computed('canAddAllergy', 'patient.allergies.[]', {
get() {
Expand All @@ -45,70 +22,13 @@ export default Ember.Component.extend({
}
}),

modalTitle: Ember.computed('currentAllergy', function() {
let currentAllergy = this.get('currentAllergy');
let i18n = this.get('i18n');
if (currentAllergy) {
return i18n.t('allergies.titles.editAllergy');
} else {
return i18n.t('allergies.titles.addAllergy');
}
}),

closeAllergyModal() {
this.set('currentAllergy', false);
this.set('displayModal', false);
},

actions: {

cancel() {
this.closeAllergyModal();
},

closeModal() {
this.closeAllergyModal();
},

editAllergy(allergy) {
this.set('currentAllergy', allergy);
this.set('displayModal', true);
this.sendAction('editAllergyAction', allergy);
},

createNewAllergy() {
this.set('displayModal', true);
},

updateAllergy() {
let model = this.get('patient');
let allergyModel = this.get('currentAllergy');
if (!allergyModel) {
allergyModel = this.get('store').createRecord('allergy', {
name: this.get('name')
});
allergyModel.save().then(() => {
model.get('allergies').pushObject(allergyModel);
model.save().then(() => {
this.set('name', '');
this.closeAllergyModal();
});
});
} else {
allergyModel.save().then(() => {
this.closeAllergyModal();
});
}
},
deleteAllergy() {
let allergy = this.get('currentAllergy');
let patient = this.get('patient');
let patientAllergies = patient.get('allergies');
allergy.destroyRecord().then(() => {
patientAllergies.removeObject(allergy);
patient.save().then(() => {
this.closeAllergyModal();
});
});
this.sendAction('showAddAllergyAction');
}
}
});
10 changes: 10 additions & 0 deletions app/components/patient-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export default Ember.Component.extend(UserSession, {
diagnosisContainer: null,
diagnosisList: null,
disablePatientLink: false,
editAllergyAction: 'editAllergy',
editDiagnosisAction: 'editDiagnosis',
editOperativePlanAction: 'editOperativePlan',
editOperationReportAction: 'editOperationReport',
editProcedureAction: 'editProcedure',
hideInActiveDiagnoses: true,
patient: null,
patientProcedures: null,
showAddAllergyAction: 'showAddAllergy',
showAddDiagnosisAction: 'showAddDiagnosis',
showPatientAction: 'showPatient',

Expand Down Expand Up @@ -86,6 +88,10 @@ export default Ember.Component.extend(UserSession, {
}
},

editAllergy(allergy) {
this.sendAction('editAllergyAction', allergy);
},

editDiagnosis(diagnosis) {
this.sendAction('editDiagnosisAction', diagnosis);
},
Expand All @@ -99,6 +105,10 @@ export default Ember.Component.extend(UserSession, {
}
},

showAddAllergy() {
this.sendAction('showAddAllergyAction');
},

showAddDiagnosis() {
this.sendAction('showAddDiagnosisAction');
}
Expand Down
41 changes: 41 additions & 0 deletions app/mixins/allergy-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Ember from 'ember';

const {
get,
set
} = Ember;

export default Ember.Mixin.create({
openAllergyModal(allergy) {
set(allergy, 'editController', this);
this.send('openModal', 'allergy.edit', allergy);
},

savePatientAllergy(patient, allergy) {
get(patient, 'allergies').pushObject(allergy);
patient.save().then(() => {
this.silentUpdate('closeModal');
});
},

deletePatientAllergy(patient, allergy) {
let patientAllergies = get(patient, 'allergies');
allergy.destroyRecord().then(() => {
patientAllergies.removeObject(allergy);
patient.save().then(() => {
this.send('closeModal');
});
});
},

actions: {
editAllergy(allergy) {
this.openAllergyModal(allergy);
},

showAddAllergy() {
let newAllergy = get(this, 'store').createRecord('allergy');
this.openAllergyModal(newAllergy);
}
}
});
8 changes: 7 additions & 1 deletion app/models/allergy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ export default AbstractModel.extend({
icd9CMCode: DS.attr('string'),
icd10Code: DS.attr('string'),
// Associations
patient: DS.belongsTo('patient')
patient: DS.belongsTo('patient'),

validations: {
name: {
presence: true
}
}
});
13 changes: 12 additions & 1 deletion app/patients/edit/controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AbstractEditController from 'hospitalrun/controllers/abstract-edit-controller';
import AllergyActions from 'hospitalrun/mixins/allergy-actions';
import BloodTypes from 'hospitalrun/mixins/blood-types';
import DiagnosisActions from 'hospitalrun/mixins/diagnosis-actions';
import Ember from 'ember';
Expand All @@ -15,7 +16,7 @@ const {
isEmpty
} = Ember;

export default AbstractEditController.extend(BloodTypes, DiagnosisActions, ReturnTo, UserSession, PatientId, PatientNotes, PatientVisits, {
export default AbstractEditController.extend(AllergyActions, BloodTypes, DiagnosisActions, ReturnTo, UserSession, PatientId, PatientNotes, PatientVisits, {

canAddAppointment: function() {
return this.currentUserCan('add_appointment');
Expand Down Expand Up @@ -182,6 +183,11 @@ export default AbstractEditController.extend(BloodTypes, DiagnosisActions, Retur
updateCapability: 'add_patient',

actions: {
addAllergy(newAllergy) {
let patient = get(this, 'model');
this.savePatientAllergy(patient, newAllergy);
},

addContact(newContact) {
let additionalContacts = this.getWithDefault('model.additionalContacts', []);
let model = this.get('model');
Expand Down Expand Up @@ -216,6 +222,11 @@ export default AbstractEditController.extend(BloodTypes, DiagnosisActions, Retur
this.send('closeModal');
},

deleteAllergy(allergy) {
let patient = get(this, 'model');
this.deletePatientAllergy(patient, allergy);
},

deleteContact(model) {
let contact = model.get('contactToDelete');
let additionalContacts = this.get('model.additionalContacts');
Expand Down
25 changes: 2 additions & 23 deletions app/templates/components/medication-allergy.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,17 @@
<label class="ps-info-label wide">{{t 'allergies.labels.patientAllergy'}}</label>

{{#if canAddAllergy}}
<a class="clickable" {{action "createNewAllergy"}}><span class="octicon octicon-plus"></span> {{t 'allergies.buttons.addAllergy'}}</a>
<a class="clickable" {{action "createNewAllergy" bubbles=false }}><span class="octicon octicon-plus"></span> {{t 'allergies.buttons.addAllergy'}}</a>
{{/if}}

<div class="ps-info-data-block allergy-list">
{{#each patient.allergies as |allergy index|}}
{{#unless (eq index 0)}}, {{/unless}}
{{#if canAddAllergy}}
<a class="clickable allergy-button" {{action "editAllergy" allergy}}>{{allergy.name}}</a>
<a class="clickable allergy-button" {{action "editAllergy" allergy bubbles=false }}>{{allergy.name}}</a>
{{else}}
<span>{{allergy.name}}</span>
{{/if}}
{{/each}}
</div>
{{/if}}

{{#if displayModal}}
{{#modal-dialog
title=modalTitle
updateButtonText=buttonConfirmText
hideCancelButton=true
updateButtonAction='updateAllergy'
additionalButtons=additionalButtons
}}
<div class="row">
<div class="col-xs-12 form-group">
<label>{{t "allergies.labels.allergyName"}}</label>
{{#if currentAllergy}}
{{input class="form-control" value=currentAllergy.name}}
{{else}}
{{input class="form-control" value=name}}
{{/if}}
</div>
</div>
{{/modal-dialog}}
{{/if}}
Loading

0 comments on commit 7d961ec

Please sign in to comment.