Skip to content

Commit e12c64f

Browse files
committed
refactoring to introduce historical data
1 parent d9c1319 commit e12c64f

5 files changed

Lines changed: 139 additions & 40 deletions

File tree

common-data-defs.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const patientBasePermissions = [
2323
{id: 'body-weight', name: 'Body weight'},
2424
]
2525

26-
const formContent = [
26+
const formProfileContent = [
2727
{
2828
streamId: 'profile-name',
2929
eventType: 'contact/name',
@@ -45,6 +45,14 @@ const formContent = [
4545
label: 'Nationality',
4646
dataFieldKey: 'nationality',
4747
},
48+
{
49+
streamId: 'profile-sex',
50+
eventType: 'attributes/biological-sex',
51+
type: 'select',
52+
options: [{ value: 'male', label: 'Male' }, { value: 'female', label: 'Female'}],
53+
label: 'Sex',
54+
dataFieldKey: 'sex',
55+
},
4856
{
4957
streamId: 'family-children',
5058
eventType: 'count/generic',
@@ -68,8 +76,63 @@ const formContent = [
6876
}
6977
];
7078

79+
// -- fields lists -- //
80+
const formHistoricalContent = [
81+
{
82+
streamId: 'ttc-tta',
83+
eventType: 'fertility-intention/ttc-tta',
84+
label: 'Trying to conceive / Avoiding pregnancy',
85+
dataFieldKey: 'ttc-tta',
86+
type: 'select',
87+
options: [
88+
{
89+
value: 0,
90+
label: 'TTA - Not taking risks. Would take all available measures to end a pregnancy.'
91+
},
92+
{
93+
value: 1,
94+
label: 'TTA - Not taking risks. Would strongly consider placing baby for adoption.'
95+
},
96+
{
97+
value: 2,
98+
label: 'TTA - Not taking risks. Would need some time, maybe counseling. Ultimately keeping the pregnancy.'
99+
},
100+
{
101+
value: 4,
102+
label: 'TTA - Not taking risks. Currently content with family size but a surprise pregnancy would be welcome.'
103+
},
104+
{
105+
value: 5,
106+
label: 'TTW/TTA - “Loosely TTA” known risks are taken in the fertile window. “OOPS” pregnancy would be welcome.'
107+
},
108+
{
109+
value: 6,
110+
label: 'TTW - Charting only for health/curiosity. Unprotected intercourse happens whenever. Pregnancy very welcome.'
111+
},
112+
{
113+
value: 7,
114+
label: 'TTW/TTC - Pregnancy very welcome (moving up the scale in very near future).'
115+
},
116+
{
117+
value: 8,
118+
label: 'TTC - “Excited to start/grow a family TTC” Intentional intercourse every cycle. Excited to start/grow a family. But would not use any fertility treatments if needed.'
119+
},
120+
{
121+
value: 9,
122+
label: 'TTC - “Highly hopeful TTC” Intentional intercourse every cycle. Would consider some but not all fertility treatments if needed.'
123+
},
124+
{
125+
value: 10,
126+
label: 'TTC - “ Seriously TTC” Intentional intercourse every cycle. Would pursue any/ALL fertility treatments or procedures if needed.'
127+
}
128+
]
129+
}
130+
]
131+
132+
71133
const dataDefs = {
72134
patientBaseStreams,
73135
patientBasePermissions,
74-
formContent
136+
formProfileContent,
137+
formHistoricalContent
75138
};

dr-lib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ async function getPatientsList () {
8585
* get the list of rows for the table
8686
*/
8787
function getFields () {
88-
return dataDefs.formContent;
88+
return dataDefs.formProfileContent;
8989
};
9090

9191
const dataFieldsCache = {};
9292
function initFieldsCache () {
9393
if (Object.keys(dataFieldsCache).length !== 0) return;
94-
for (const formField of dataDefs.formContent) {
94+
for (const formField of dataDefs.formProfileContent) {
9595
const dataFieldId = formField.streamId + ':' + formField.eventType;
9696
dataFieldsCache[dataFieldId] = formField;
9797
}

patient-controler.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
window.onload = (event) => {
99
stateChange('loggedOut');
1010
patientLib.showLoginButton('login-button', stateChange);
11-
document.getElementById('submit-button').addEventListener("click", submitForm);
11+
document.getElementById('submit-button-profile').addEventListener("click", function () { submitForm('profile'); });
12+
document.getElementById('submit-button-historical').addEventListener("click", function () { submitForm('historical'); });
1213
};
1314

1415
function stateChange(state) {
1516
if (state === 'loggedIN') {
1617
document.getElementById('please-login').style.visibility = 'hidden';
1718
document.getElementById('card-form').style.visibility = 'visible';
1819
getDoctorInfo();
19-
updateFormContent();
20+
updateFormContent('profile');
21+
updateFormContent('historical');
2022
} else {
2123
document.getElementById('please-login').style.visibility = 'visible';
2224
document.getElementById('card-form').style.visibility = 'hidden';
@@ -40,11 +42,11 @@ async function getDoctorInfo() {
4042
/**
4143
* Take the from content from the definition and actual values and create the HTML
4244
*/
43-
async function updateFormContent() {
44-
const formData = await patientLib.getFormContent();
45+
async function updateFormContent(formKey) {
46+
const formData = await patientLib.getFormContent(formKey);
4547
console.log('Form content:', formData);
4648

47-
document.getElementById('inputs-list').innerHTML = ''; // Clear previous content
49+
document.getElementById('inputs-' + formKey).innerHTML = ''; // Clear previous content
4850
for (let i = 0; i < formData.length; i++) {
4951
const formField = formData[i];
5052
const fieldId = formField.id;
@@ -57,28 +59,30 @@ async function updateFormContent() {
5759
if (fieldType === 'text' || fieldType === 'number') {
5860
fieldHTML += `<input type="${fieldType}" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
5961
} else if (fieldType === 'select') {
60-
fieldHTML += `<select id="${fieldId}">`;
61-
for (const option of fieldValue) {
62-
fieldHTML += `<option value="${option.value}">${option.label}</option>`;
62+
fieldHTML += `<select id="${fieldId}" class="form-control">`;
63+
fieldHTML += `<option value="">--</option>`;
64+
for (const option of formField.options) {
65+
const selected = (option.value === fieldValue) ? 'selected' : '';
66+
fieldHTML += `<option value="${option.value}" ${selected}>${option.label}</option>`;
6367
}
6468
fieldHTML += `</select>`;
6569
}
6670
// Append the HTML to the form
67-
document.getElementById('inputs-list').innerHTML += fieldHTML;
71+
document.getElementById('inputs-' + formKey).innerHTML += fieldHTML;
6872
}
6973
}
7074

7175
/**
7276
* Submit the form and send the data to the API
7377
*/
74-
async function submitForm() {
78+
async function submitForm(formKey) {
7579
const values = {};
76-
const formData = await patientLib.getFormContent();
80+
const formData = await patientLib.getFormContent(formKey);
7781
for (let i = 0; i < formData.length; i++) {
7882
const field = formData[i];
7983
const fieldId = field.id;
8084
const fieldValue = document.getElementById(fieldId).value.trim();
8185
values[field.id] = fieldValue; // Store the value in the values object
8286
}
83-
await patientLib.handleFormSubmit(values);
87+
await patientLib.handleFormSubmit(formKey, values);
8488
};

patient-lib.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,34 @@ async function initSharingWithDr (formApiEndpoint) {
125125
// ---------------- form content ---------------- //
126126

127127

128-
// local copy of formContent + actual values
129-
let formData = null;
130-
async function getFormContent () {
131-
if (formData) { return formData; }
132-
formData = structuredClone(dataDefs.formContent);
133-
formInitialized = true; // prevent multiple calls to this function
128+
// local copy of formProfileContent + actual values
129+
let formProfileData = null;
130+
async function getFormContent (formKey) {
131+
if (formKey === 'profile') {
132+
return getFormProfileContent();
133+
}
134+
if (formKey === 'historical') {
135+
return getFormHistoricalContent();
136+
}
137+
return [];
138+
}
139+
140+
async function getFormHistoricalContent () {
141+
console.log('## getFormHistoricalContent');
142+
const formHistoricalData = structuredClone(dataDefs.formHistoricalContent);
143+
formHistoricalData.forEach(field => {
144+
field.id = 'field-historical-' + field.dataFieldKey;
145+
});
146+
return formHistoricalData;
147+
}
148+
149+
150+
async function getFormProfileContent () {
151+
if (formProfileData) { return formProfileData; }
152+
formProfileData = structuredClone(dataDefs.formProfileContent);
134153

135154
// get the values from the API
136-
const apiCalls = formData.map(field => ({
155+
const apiCalls = formProfileData.map(field => ({
137156
method: 'events.get',
138157
params: {
139158
streams: [field.streamId],
@@ -145,16 +164,16 @@ async function getFormContent () {
145164
const res = await connection.api(apiCalls);
146165
for (let i = 0; i < res.length; i++) {
147166
const e = res[i];
148-
const field = formData[i];
149-
field.id = 'field-' + i; // generate a unique id for the field
167+
const field = formProfileData[i];
168+
field.id = 'field-profile-' + field.dataFieldKey;
150169
console.log('## getFormContent ' + i, e);
151170
if (e.events && e.events.length > 0) {
152171
const event = e.events[0];
153172
field.value = event.content;
154173
field.eventId = event.id; // will allow t track if the event is to be updated
155174
}
156175
}
157-
return formData;
176+
return formProfileData;
158177
};
159178

160179
// ---------------- create / update data ---------------- //
@@ -177,9 +196,9 @@ function parseValue (value, type) {
177196
return value;
178197
}
179198

180-
async function handleFormSubmit (values) {
199+
async function handleFormSubmit (formKey, values) {
181200
const apiCalls = [];
182-
for (const field of formData) {
201+
for (const field of formProfileData) {
183202
const streamId = field.streamId;
184203
const eventType = field.eventType;
185204
const eventId = field.eventId;

patient.html

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,31 @@ <h2 class="card-title">Welcome</h2>
3838
</div>
3939
</div>
4040
<div class="card" id="card-form" style="visibility: hidden;">
41-
<form id="form">
42-
<h2 class="card-title">Form</h2>
43-
<div class="form-group">
44-
<label for="input-bp">
45-
Please ...
46-
</label>
47-
<div class="input-group" id="inputs-list">
48-
<!-- HERE WILL GO THE INPUTS-->
49-
</div>
50-
</div>
51-
<button type="button" id="submit-button" class="btn btn-primary mb-2">Submit</button>
52-
</form>
41+
<table width="100%"><tr>
42+
<td>
43+
<form id="form-profile">
44+
<h2 class="card-title">Profile</h2>
45+
<div class="form-group">
46+
<div class="input-group" id="inputs-profile">
47+
<!-- HERE WILL GO THE INPUTS-->
48+
</div>
49+
</div>
50+
<button type="button" id="submit-button-profile" class="btn btn-primary mb-2">Submit</button>
51+
</form>
52+
</td>
53+
<td>&nbsp &nbsp</td>
54+
<td valign="top">
55+
<form id="form-historical">
56+
<h2 class="card-title">Historical Data</h2>
57+
<div class="form-group">
58+
<div class="input-group" id="inputs-historical">
59+
<!-- HERE WILL GO THE INPUTS-->
60+
</div>
61+
</div>
62+
<button type="button" id="submit-button-historical" class="btn btn-primary mb-2">Submit</button>
63+
</form>
64+
</td>
65+
</tr></table>
5366
</div>
5467
</div>
5568
</body>

0 commit comments

Comments
 (0)