Skip to content

Commit

Permalink
(fix) Improve Form Field Value Initialization and Performance (#436)
Browse files Browse the repository at this point in the history
* (fix) prevent initial value overwrite and optimize initialization

* Cache refined value

* cleanup
  • Loading branch information
samuelmale authored Dec 3, 2024
1 parent 66aca31 commit 3f16428
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 179 deletions.
1 change: 0 additions & 1 deletion __mocks__/forms/rfe-forms/sample_ui-select-ext.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
}
}
},
"meta": {},
"id": "patient_transfer_location"
},
{
Expand Down
9 changes: 5 additions & 4 deletions src/adapters/inline-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const InlineDateAdapter: FormFieldValueAdapter = {
} else {
targetField.meta.submission.newValue.obsDatetime = dateString;
}
} else if (!hasSubmission(targetField) && targetField.meta.previousValue) {
if (isEmpty(value) && isEmpty(targetField.meta.previousValue.obsDatetime)) {
} else if (!hasSubmission(targetField) && targetField.meta.initialValue?.omrsObject) {
if (isEmpty(value) && isEmpty((targetField.meta.initialValue.omrsObject as OpenmrsResource)?.obsDatetime)) {
return null;
}
// generate submission
Expand All @@ -37,8 +37,9 @@ export const InlineDateAdapter: FormFieldValueAdapter = {
if (encounter) {
const targetFieldId = field.id.split('_inline_date')[0];
const targetField = context.formFields.find((field) => field.id === targetFieldId);
if (targetField?.meta.previousValue?.obsDatetime) {
return parseDate(targetField?.meta.previousValue?.obsDatetime);
const targetFieldInitialObs = targetField?.meta.initialValue?.omrsObject as OpenmrsResource;
if (targetFieldInitialObs?.obsDatetime) {
return parseDate(targetFieldInitialObs.obsDatetime);
}
}
return null;
Expand Down
242 changes: 137 additions & 105 deletions src/adapters/obs-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
},
meta: {
previousValue: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: 'Can be discharged in next visit',
initialValue: {
omrsObject: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: 'Can be discharged in next visit',
},
},
},
id: 'visit-note',
Expand Down Expand Up @@ -236,15 +238,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
},
meta: {
previousValue: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: '5197ca4f-f0f7-4e63-9a68-8614224dce44',
initialValue: {
omrsObject: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: '5197ca4f-f0f7-4e63-9a68-8614224dce44',
},
},
},
id: 'hts-result',
Expand Down Expand Up @@ -276,22 +280,24 @@ describe('ObsAdapter - transformFieldValue', () => {
],
},
meta: {
previousValue: [
{
uuid: 'f2487de5-e55f-4689-8791-0c919179818b',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3hbkj9-b6d8-4eju-8f37-0b14f5347jv9',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
formFieldNamespace: 'rfe-forms',
formFieldPath: 'rfe-forms-past-patient-programs',
value: {
uuid: '105e7ad6-c1fd-11eb-8529-0242ac130ju9',
initialValue: {
omrsObject: [
{
uuid: 'f2487de5-e55f-4689-8791-0c919179818b',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3hbkj9-b6d8-4eju-8f37-0b14f5347jv9',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
formFieldNamespace: 'rfe-forms',
formFieldPath: 'rfe-forms-past-patient-programs',
value: {
uuid: '105e7ad6-c1fd-11eb-8529-0242ac130ju9',
},
},
},
],
],
},
},
id: 'past-patient-programs',
};
Expand Down Expand Up @@ -327,15 +333,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
},
meta: {
previousValue: {
uuid: 'bca7277f-a726-4d3d-9db8-40937228ead5',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: new Date(2020, 11, 16),
initialValue: {
omrsObject: {
uuid: 'bca7277f-a726-4d3d-9db8-40937228ead5',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: new Date(2020, 11, 16),
},
},
},
id: 'hts-date',
Expand Down Expand Up @@ -365,15 +373,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
},
meta: {
previousValue: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: 'Can be discharged in next visit',
initialValue: {
omrsObject: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: 'Can be discharged in next visit',
},
},
},
id: 'visit-note',
Expand Down Expand Up @@ -401,15 +411,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
},
meta: {
previousValue: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: '5197ca4f-f0f7-4e63-9a68-8614224dce44',
initialValue: {
omrsObject: {
uuid: '305ed1fc-c1fd-11eb-8529-0242ac130003',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '1c43b05b-b6d8-4eb5-8f37-0b14f5347568',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: '5197ca4f-f0f7-4e63-9a68-8614224dce44',
},
},
},
id: 'hts-result',
Expand All @@ -435,20 +447,22 @@ describe('ObsAdapter - transformFieldValue', () => {
answers: [{ label: 'Option 1', concept: '105e7ad6-c1fd-11eb-8529-0242ac130ju9' }],
},
meta: {
previousValue: [
{
uuid: 'f2487de5-e55f-4689-8791-0c919179818b',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3hbkj9-b6d8-4eju-8f37-0b14f5347jv9',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: {
uuid: '105e7ad6-c1fd-11eb-8529-0242ac130ju9',
initialValue: {
omrsObject: [
{
uuid: 'f2487de5-e55f-4689-8791-0c919179818b',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3hbkj9-b6d8-4eju-8f37-0b14f5347jv9',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: {
uuid: '105e7ad6-c1fd-11eb-8529-0242ac130ju9',
},
},
},
],
],
},
},
id: 'past-patient-programs',
};
Expand Down Expand Up @@ -479,15 +493,17 @@ describe('ObsAdapter - transformFieldValue', () => {
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
},
meta: {
previousValue: {
uuid: 'bca7277f-a726-4d3d-9db8-40937228ead5',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: htsDate,
initialValue: {
omrsObject: {
uuid: 'bca7277f-a726-4d3d-9db8-40937228ead5',
person: '833db896-c1f0-11eb-8529-0242ac130003',
concept: '3e432ad5-7b19-4866-a68f-abf0d9f52a01',
location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
order: null,
groupMembers: [],
voided: false,
value: htsDate,
},
},
},
id: 'hts-date',
Expand Down Expand Up @@ -790,9 +806,11 @@ describe('hasPreviousObsValueChanged', () => {
rendering: 'radio',
},
meta: {
previousValue: {
value: {
uuid: '1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
initialValue: {
omrsObject: {
value: {
uuid: '1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
},
},
},
Expand All @@ -808,8 +826,10 @@ describe('hasPreviousObsValueChanged', () => {
rendering: 'date',
},
meta: {
previousValue: {
value: '2024-05-01T19:49:50.000+0000',
initialValue: {
omrsObject: {
value: '2024-05-01T19:49:50.000+0000',
},
},
},
} as any as FormField;
Expand All @@ -823,8 +843,10 @@ describe('hasPreviousObsValueChanged', () => {
rendering: 'datetime',
},
meta: {
previousValue: {
value: '2024-04-01T19:50:00.000+0000',
initialValue: {
omrsObject: {
value: '2024-04-01T19:50:00.000+0000',
},
},
},
} as any as FormField;
Expand All @@ -837,8 +859,10 @@ describe('hasPreviousObsValueChanged', () => {
rendering: 'text',
},
meta: {
previousValue: {
value: 'Text value',
initialValue: {
omrsObject: {
value: 'Text value',
},
},
},
} as any as FormField;
Expand Down Expand Up @@ -1128,10 +1152,12 @@ describe('ObsAdapter - handling nested obsGroups', () => {

const healthCenterField = fields.questions[0];
healthCenterField.meta = {
previousValue: {
uuid: 'health-center-uuid',
value: {
uuid: '1588AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
initialValue: {
omrsObject: {
uuid: 'health-center-uuid',
value: {
uuid: '1588AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
},
},
};
Expand All @@ -1151,9 +1177,11 @@ describe('ObsAdapter - handling nested obsGroups', () => {

const commentField = fields.questions[1].questions[0];
commentField.meta = {
previousValue: {
uuid: 'comment-uuid',
value: 'Test comment for nested group',
initialValue: {
omrsObject: {
uuid: 'comment-uuid',
value: 'Test comment for nested group',
},
},
};

Expand All @@ -1173,9 +1201,11 @@ describe('ObsAdapter - handling nested obsGroups', () => {

const commentField = fields.questions[1].questions[0];
commentField.meta = {
previousValue: {
uuid: 'comment-uuid',
value: 'Test comment for nested group',
initialValue: {
omrsObject: {
uuid: 'comment-uuid',
value: 'Test comment for nested group',
},
},
};

Expand All @@ -1188,10 +1218,12 @@ describe('ObsAdapter - handling nested obsGroups', () => {

const diagnosisField = fields.questions[1].questions[1];
diagnosisField.meta = {
previousValue: {
uuid: 'diagnosis-uuid',
value: {
uuid: '159394AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
initialValue: {
omrsObject: {
uuid: 'diagnosis-uuid',
value: {
uuid: '159394AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
},
},
};
Expand Down
Loading

0 comments on commit 3f16428

Please sign in to comment.