Skip to content

Commit de2c239

Browse files
committed
Using questionnary to get form for patient
1 parent 536df55 commit de2c239

5 files changed

Lines changed: 48 additions & 34 deletions

File tree

common-data-defs.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,19 @@ questionnaires = {
151151
level: 'read',
152152
name: perm.name,
153153
})),
154-
patientBaseStreams
154+
patientBaseStreams,
155+
forms: {
156+
profile: {
157+
type: 'permanent',
158+
name: 'Profile',
159+
content: formProfileContent
160+
},
161+
historical: {
162+
type: 'recurring',
163+
name: 'Historical',
164+
content: formHistoricalContent
165+
}
166+
}
155167
}
156168
}
157169

dr-controler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function setPatientList() {
4444
}
4545

4646
// --- patients
47-
const patients = await drLib.getPatientsList(100);
47+
const patients = await drLib.getPatientsList('demo-dr-forms-questionary-x', 100);
4848
for (const patient of Object.values(patients)) {
4949
const row = table.insertRow(-1);
5050
const cellStatus = row.insertCell(-1);

dr-lib.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
5151

5252
const patients = {};
5353

54-
async function getPatientsList (limit = 100) {
54+
async function getPatientsList (questionnaryId, limit = 100) {
5555
const res = await drConnection.api([{ method: 'events.get', params: { types: ['credentials/pryv-api-endpoint'], limit } }]);
5656
const patientApiEndpointEvents = res[0].events;
5757

@@ -78,7 +78,7 @@ async function getPatientsList (limit = 100) {
7878

7979
// -- get the patients
8080
for (const patientEvent of Object.values(uniques)) {
81-
const patient = await getPatientDetails(patientEvent);
81+
const patient = await getPatientDetails(questionnaryId, patientEvent);
8282
if (patient) {
8383
patients[patient.apiEndpoint] = patient;
8484
console.log('## Patient details', patient);
@@ -92,7 +92,7 @@ async function getPatientsList (limit = 100) {
9292
/**
9393
* get patients details
9494
*/
95-
async function getPatientDetails (patientEvent) {
95+
async function getPatientDetails (questionnaryId, patientEvent) {
9696
if (patientEvent.type !== 'credentials/pryv-api-endpoint') return null;
9797
const patient = {
9898
status: 'active',
@@ -123,7 +123,7 @@ async function getPatientDetails (patientEvent) {
123123
}
124124
}
125125

126-
// -- mark as revoked
126+
// -- marked revoked
127127
if (patientEvent.streamIds.includes('patients-revoked')) {
128128
patient.status = 'revoked';
129129
const usernameFromApiEndpoint = patientEvent.content.split('/')[3];
@@ -133,6 +133,7 @@ async function getPatientDetails (patientEvent) {
133133

134134

135135
// -- get data
136+
136137
const profileEvents = await patientConnection.api([{ method: 'events.get', params: { limit: 100 } }]);
137138
for (const profileEvent of profileEvents[0].events) {
138139
const field = dataFieldFromEvent(profileEvent);

patient-lib.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,35 @@ async function connect (apiEndpoint, questionaryId) {
1616
// ---------------- form content ---------------- //
1717

1818

19-
// local copy of formProfileContent + actual values
20-
let formProfileData = null;
21-
async function getFormContent (formKey) {
22-
if (formKey === 'profile') {
23-
return getFormProfileContent();
19+
20+
async function getFormContent (questionaryId, formKey) {
21+
const form = dataDefs.questionnaires[questionaryId].forms[formKey];
22+
console.log('## getFormContent', form);
23+
if (form.type === 'permanent') {
24+
return getFormPermanentContent(form);
2425
}
25-
if (formKey === 'historical') {
26-
return getFormHistoricalContent();
26+
if (formKey === 'recurring') {
27+
return getFormRecurringContent(form);
2728
}
2829
return [];
2930
}
3031

31-
async function getFormHistoricalContent () {
32-
console.log('## getFormHistoricalContent');
33-
const formHistoricalData = structuredClone(dataDefs.formHistoricalContent);
34-
formHistoricalData.forEach(field => {
32+
async function getFormRecurringContent (form) {
33+
const formReccuringData = structuredClone(form.content);
34+
formReccuringData.forEach(field => {
3535
field.id = 'field-historical-' + field.dataFieldKey;
3636
});
37-
return formHistoricalData;
37+
return formReccuringData;
3838
}
3939

40-
41-
async function getFormProfileContent () {
42-
if (formProfileData) { return formProfileData; }
43-
formProfileData = structuredClone(dataDefs.formProfileContent);
40+
// local copy of formProfileContent + actual values
41+
let formPermanentData = null;
42+
async function getFormPermanentContent (form) {
43+
if (formPermanentData) { return formPermanentData; }
44+
formPermanentData = structuredClone(form.content);
4445

4546
// get the values from the API
46-
const apiCalls = formProfileData.map(field => ({
47+
const apiCalls = formPermanentData.map(field => ({
4748
method: 'events.get',
4849
params: {
4950
streams: [field.streamId],
@@ -55,7 +56,7 @@ async function getFormProfileContent () {
5556
const res = await connection.api(apiCalls);
5657
for (let i = 0; i < res.length; i++) {
5758
const e = res[i];
58-
const field = formProfileData[i];
59+
const field = formPermanentData[i];
5960
field.id = 'field-profile-' + field.dataFieldKey;
6061
console.log('## getFormContent ' + i, e);
6162
if (e.events && e.events.length > 0) {
@@ -75,7 +76,7 @@ async function getFormProfileContent () {
7576
field.eventId = event.id; // will allow t track if the event is to be updated
7677
}
7778
}
78-
return formProfileData;
79+
return formPermanentData;
7980
};
8081

8182
// ---------------- create / update data ---------------- //
@@ -104,9 +105,9 @@ function parseValue (value, type) {
104105
return value;
105106
}
106107

107-
async function handleFormSubmit (formKey, values) {
108+
async function handleFormSubmit (questionaryId, formKey, values) {
108109
const apiCalls = [];
109-
for (const field of formProfileData) {
110+
for (const field of formPermanentData) {
110111
const streamId = field.streamId;
111112
const eventType = field.eventType;
112113
const eventId = field.eventId;

patient-profile-controler.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ window.onload = async (event) => {
99
const { patientApiEndpoint, questionaryId } = getPatientApiEndpointAndFormId();
1010
console.log('## patientApiEndpoint:', patientApiEndpoint);
1111
await connect(patientApiEndpoint, questionaryId);
12-
updateFormContent('profile');
12+
updateFormContent(questionaryId, 'profile');
1313
document.getElementById('submit-button-profile').addEventListener("click", function () {
14-
submitForm('profile');
14+
submitForm(questionaryId, 'profile');
1515
});
1616
}
1717

@@ -38,8 +38,8 @@ function getPatientApiEndpointAndFormId() {
3838
/**
3939
* Take the from content from the definition and actual values and create the HTML
4040
*/
41-
async function updateFormContent(formKey) {
42-
const formData = await patientLib.getFormContent(formKey);
41+
async function updateFormContent(questionaryId, formKey) {
42+
const formData = await patientLib.getFormContent(questionaryId, formKey);
4343
console.log('Form content:', formData);
4444

4545
document.getElementById('inputs-' + formKey).innerHTML = ''; // Clear previous content
@@ -73,9 +73,9 @@ async function updateFormContent(formKey) {
7373
/**
7474
* Submit the form and send the data to the API
7575
*/
76-
async function submitForm(formKey) {
76+
async function submitForm(questionaryId, formKey) {
7777
const values = {};
78-
const formData = await patientLib.getFormContent(formKey);
78+
const formData = await patientLib.getFormContent(questionaryId, formKey);
7979
for (let i = 0; i < formData.length; i++) {
8080
const field = formData[i];
8181
const fieldId = field.id;
@@ -87,6 +87,6 @@ async function submitForm(formKey) {
8787
values[field.id] = formField.value.trim();
8888
}
8989
}
90-
await patientLib.handleFormSubmit(formKey, values);
90+
await patientLib.handleFormSubmit(questionaryId, formKey, values);
9191
alert('Form submitted successfully');
9292
};

0 commit comments

Comments
 (0)