-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatient-home-controler.js
More file actions
181 lines (159 loc) · 6.41 KB
/
patient-home-controler.js
File metadata and controls
181 lines (159 loc) · 6.41 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
import { patientLib } from './patient-lib.js';
import { patientHomeLib } from './patient-home-lib.js';
import { stateGetApp, stateSetData } from './common-lib.js';
/**
* UI management code.
* Relies on patientLib for API calls and data management
*
* @param {*} event
*/
window.onload = (event) => {
stateChange('loggedOut');
patientHomeLib.showLoginButton('login-button', stateChange);
};
function stateChange(state) {
if (state === 'loggedIN') {
document.getElementById('please-login').style.display = 'none';
document.getElementById('card-content').style.display = 'block';
refresh();
} else {
document.getElementById('please-login').style.display = 'block';
document.getElementById('card-content').style.display = 'none';
}
}
async function refresh() {
await HDSLib.initHDSModel();
const inviteParams = getInviteParamsFromURL();
const appClient = await stateGetApp('client');
if (inviteParams) {
const collectorClient = await appClient.handleIncomingRequest(inviteParams.apiEndpoint, inviteParams.eventId);
console.log('>>## refresh: new incoming request', collectorClient, collectorClient.key);
}
const collectorClients = await appClient.getCollectorClients();
console.log('>>## refresh: collectorClients', collectorClients);
showFormList(collectorClients);
// showFormDetails(null);
}
// ------- Get Dr's info -------- //
function getInviteParamsFromURL() {
const params = new URLSearchParams(document.location.search);
const apiEndpoint = params.get('apiEndpoint');
const eventId = params.get('eventId');
console.log('>>## getInviteParamsFromURL', { apiEndpoint, eventId });
if (apiEndpoint == null || eventId == null) return null;
return { apiEndpoint, eventId };
}
// --------- Update form list --------- //
const questionnaryRows = {};
async function showFormList(collectorClients) {
console.log('## showFormList', collectorClients);
// -- table
const tbody = document.getElementById('questionnary-table').getElementsByTagName('tbody')[0];
// clear previous content
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
for (const collectorClient of collectorClients) {
const requestData = collectorClient.requestData;
// fill the table row
const row = tbody.insertRow(-1);
const cellQuestionnary = row.insertCell(-1);
const formTitle = HDSLib.l(requestData.title);
cellQuestionnary.innerHTML = `<button type="button" class="btn btn-secondary mb-sm">${formTitle}</button>`;
questionnaryRows[collectorClient.key] = {
row, button: cellQuestionnary.getElementsByTagName('button')[0]
}
cellQuestionnary.onclick = function () {
showFormDetails(collectorClient);
};
const cellDr = row.insertCell(-1);
cellDr.innerHTML = requestData.requester.name;
const cellStatus = row.insertCell(-1);
cellStatus.innerHTML = collectorClient.status;
}
}
function highlightQuestionnaryButton(buttonKey) {
for (const [key, entry] of Object.entries(questionnaryRows)) {
const colorButton = (buttonKey === key) ? "LightSeaGreen" : 'lightgrey';
const colorRow = (buttonKey === key) ? 'lightgrey' : '';
entry.button.style.backgroundColor = colorButton;
entry.row.style.backgroundColor = colorRow;
}
}
// ----------- Show form details ----------- //
async function showFormDetails(collectorClient) {
highlightQuestionnaryButton(collectorClient.key);
const show = !!collectorClient;
document.getElementById('card-questionnary-details-nothing').style.display = show ? 'none' : 'block';
document.getElementById('card-questionnary-details-something').style.display= show ? 'block' : 'none';
// clear navData
stateSetData(null, 'collector');
if (!show) return;
const requestData = collectorClient.requestData;
console.log('## showFormDetails', requestData);
// - form title
const formTitle = document.getElementById('card-questionnary-details-title');
formTitle.innerHTML = HDSLib.l(requestData.title);
// - description
const formDesc = document.getElementById('card-questionnary-details-description')
formDesc.innerHTML = HDSLib.l(requestData.description);
// - consent
const formConsent = document.getElementById('questionnary-details-consent')
formConsent.innerHTML = HDSLib.l(requestData.consent);
// - permissions
const tbody = document.getElementById('access-request').getElementsByTagName('tbody')[0];;
// clear previous content
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
for (const permission of requestData.permissions) {
const row = tbody.insertRow(-1);
const cellStream = row.insertCell(-1);
cellStream.innerHTML = permission.defaultName;
const cellLevel = row.insertCell(-1);
cellLevel.innerHTML = permission.level;
}
// - grant access / open
const buttonOpen = document.getElementById('grant-access-button');
const buttonRevoke = document.getElementById('revoke-access-button');
// -- pass the current collector to the next page
stateSetData({ collectorClientKey: collectorClient.key }, 'collector');
const nextPage = (await patientLib.navGetPages(collectorClient))[1].url;
console.log('## detail ', collectorClient, collectorClient.status);
if (collectorClient.status === 'Active') {
buttonOpen.innerHTML = 'Open';
buttonOpen.onclick = async function () {
document.location.href = nextPage;
};
buttonRevoke.innerHTML = 'Revoke';
buttonRevoke.onclick = async function () {
const doRevoke = confirm('Revoke ?');
if (doRevoke) await collectorClient.revoke();;
refresh();
};
}
else {
buttonOpen.innerHTML = 'Grant access and Open';
buttonOpen.onclick = async function () {
try {
const acceptResult = await collectorClient.accept();
if (acceptResult == null) throw new Error('Accepted result is empty');
console.log('## AcceptResult', acceptResult);
alert('Access successfully granted');
document.location.href = nextPage;
} catch (e) {
console.log(e);
alert('Error accepting: ' + e.message);
}
};
buttonRevoke.innerHTML = 'Refuse';
buttonRevoke.onclick = async function () {
const deRefuse = confirm('Refuse ?');
if (deRefuse) await collectorClient.refuse();
refresh();
}
}
// - json
// const jsonContent = document.getElementById('card-questionnary-details-content');
// jsonContent.innerHTML = '<pre>' + JSON.stringify(formInfo.formEvent, null, 2) + '<pre>';
}