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

Commit

Permalink
Add Always Include option on custom forms
Browse files Browse the repository at this point in the history
Resolves #896
  • Loading branch information
jkleinsc committed Dec 28, 2016
1 parent 18ca461 commit 6a11dee
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 55 deletions.
5 changes: 5 additions & 0 deletions app/admin/custom-forms/edit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const {
} = Ember;

export default AbstractEditController.extend({
customForms: Ember.inject.service(),
preview: false,
previewModel: Ember.Object.create(),
updateCapability: 'update_config',

afterUpdate() {
let customForms = this.get('customForms');
let model = this.get('model');
customForms.resetCustomFormByType(model.get('formType'));
this.displayAlert(this.get('i18n').t('admin.customForms.titles.formSaved'), this.get('i18n').t('admin.customForms.messages.formSaved', this.get('model')));
},

Expand Down Expand Up @@ -72,6 +76,7 @@ export default AbstractEditController.extend({

formTypeValues: [
'patient',
'socialwork',
'visit'
],

Expand Down
1 change: 1 addition & 0 deletions app/admin/custom-forms/edit/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
property="formType" content=formTypes prompt=" "
}}
{{em-input property="columns" label=(t 'admin.customForms.labels.columns') class="form-input-group col-xs-2 custom-form-columns"}}
{{em-checkbox label=(t 'admin.customForms.labels.alwaysInclude') property="alwaysInclude" class="form-input-group col-xs-2" }}
</div>
<h4>
{{t 'admin.customForms.titles.fields'}}
Expand Down
4 changes: 3 additions & 1 deletion app/admin/custom-forms/index/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<tr {{action 'editItem' customForm}} class="clickable">
<td class="custom-form-name">{{customForm.name}}</td>
<td>
{{t (concat 'admin.customForms.labels.' customForm.formType 'FormType')}}
{{#if customForm.formType}}
{{t (concat 'admin.customForms.labels.' customForm.formType 'FormType')}}
{{/if}}
</td>
{{#if showActions}}
<td>
Expand Down
50 changes: 33 additions & 17 deletions app/components/custom-form-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,48 @@ const {
} = Ember;

export default Ember.Component.extend(SelectValues, {
customForms: null,
customForms: Ember.inject.service(),
formType: null,
formsForType: null,
model: null,
openModalAction: 'openModal',

formsForSelect: computed('customForms', 'usedForms', function() {
didReceiveAttrs(/* attrs */) {
this._super(...arguments);
let customForms = this.get('customForms');
let usedForms = this.get('usedForms');
let formsForSelect = customForms.filter((customForm) => {
return (!usedForms.includes(customForm.get('id')));
});
formsForSelect = formsForSelect.map((customForm) => {
return {
id: customForm.get('id'),
value: customForm.get('name')
};
let formType = this.get('formType');
customForms.getCustomForms([formType]).then((forms) => {
let isDestroyed = this.get('isDestroyed');
if (!isDestroyed) {
this.set('formsForType', forms);
}
});
return formsForSelect;
},

formsForSelect: computed('formsForType', 'usedForms', function() {
let formsForType = this.get('formsForType');
let usedForms = this.get('usedForms');
if (!isEmpty(formsForType)) {
let formsForSelect = formsForType.filter((customForm) => {
return (!usedForms.includes(customForm.get('id')));
});
formsForSelect = formsForSelect.map((customForm) => {
return {
id: customForm.get('id'),
value: customForm.get('name')
};
});
return formsForSelect;
}
}),

formsToDisplay: computed('model.customForms', function() {
let customForms = this.get('customForms');
formsToDisplay: computed('formsForType', 'model.customForms', function() {
let formsForType = this.get('formsForType');
let modelForms = this.get('model.customForms');
if (!isEmpty(modelForms)) {
if (!isEmpty(modelForms) && !isEmpty(formsForType)) {
return Object.keys(modelForms).map((formId) => {
return {
form: customForms.findBy('id', formId),
form: formsForType.findBy('id', formId),
propertyPrefix: `customForms.${formId}.`
};
});
Expand All @@ -50,7 +66,7 @@ export default Ember.Component.extend(SelectValues, {

showAddButton: computed('formsForSelect', function() {
let formsForSelect = this.get('formsForSelect');
return formsForSelect.length > 0;
return !isEmpty(formsForSelect);
}),

actions: {
Expand Down
2 changes: 2 additions & 0 deletions app/locales/en/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default {

},
labels: {
alwaysInclude: 'Always Include',
checkbox: 'Checkbox',
columns: 'Columns',
colSpan: 'Number of Columns To Span',
Expand All @@ -138,6 +139,7 @@ export default {
property: 'Property',
radio: 'Radio',
select: 'Dropdown',
socialworkFormType: 'Social Work',
text: 'Text',
textarea: 'Large Text',
visitFormType: 'Visit'
Expand Down
37 changes: 22 additions & 15 deletions app/mixins/add-new-patient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ember from 'ember';
import PatientId from 'hospitalrun/mixins/patient-id';

export default Ember.Mixin.create(PatientId, {
customForms: Ember.inject.service(),
addedNewPatient: false,
newPatientId: null,

Expand All @@ -15,30 +17,35 @@ export default Ember.Mixin.create(PatientId, {
},

addNewPatient() {

let i18n = this.get('i18n');
this.displayAlert(i18n.t('alerts.pleaseWait'), i18n.t('messages.newPatientHasToBeCreated'));
this._getNewPatientId().then(function(friendlyId) {
this._getNewPatientId().then((friendlyId) => {
let patientTypeAhead = this.get('model.patientTypeAhead');
let nameParts = patientTypeAhead.split(' ');
let patientDetails = {
customForms: Ember.Object.create(),
friendlyId,
patientFullName: patientTypeAhead,
requestingController: this
};
let patient;
if (nameParts.length >= 3) {
patientDetails.firstName = nameParts[0];
patientDetails.middleName = nameParts[1];
patientDetails.lastName = nameParts.splice(2, nameParts.length).join(' ');
} else if (nameParts.length === 2) {
patientDetails.firstName = nameParts[0];
patientDetails.lastName = nameParts[1];
} else {
patientDetails.firstName = patientTypeAhead;
}
patient = this.store.createRecord('patient', patientDetails);
this.send('openModal', 'patients.quick-add', patient);
}.bind(this));
let customForms = this.get('customForms');
return customForms.setDefaultCustomForms(['patient', 'socialwork'], patientDetails).then(() => {
let patient;
if (nameParts.length >= 3) {
patientDetails.firstName = nameParts[0];
patientDetails.middleName = nameParts[1];
patientDetails.lastName = nameParts.splice(2, nameParts.length).join(' ');
} else if (nameParts.length === 2) {
patientDetails.firstName = nameParts[0];
patientDetails.lastName = nameParts[1];
} else {
patientDetails.firstName = patientTypeAhead;
}
patient = this.store.createRecord('patient', patientDetails);
this.send('openModal', 'patients.quick-add', patient);
});
});
},

_getNewPatientId() {
Expand Down
3 changes: 2 additions & 1 deletion app/models/custom-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function defaultFields() {
}

export default AbstractModel.extend({
columns: DS.attr('number'),
alwaysInclude: DS.attr('boolean'),
columns: DS.attr('number', { defaultValue: 1 }),
fields: DS.attr('custom-fields', { defaultValue: defaultFields }),
formType: DS.attr('string'),
name: DS.attr('string'),
Expand Down
1 change: 1 addition & 0 deletions app/models/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default AbstractModel.extend(DOBDays, PatientName, {
clinic: DS.attr('string'),
country: DS.attr('string'),
checkedIn: DS.attr('boolean', { defaultValue: false }),
customForms: DS.attr('custom-forms'),
dateOfBirth: DS.attr('date'),
diagnoses: DS.hasMany('diagnosis', {
async: false
Expand Down
4 changes: 2 additions & 2 deletions app/patients/edit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import ReturnTo from 'hospitalrun/mixins/return-to';
import SelectValues from 'hospitalrun/utils/select-values';
import UserSession from 'hospitalrun/mixins/user-session';
import VisitStatus from 'hospitalrun/utils/visit-statuses';

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

canAddAppointment: function() {
return this.currentUserCan('add_appointment');
}.property(),
Expand Down Expand Up @@ -96,10 +98,8 @@ export default AbstractEditController.extend(BloodTypes, DiagnosisActions, Retur

clinicList: Ember.computed.alias('patientController.clinicList'),
countryList: Ember.computed.alias('patientController.countryList'),
customSocialForm: Ember.computed.alias('patientController.customSocialForm.value'),
diagnosisList: Ember.computed.alias('patientController.diagnosisList'),
isFileSystemEnabled: Ember.computed.alias('filesystem.isFileSystemEnabled'),

pricingProfiles: Ember.computed.map('patientController.pricingProfiles', SelectValues.selectObjectMap),
sexList: Ember.computed.alias('patientController.sexList'),
statusList: Ember.computed.alias('patientController.statusList'),
Expand Down
12 changes: 10 additions & 2 deletions app/patients/edit/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import PouchDbMixin from 'hospitalrun/mixins/pouchdb';
import { translationMacro as t } from 'ember-i18n';

export default AbstractEditRoute.extend(PatientId, PatientVisits, PouchDbMixin, PatientNotes, {
customForms: Ember.inject.service(),
editTitle: t('patients.titles.edit'),
modelName: 'patient',
newTitle: t('patients.titles.new'),
Expand Down Expand Up @@ -54,8 +55,15 @@ export default AbstractEditRoute.extend(PatientId, PatientVisits, PouchDbMixin,
},

getNewData() {
return this.generateFriendlyId().then(function(friendlyId) {
return { friendlyId };
let customForms = this.get('customForms');
let newPatientData = {
customForms: Ember.Object.create()
};
return customForms.setDefaultCustomForms(['patient', 'socialwork'], newPatientData).then(() => {
return this.generateFriendlyId().then(function(friendlyId) {
newPatientData.friendlyId = friendlyId;
return newPatientData;
});
});
},

Expand Down
3 changes: 2 additions & 1 deletion app/patients/edit/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
</table>
{{/if}}
</div>
{{custom-form-manager model=model formType="patient"}}
</div>
{{#unless isNewOrDeleted}}
<div role="tabpanel" class="tab-pane" id="photos">
Expand Down Expand Up @@ -448,7 +449,7 @@
</tr>
{{/if}}
</table>
{{custom-form model=model form=customSocialForm}}
{{custom-form-manager model=model formType="socialwork"}}
</div>
</div>
</div>
Expand Down
11 changes: 8 additions & 3 deletions app/patients/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export default AbstractModuleRoute.extend(PatientId, {
}, {
name: 'countryList',
findArgs: ['lookup', 'country_list']
}, {
name: 'customSocialForm',
findArgs: ['option', 'custom_form_social']
}, {
name: 'diagnosisList',
findArgs: ['lookup', 'diagnosis_list']
Expand All @@ -31,6 +28,14 @@ export default AbstractModuleRoute.extend(PatientId, {
}, {
name: 'sexList',
findArgs: ['lookup', 'sex']
}, {
name: 'socialCustomForms',
queryArgs: ['custom-form', {
options: {
key: 'socialwork'
},
mapReduce: 'custom_form_by_type'
}]
}, {
name: 'statusList',
findArgs: ['lookup', 'patient_status_list']
Expand Down
62 changes: 62 additions & 0 deletions app/services/custom-forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Ember from 'ember';

const {
isEmpty,
set
} = Ember;

export default Ember.Service.extend({
store: Ember.inject.service(),
customForms: {},
getCustomForms(formTypes) {
let customForms = this.get('customForms');
let store = this.get('store');
let formTypesToQuery = formTypes.filter(function(formType) {
if (isEmpty(customForms[formType])) {
return true;
}
});
if (isEmpty(formTypesToQuery)) {
return Ember.RSVP.resolve(this._getCustomFormsFromCache(formTypes));
} else {
return store.query('custom-form', {
options: {
keys: formTypesToQuery
},
mapReduce: 'custom_form_by_type'
}).then((forms) => {
formTypesToQuery.forEach((formType) => {
customForms[formType] = forms.filterBy('formType', formType);
});
return this._getCustomFormsFromCache(formTypes);
});
}
},

_getCustomFormsFromCache(formTypes) {
let customForms = this.get('customForms');
let returnForms = [];
formTypes.forEach((formType) => {
returnForms.addObjects(customForms[formType]);
});
return returnForms;
},

resetCustomFormByType(formType) {
let customForms = this.get('customForms');
delete customForms[formType];
},

setDefaultCustomForms(customFormNames, model) {
return this.getCustomForms(customFormNames).then((customForms) => {
if (!isEmpty(customForms)) {
customForms.forEach((customForm) => {
if (customForm.get('alwaysInclude')) {
set(model, `customForms.${customForm.get('id')}`, Ember.Object.create());
}
});
}
});
}

});
2 changes: 0 additions & 2 deletions app/visits/edit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const {

export default AbstractEditController.extend(AddNewPatient, ChargeActions, DiagnosisActions, PatientSubmodule, PatientNotes, UserSession, VisitTypes, {
visitsController: Ember.inject.controller('visits'),

additionalButtons: computed('model.status', function() {
let buttonProps = {
buttonIcon: 'glyphicon glyphicon-log-out',
Expand Down Expand Up @@ -103,7 +102,6 @@ export default AbstractEditController.extend(AddNewPatient, ChargeActions, Diagn
chargePricingCategory: 'Ward',
chargeRoute: 'visits.charge',
createNewPatient: false,
customForms: Ember.computed.alias('visitsController.customForms'),
diagnosisList: Ember.computed.alias('visitsController.diagnosisList'),
findPatientVisits: false,
hideChargeHeader: true,
Expand Down
Loading

0 comments on commit 6a11dee

Please sign in to comment.