Skip to content

Commit 47c0587

Browse files
committed
Introducing multiple questionnary for dr
1 parent d7aa199 commit 47c0587

3 files changed

Lines changed: 84 additions & 38 deletions

File tree

dr-controler.js

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,61 @@ async function stateChange(state) {
1818
if (state === 'loggedIN') {
1919
document.getElementById('please-login').style.visibility = 'hidden';
2020
document.getElementById('data-view').style.visibility = 'visible';
21-
setSharingLink();
22-
const {headers, patientsData} = await setPatientList(questionaryId);
23-
document.getElementById('button-download').onclick = () => {
24-
exportCSVFile(headers, patientsData, 'patients');
25-
}
21+
setQuestionnaries();
2622

2723
} else {
2824
document.getElementById('please-login').style.visibility = 'visible';
2925
document.getElementById('data-view').style.visibility = 'hidden';
3026
}
3127
}
3228

29+
async function setQuestionnaries() {
30+
const select = document.getElementById("select-questionnary");
31+
const optionNull = document.createElement("option");
32+
optionNull.text = '---';
33+
optionNull.value = '';
34+
select.add(optionNull);
35+
36+
const quests = await drLib.getQuestionnaires();
37+
for (const [key, value] of Object.entries(quests)) {
38+
const option = document.createElement("option");
39+
option.text = value.title;
40+
option.value = key;
41+
select.add(option);
42+
}
43+
44+
// --
45+
select.onchange = function () {
46+
if (select.value === '') {
47+
showQuestionnary(null);
48+
return;
49+
}
50+
showQuestionnary(select.value);
51+
}
52+
}
53+
54+
async function showQuestionnary(questionaryId) {
55+
if (questionaryId == null) {
56+
document.getElementById('questionnary-view').style.visibility = 'hidden';
57+
return;
58+
}
59+
60+
setSharingLink(questionaryId);
61+
const {headers, patientsData} = await setPatientList(questionaryId);
62+
document.getElementById('button-download').onclick = () => {
63+
exportCSVFile(headers, patientsData, 'patients');
64+
}
65+
document.getElementById('questionnary-view').style.visibility = 'visible';
66+
}
67+
3368
const rowItems = ['name', 'surname', 'nationality'];
3469
/**
3570
* Update the patient list
3671
*/
3772
async function setPatientList(questionaryId) {
3873
const table = document.getElementById('patients-table');
74+
// clear table
75+
table.innerHTML = '';
3976
const fields = drLib.getFields(questionaryId);
4077
const headers = {
4178
status: 'Status',
@@ -84,12 +121,12 @@ async function setPatientList(questionaryId) {
84121
/**
85122
* Creates the sharing link on the page
86123
*/
87-
async function setSharingLink() {
124+
async function setSharingLink(questionaryId) {
88125
const currentPage = window.location.href;
89126
const posDrHTML = currentPage.indexOf('dr.html');
90127
const patientURL = currentPage.substring(0, posDrHTML) + 'patient.html';
91128

92-
const formApiEndpoint = await drLib.getSharingToken();
129+
const formApiEndpoint = await drLib.getSharingToken(questionaryId);
93130
const sharingLink = patientURL + '?formApiEndpoint=' + formApiEndpoint;
94131
const sharingMailBody = 'Hello,\n\nI am sending you a link to fill out a form.\nPlease click on the link below to access the form: \n\n' + sharingLink + '\n\nBest regards,\nYour Doctor';
95132
const sharingLinkHTML = `<A HREF="mailto:?subject=Invitation&body=${encodeURI(sharingMailBody)}">Send by email</A> - ${sharingLink}`;

dr-lib.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const drLib = {
66
showLoginButton,
77
getSharingToken,
88
getPatientsList,
9-
getFields
9+
getFields,
10+
getQuestionnaires
1011
}
1112

1213
function showLoginButton (loginSpanId, stateChangeCallBack) {
@@ -49,6 +50,11 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
4950
}
5051
}
5152

53+
// -------- Questionnaties -----
54+
async function getQuestionnaires() {
55+
return dataDefs.questionnaires;
56+
}
57+
5258
// -------- Fetch patient list --------
5359

5460
const patients = {};
@@ -207,7 +213,6 @@ function dataFieldFromEvent (formProfile, event) {
207213
event: event
208214
};
209215
if (dataField.type === 'date') {
210-
// convert the date to a Date object
211216
const date = new Date(event.content);
212217
if (!isNaN(date)) {
213218
field.value = date.toISOString().split('T')[0]; // format YYYY-MM-DD
@@ -234,14 +239,23 @@ async function initDrAccount (connection) {
234239
* Initialize or get the sharing token for patients
235240
* @returns
236241
*/
237-
async function getSharingToken () {
242+
async function getSharingToken (questionaryId) {
238243
const accessesCheckRes = await drConnection.api([{ method: 'accesses.get', params: {}}]);
239244
const sharedAccess = accessesCheckRes[0].accesses.find(access => access.name === 'demo-dr-form-shared');
240245
if (sharedAccess) {
241246
console.log('## Dr account already has a shared access');
242247
return sharedAccess.apiEndpoint;
243248
}
244-
const accessRes = await drConnection.api([{
249+
250+
251+
const resultStreamAndAccess = await drConnection.api([{
252+
method: 'streams.create', // make sure streamId with questionnaryId exists
253+
params: {
254+
id: 'demo-dr-forms-questionary-x',
255+
name: 'Questionnary x',
256+
parentId: 'demo-dr-forms'
257+
}
258+
}, { // create access
245259
method: 'accesses.create',
246260
params: {
247261
name: 'demo-dr-form-shared',
@@ -251,7 +265,7 @@ async function getSharingToken () {
251265
level: 'create-only'
252266
},
253267
{
254-
streamId: 'demo-dr-forms-questionary-x',
268+
streamId: questionaryId,
255269
level: 'read'
256270
},
257271
{ // for "publicly shared access" always forbid the selfRevoke feature
@@ -260,13 +274,13 @@ async function getSharingToken () {
260274
}],
261275
clientData: {
262276
'demo-dr-form': {
263-
questionaryId: 'demo-dr-forms-questionary-x'
277+
questionaryId
264278
}
265279
}
266280
}
267281
}]);
268-
console.log('## Dr account shared access created', accessRes);
269-
return accessRes[0].access.apiEndpoint;
282+
console.log('## Dr account shared access created', resultStreamAndAccess);
283+
return resultStreamAndAccess[1].access.apiEndpoint;
270284
}
271285

272286

@@ -317,13 +331,6 @@ async function initStreams () {
317331
id: 'demo-dr-forms',
318332
name: 'Demo Dr Forms'
319333
}
320-
},
321-
{
322-
method: 'streams.create',
323-
params: {
324-
id: 'demo-dr-forms-questionary-x',
325-
name: 'Questionnary x'
326-
}
327334
}
328335
];
329336
const result = await drConnection.api(apiCalls);

dr.html

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,24 @@ <h2 class="card-title">Welcome</h2>
3636
</div>
3737
</div>
3838
<div class="card" id="data-view" style="visibility: hidden;">
39-
<div class="card-login">
40-
<h2 class="card-title">Sharing link</h2>
41-
<span id="sharing-link">
42-
43-
</div>
44-
<div class="card-login">
45-
<h2 class="card-title">Patients</h2>
46-
<button id="button-download" class="btn btn-secondary">Download</button>
47-
<table id='patients-table' class="table">
48-
<thead>
49-
<!-- will be filled by setPatientList-->
50-
</thead>
51-
<tbody>
52-
<!-- will be filled by setPatientList-->
53-
</tbody>
54-
</table>
39+
Select a questionnary: <select id="select-questionnary" class="form-control"></select>
40+
<div id="questionnary-view">
41+
<div class="card">
42+
<h2 class="card-title">Sharing link</h2>
43+
<span id="sharing-link">
44+
</div>
45+
<div class="card">
46+
<h2 class="card-title">Patients</h2>
47+
<button id="button-download" class="btn btn-secondary">Download</button>
48+
<table id='patients-table' class="table">
49+
<thead>
50+
<!-- will be filled by setPatientList-->
51+
</thead>
52+
<tbody>
53+
<!-- will be filled by setPatientList-->
54+
</tbody>
55+
</table>
56+
</div>
5557
</div>
5658
</div>
5759
</div>

0 commit comments

Comments
 (0)