Skip to content

Commit 7c8fa3e

Browse files
committed
Preparing for historical data
1 parent 3c01d3c commit 7c8fa3e

5 files changed

Lines changed: 111 additions & 49 deletions

File tree

patient-history-controler.js

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@ import { navControler } from './patient-nav-controler.js'
66
*
77
* @param {*} event
88
*/
9-
9+
let navData;
1010
window.onload = async (event) => {
11-
const navData = await navControler.setNavComponents();
11+
navData = await navControler.setNavComponents();
1212
console.log('## navData', navData);
13-
const { patientApiEndpoint, questionaryId } = navData;
13+
const { patientApiEndpoint, questionaryId, formKey } = navData;
1414
console.log('## patientApiEndpoint:', patientApiEndpoint);
1515
await patientLib.connect(patientApiEndpoint, questionaryId);
16-
// - form title
17-
const formTitle = document.getElementById('card-questionnary-details-title');
18-
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
19-
20-
// -- connection
21-
updateFormContent(questionaryId, 'history');
22-
document.getElementById('submit-button-history').addEventListener("click", function () {
23-
submitForm(questionaryId, 'history');
24-
});
16+
// - form title
17+
const formTitle = document.getElementById('card-questionnary-details-title');
18+
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
19+
refreshForm();
20+
}
21+
22+
23+
async function refreshForm () {
24+
const { questionaryId, formKey } = navData;
25+
// -- content
26+
; console.log()
27+
const formData = await patientLib.getFormContent(questionaryId, formKey)
28+
updateFormContent(formData);
29+
document.getElementById('submit-button-list').onclick = function () {
30+
submitForm(formData);
31+
};
2532
}
2633

2734

