-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatient-profile-controler.js
More file actions
101 lines (88 loc) · 3.51 KB
/
patient-profile-controler.js
File metadata and controls
101 lines (88 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { patientLib } from './patient-lib.js';
import { navControler } from './patient-nav-controler.js';
import { stateGetApp, stateGetData } from './common-lib.js';
/**
* UI management code.
* Relies on patientLib for API calls and data management
*
* @param {*} event
*/
let navData;
window.onload = async (event) => {
await HDSLib.initHDSModel();
// const get formKey
const formKey = (new URLSearchParams(window.location.search)).get('formKey');
const appClient = await stateGetApp('client');
const { collectorClientKey } = stateGetData('collector');
const collectorClient = await appClient.getCollectorClientByKey(collectorClientKey);
await navControler.setNavComponents(collectorClient, formKey);
// form title
const formTitle = document.getElementById('card-questionnary-details-title');
const section = collectorClient.request.getSectionByKey(formKey);
const title = HDSLib.l(section.title);
formTitle.innerHTML = title;
// set navData
navData = { appClient, collectorClient, formKey };
refreshForm();
}
async function refreshForm () {
const { appClient, collectorClient, formKey } = navData;
// -- content
; console.log()
const formData = await patientLib.getFormPermanentContent(collectorClient, formKey)
updateFormContent(formData);
document.getElementById('submit-button-list').onclick = function () {
submitForm(collectorClient, formData, formKey);
};
}
// ------- Form -------- //
/**
* Take the from content from the definition and actual values and create the HTML
*/
async function updateFormContent(formData) {
console.log('### Form content:', formData);
// Append the HTML to the form
document.getElementById('inputs-list').innerHTML = ''; // Clear previous content
for (let i = 0; i < formData.length; i++) {
const formField = formData[i];
const fieldId = formField.id;
const fieldValue = (formField.value != null) ? formField.value : '';
const fieldType = formField.type;
const fieldLabel = formField.label;
// Create the HTML for the form field
let fieldHTML = `\n<BR><label for="${fieldId}">${fieldLabel}</label>`;
if (fieldType === 'text' || fieldType === 'number') {
fieldHTML += `<input type="${fieldType}" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
} else if (fieldType === 'select') {
fieldHTML += `<select id="${fieldId}" class="form-control">`;
fieldHTML += `<option value="">--</option>`;
for (const option of formField.options) {
const selected = (option.value === fieldValue) ? 'selected' : '';
fieldHTML += `<option value="${option.value}" ${selected}>${HDSLib.l(option.label)}</option>`;
}
fieldHTML += `</select>`;
} else if (fieldType === 'date') {
fieldHTML += `<input type="date" id="${fieldId}" value="${fieldValue}" class="form-control"/>`;
}
document.getElementById('inputs-list').innerHTML += fieldHTML;
}
}
/**
* Submit the form and send the data to the API
*/
async function submitForm(collectorClient, formData, formKey) {
const values = {};
for (let i = 0; i < formData.length; i++) {
const field = formData[i];
const fieldId = field.id;
const formField = document.getElementById(fieldId);
// Store the value in the values object
if (field.type === 'date') {
values[field.id] = formField.valueAsDate;
} else {
values[field.id] = formField.value.trim();
}
}
await patientLib.handleFormSubmit(collectorClient, formData, formKey, values);
alert('Form submitted successfully');
};