-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdr-controler.js
More file actions
230 lines (194 loc) · 8.53 KB
/
dr-controler.js
File metadata and controls
230 lines (194 loc) · 8.53 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { stateGetApp } from './common-lib.js';
import { drLib } from './dr-lib.js';
import { exportXLSFile } from './exporToXLS.js';
/**
* UI management code.
* Relies on drLib for API calls and data management
* Used to seprate the UI from the API calls
* @param {*} event
*/
window.onload = (event) => {
stateChange('loggedOut');
drLib.showLoginButton('login-button', stateChange);
};
async function stateChange(state) {
await HDSLib.initHDSModel();
if (state === 'loggedIN') {
document.getElementById('please-login').style.display = 'none';
document.getElementById('data-view').style.display = 'block';
setQuestionnaries();
} else {
document.getElementById('please-login').style.display = 'block';
document.getElementById('data-view').style.display = 'none';
}
}
const questionnaryButtons = {};
async function setQuestionnaries() {
// -- on load
const selectedQuestionnary = getQuestionnaryFromUrl();
const tbody = document.getElementById('questionnary-table').getElementsByTagName('tbody')[0];
// clear previous content
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
const appManaging = await stateGetApp('managing');
const collectors = await appManaging.getCollectors();
for (const collector of collectors) {
const row = tbody.insertRow(-1);
const cellQuestionnary = row.insertCell(-1);
cellQuestionnary.innerHTML = `<button type="button" class="btn btn-secondary mb-sm">${collector.name}</button>`;
questionnaryButtons[collector.id] = cellQuestionnary.getElementsByTagName('button')[0];
cellQuestionnary.onclick = function () {
showQuestionnary(collector.id);
};
};
if (selectedQuestionnary) {
showQuestionnary(selectedQuestionnary);
}
}
// ------- Get Dr's info -------- //
function getQuestionnaryFromUrl() {
const params = new URLSearchParams(document.location.search);
const questionaryId = params.get('collectorId');
return questionaryId
}
function highlightQuestionnaryButton(questionaryId) {
for (const [key, button] of Object.entries(questionnaryButtons)) {
const color = (questionaryId === key) ? "LightSeaGreen" : 'lightgrey';
button.style.backgroundColor = color;
}
}
async function showQuestionnary(questionaryId) {
highlightQuestionnaryButton(questionaryId);
console.log('## showQuestionnaryId', questionaryId);
if (questionaryId == null) {
document.getElementById('questionnary-view').style.display = 'none';
return;
}
const appManaging = await stateGetApp('managing');
// get questionnary (Controller)
const collector = await appManaging.getCollectorById(questionaryId);
// TODO check if the following line is necessary
await collector.init(); // load controller data only when needed
// show details
document.getElementById('request-title').innerHTML = HDSLib.l(collector.request.title);
document.getElementById('request-requester').innerHTML = collector.request.requesterName;
document.getElementById('request-description').innerHTML = HDSLib.l(collector.request.description);
document.getElementById('request-consent').innerHTML = HDSLib.l(collector.request.consent);
const permissionsStr = collector.request.permissions.map(p => `- ${p.defaultName} => ${p.level}`).join('<BR>\n');
document.getElementById('request-permissions').innerHTML = permissionsStr;
document.getElementById('request-app-id').innerHTML = collector.request.appId;
document.getElementById('request-app-url').innerHTML = collector.request.appUrl;
// forms sections
const table = document.getElementById('forms-sections');
table.innerHTML = '';
const keyTitles = { type: 'Type', name: 'Name', itemKeys: 'ItemKeys'};
const sections = collector.request.sections;
console.log('## sections', sections);
for (const [key, title] of Object.entries(keyTitles)) {
const row = table.insertRow(-1);
row.insertCell(-1).innerHTML = title;
for (const section of sections) {
let content = section[key];
if (key === 'name') {
content = HDSLib.l(content); // localizable text
}
if (key === 'itemKeys') {
content = content.map((itemKey) => {
const itemDef = HDSLib.getHDSModel().itemsDefs.forKey(itemKey);
return '- ' + itemDef.label;
}).join('\n<br>');
}
row.insertCell(-1).innerHTML = content;
}
}
console.log('## showQuestionnary status:', requestContent);
// set create sharing button
document.getElementById('button-new-sharing').onclick = async () => {
const title = document.getElementById('title-new-sharing').value.trim();
if (title.length < 5){
alert('Sharing title too short (4 char min)');
return;
}
const options = { customData: { hello: 'bob' } }; // useless for now kept as reference, usage could be to pass userId in an other system
const invite = await collector.createInvite(title, options);
const inviteSharingData = await invite.getSharingData();
console.log('## createInvite newInvite and sharing', { invite, inviteSharingData });
refreshInviteList(collector);
}
// show current pending invitations
await refreshInviteList(collector);
// show current patients
const {headers, patientsData} = await refreshPatientList(collector);
//const {headers, patientsData} = await refreshPatientList(questionaryId);
document.getElementById('button-download').onclick = async () => {
await exportXLSFile(headers, patientsData, 'patients');
}
document.getElementById('questionnary-view').style.display = 'block';
}
async function refreshInviteList(collector) {
// check inbox for new incoming accepted requests
const newCollectorInvites = await collector.checkInbox();
console.log('## refreshInviteList inbox ', newCollectorInvites);
const table = document.getElementById('invites-table');
// clear table
table.innerHTML = '';
// get all invites
const invites = await collector.getInvites(); // Todo add a "filter by" maybe only list "Pending" invites
console.log('## refreshInviteList invites ', invites);
const pendingInvites = invites.filter(i => i.status === 'pending');
pendingInvites.sort((a, b) => b.dateCreation - a.dateCreation); // sort by creation date reverse
console.log('## refreshInviteList pending ', pendingInvites);
for (const invite of pendingInvites) {
const row = table.insertRow(-1);
row.insertCell(-1).innerHTML = invite.displayName;
row.insertCell(-1).innerHTML = invite.status;
row.insertCell(-1).innerHTML = invite.dateCreation.toLocaleString();
const inviteSharingData = await invite.getSharingData();
row.insertCell(-1).innerHTML = getSharingLinkHTML(inviteSharingData);
}
}
/**
* Update the patient list
*/
async function refreshPatientList(collector) {
const { headers, patientsData } = await drLib.getPatientsData(collector);
const table = document.getElementById('patients-table');
// clear table
table.innerHTML = '';
// --- headers
const headerRow = table.insertRow(-1);
for (const [key, value] of Object.entries(headers)) {
const headerCell = document.createElement("TH");
headerCell.innerHTML = value;
headerRow.appendChild(headerCell);
}
// --- patients
for (const patient of patientsData) {
const row = table.insertRow(-1);
for (const key of Object.keys(headers)) {
let text = patient[key];
if (key === 'inviteName') { // for inviteName add a link
const page = `dr-patient-view.html?collectorId=${collector.id}&inviteKey=${patient.invite.key}`;
text = `<A HREF="${page}">${patient.inviteName}</A>`;
}
row.insertCell(-1).innerHTML = text;
}
}
// return this to be used by Excel Download
return { headers, patientsData };
}
/**
* Creates the sharing link on the page
*/
function getSharingLinkHTML(inviteSharingData) {
const currentPage = window.location.href;
const posDrHTML = currentPage.indexOf('dr.html');
const patientURL = currentPage.substring(0, posDrHTML) + 'patient.html';
const sharingLink = `${patientURL}?apiEndpoint=${inviteSharingData.apiEndpoint}&eventId=${inviteSharingData.eventId}`;
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';
let sharingLinkHTML = `<A HREF="mailto:?subject=Invitation&body=${encodeURIComponent(sharingMailBody)}">Send by email</A>`;
// add copy link
sharingLinkHTML += ` - <A HREF="#" onclick="navigator.clipboard.writeText('${sharingLink}'); alert('Copied the sharing link to clipboard')">Copy link to clipboard</A>`;
return sharingLinkHTML;
}