@@ -30,15 +37,54 @@ window.onload = async (event) => {
3037
/**
3138
* Take the from content from the definition and actual values and create the HTML
3239
*/
33-
async function updateFormContent(questionaryId, formKey) {
34-
const formData = await patientLib.getFormContent(questionaryId, formKey);
40+
async function updateFormContent(formData) {
3541
console.log('Form content:', formData);
3642

43+
// Append the HTML to the form
44+
document.getElementById('inputs-list').innerHTML = ''; // Clear previous content
45+
for (let i = 0; i < formData.length; i++) {
46+
const formField = formData[i];
47+
const fieldId = formField.id;
48+
const fieldValue = (formField.value != null) ? formField.value : '';
49+
const fieldType = formField.type;
50+
const fieldLabel = formField.label;
51+
52+
// Create the HTML for the form field
53+
let fieldHTML = `\n<BR><label for="${fieldId}">${fieldLabel}</label>`;
54+
if (fieldType === 'text' || fieldType === 'number') {
55+
fieldHTML += `<input type="${fieldType}" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
56+
} else if (fieldType === 'select') {
57+
fieldHTML += `<select id="${fieldId}" class="form-control">`;
58+
fieldHTML += `<option value="">--</option>`;
59+
for (const option of formField.options) {
60+
const selected = (option.value === fieldValue) ? 'selected' : '';
61+
fieldHTML += `<option value="${option.value}" ${selected}>${option.label}</option>`;
62+
}
63+
fieldHTML += `</select>`;
64+
} else if (fieldType === 'date') {
65+
fieldHTML += `<input type="date" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
66+
}
67+
68+
document.getElementById('inputs-list').innerHTML += fieldHTML;
69+
}
3770
}
3871

3972
/**
4073
* Submit the form and send the data to the API
4174
*/
42-
async function submitForm(questionaryId, formKey) {
43-
75+
async function submitForm(formData) {
76+
const values = {};
77+
for (let i = 0; i < formData.length; i++) {
78+
const field = formData[i];
79+
const fieldId = field.id;
80+
const formField = document.getElementById(fieldId);
81+
// Store the value in the values object
82+
if (field.type === 'date') {
83+
values[field.id] = formField.valueAsDate;
84+
} else {
85+
values[field.id] = formField.value.trim();
86+
}
87+
}
88+
await patientLib.handleFormSubmit(formData, values);
89+
alert('Form submitted successfully');
4490
};

patient-history.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ <h1>Demo Form - Patient's page</h1>
2828
<h2 class="card-title"> Questionnary: <span id="card-questionnary-details-title"></span></h2>
2929
<h3 class="card-title"> Set: <span id="card-questionnary-details-question-set">Historical Data</span></h3>
3030
<div class="form-group">
31-
<div class="input-group" id="inputs-history">
31+
<div class="input-group" id="inputs-list">
3232
<!-- HERE WILL GO THE INPUTS-->
3333
</div>
3434
</div>
35-
<button type="button" id="submit-button-history" class="btn btn-primary mb-2">Submit</button>
35+
<button type="button" id="submit-button-list" class="btn btn-primary mb-2">Submit</button>
3636
</form>
3737
</div>
3838
</div>

patient-lib.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,47 @@ async function getForms (questionaryId) {
7575
return dataDefs.questionnaires[questionaryId].forms;
7676
}
7777

78-
79-
async function getFormContent (questionaryId, formKey) {
78+
/**
79+
* Get Form content
80+
* @param {string} questionaryId
81+
* @param {string} formKey
82+
* @param {Date} [date] - If editing content from existing date
83+
* @returns
84+
*/
85+
async function getFormContent (questionaryId, formKey, date) {
8086
const form = dataDefs.questionnaires[questionaryId].forms[formKey];
8187
console.log('## getFormContent', form, questionaryId, formKey);
8288
if (form.type === 'permanent') {
83-
return getFormPermanentContent(form);
89+
return getFormExistingContent(form);
8490
}
8591
if (form.type === 'recurring') {
86-
return getFormRecurringContent(form);
92+
return getFormRecurringContent(form, date);
8793
}
8894
return [];
8995
}
9096

91-
async function getFormRecurringContent (form) {
97+
async function getFormRecurringContent (form, date) {
9298
const formReccuringData = structuredClone(form.content);
9399

94100
for (let i = 0; i < formReccuringData.length; i++) {
95101
const field = formReccuringData[i];
96102
field.id = form.key + '-' + i;
97103
}
98104
return formReccuringData;
105+
99106
}
100107

101108
// local copy of formProfileContent + actual values
102-
let formPermanentData = null;
103-
async function getFormPermanentContent (form) {
104-
if (formPermanentData) { return formPermanentData; }
105-
formPermanentData = structuredClone(form.content);
109+
/**
110+
* @param {*} form
111+
* @param {*} date
112+
* @returns
113+
*/
114+
async function getFormExistingContent (form, date) {
115+
const formData = structuredClone(form.content);
106116

107117
// get the values from the API
108-
const apiCalls = formPermanentData.map(field => ({
118+
const apiCalls = formData.map(field => ({
109119
method: 'events.get',
110120
params: {
111121
streams: [field.streamId],
@@ -117,7 +127,7 @@ async function getFormPermanentContent (form) {
117127
const res = await connection.api(apiCalls);
118128
for (let i = 0; i < res.length; i++) {
119129
const e = res[i];
120-
const field = formPermanentData[i];
130+
const field = formData[i];
121131
field.id = form.key + '-' + i;
122132
console.log('## getFormContent ' + i, e);
123133
if (e.events && e.events.length > 0) {
@@ -137,7 +147,7 @@ async function getFormPermanentContent (form) {
137147
field.eventId = event.id; // will allow t track if the event is to be updated
138148
}
139149
}
140-
return formPermanentData;
150+
return formData;
141151
};
142152

143153
// ---------------- create / update data ---------------- //
@@ -166,9 +176,9 @@ function parseValue (value, type) {
166176
return value;
167177
}
168178

169-
async function handleFormSubmit (questionaryId, formKey, values) {
179+
async function handleFormSubmit (formData, values, date) {
170180
const apiCalls = [];
171-
for (const field of formPermanentData) {
181+
for (const field of formData) {
172182
const streamId = field.streamId;
173183
const eventType = field.eventType;
174184
const eventId = field.eventId;

patient-profile-controler.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@ import { navControler } from './patient-nav-controler.js'
77
* @param {*} event
88
*/
99

10+
let navData;
1011
window.onload = async (event) => {
11-
const navData = await navControler.setNavComponents();
12+
navData = await navControler.setNavComponents();
1213
console.log('## navData', navData);
13-
const { patientApiEndpoint, questionaryId } = navData;
14+
const { patientApiEndpoint, questionaryId, formKey } = navData;
1415
console.log('## patientApiEndpoint:', patientApiEndpoint);
1516
await patientLib.connect(patientApiEndpoint, questionaryId);
1617
// - form title
1718
const formTitle = document.getElementById('card-questionnary-details-title');
1819
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
20+
refreshForm();
21+
}
22+
23+
async function refreshForm () {
24+
const { questionaryId, formKey } = navData;
1925
// -- content
20-
updateFormContent(questionaryId, 'profile');
21-
document.getElementById('submit-button-profile').addEventListener("click", function () {
22-
submitForm(questionaryId, 'profile');
23-
});
24-
26+
; console.log()
27+
const formData = await patientLib.getFormContent(questionaryId, formKey)
28+
updateFormContent(formData);
29+
document.getElementById('submit-button-list').onclick = function () {
30+
submitForm(formData);
31+
};
2532
}
2633

2734

@@ -30,11 +37,11 @@ window.onload = async (event) => {
3037
/**
3138
* Take the from content from the definition and actual values and create the HTML
3239
*/
33-
async function updateFormContent(questionaryId, formKey) {
34-
const formData = await patientLib.getFormContent(questionaryId, formKey);
40+
async function updateFormContent(formData) {
3541
console.log('Form content:', formData);
3642

37-
document.getElementById('inputs-' + formKey).innerHTML = ''; // Clear previous content
43+
// Append the HTML to the form
44+
document.getElementById('inputs-list').innerHTML = ''; // Clear previous content
3845
for (let i = 0; i < formData.length; i++) {
3946
const formField = formData[i];
4047
const fieldId = formField.id;
@@ -57,17 +64,16 @@ async function updateFormContent(questionaryId, formKey) {
5764
} else if (fieldType === 'date') {
5865
fieldHTML += `<input type="date" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
5966
}
60-
// Append the HTML to the form
61-
document.getElementById('inputs-' + formKey).innerHTML += fieldHTML;
67+
68+
document.getElementById('inputs-list').innerHTML += fieldHTML;
6269
}
6370
}
6471

6572
/**
6673
* Submit the form and send the data to the API
6774
*/
68-
async function submitForm(questionaryId, formKey) {
75+
async function submitForm(formData) {
6976
const values = {};
70-
const formData = await patientLib.getFormContent(questionaryId, formKey);
7177
for (let i = 0; i < formData.length; i++) {
7278
const field = formData[i];
7379
const fieldId = field.id;
@@ -79,6 +85,6 @@ async function submitForm(questionaryId, formKey) {
7985
values[field.id] = formField.value.trim();
8086
}
8187
}
82-
await patientLib.handleFormSubmit(questionaryId, formKey, values);
88+
await patientLib.handleFormSubmit(formData, values);
8389
alert('Form submitted successfully');
8490
};

patient-profile.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ <h1>Demo Form - Patient's page</h1>
2828
<h2 class="card-title"> Questionnary: <span id="card-questionnary-details-title"></span></h2>
2929
<h3 class="card-title"> Set: <span id="card-questionnary-details-question-set">Profile</span></h3>
3030
<div class="form-group">
31-
<div class="input-group" id="inputs-profile">
31+
<div class="input-group" id="inputs-list">
3232
<!-- HERE WILL GO THE INPUTS-->
3333
</div>
3434
</div>
35-
<button type="button" id="submit-button-profile" class="btn btn-primary mb-2">Submit</button>
35+
<button type="button" id="submit-button-list" class="btn btn-primary mb-2">Submit</button>
3636
</form>
3737
</div>
3838
</div>

0 commit comments

Comments
 (0)