Skip to content

Commit 21e8526

Browse files
committed
moving state management to common for usage by dr app
1 parent 5e20751 commit 21e8526

10 files changed

Lines changed: 124 additions & 453 deletions

common-lib.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CookieUtils } from './CookieUtils.js';
12
let model;
23

34
// following the APP GUIDELINES: https://api.pryv.com/guides/app-guidelines/
@@ -30,3 +31,57 @@ export async function connectAPIEndpoint (apiEndpoint) {
3031
connection.hdsModel = await initHDSModel();
3132
return connection;
3233
}
34+
35+
// ---- state management to be replaced by framework logic
36+
37+
const apps = {
38+
client: null,
39+
managing: null
40+
};
41+
42+
/**
43+
*
44+
* @param {*} app - AppClientAccount or AppManagingAccount (null to delete)
45+
* @param {string} type 'client' or 'managing'
46+
*/
47+
export function stateSaveApp (type, app) {
48+
console.log('## stateSaveApp ', app);
49+
const navData = app ? {
50+
apiEndpoint: app.connection.apiEndpoint,
51+
streamId: app.baseStreamId,
52+
name: app.appName
53+
} : null;
54+
stateSetData(navData, 'hds-app-' + type + '-app');
55+
apps[type] = app;
56+
}
57+
58+
export async function stateGetApp (type) {
59+
if (apps[type]) return apps[type];
60+
const navData = stateGetData('hds-app-' + type + '-app');
61+
if (navData !== null) {
62+
if (type === 'client') {
63+
apps.client = await HDSLib.appTemplates.AppClientAccount.newFromApiEndpoint(navData.streamId, navData.apiEndpoint, navData.name);
64+
} else {
65+
apps.managing = await HDSLib.appTemplates.AppManagingAccount.newFromApiEndpoint(navData.streamId, navData.apiEndpoint, navData.name);
66+
}
67+
}
68+
console.log('## stateGetApp ', type, apps[type]);
69+
return apps[type];
70+
}
71+
72+
73+
const COOKIE_KEY = 'hds-app-client-de';
74+
export function stateSetData(data, cookieKey = COOKIE_KEY) {
75+
if (data == null) return CookieUtils.del(cookieKey, '/');
76+
CookieUtils.set(cookieKey, data, 365, '/');
77+
// debug
78+
const debugData = stateGetData(cookieKey);
79+
console.log('## stateSetData ', {cookieKey, data, debugData});
80+
}
81+
82+
export function stateGetData(cookieKey = COOKIE_KEY) {
83+
const cookieContent = CookieUtils.get(cookieKey);
84+
console.log('## stateGetData ', { cookieKey, cookieContent });
85+
const formKey = (new URLSearchParams(window.location.search)).get('formKey');
86+
return Object.assign({ formKey }, cookieContent);
87+
}

dr-controler.js

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ async function showQuestionnary(questionaryId) {
103103
await refreshInviteList(collector);
104104

105105
// show current patients
106-
await setPatientList(collector);
106+
await refreshPatientList(collector);
107107

108-
//const {headers, patientsData} = await setPatientList(questionaryId);
108+
//const {headers, patientsData} = await refreshPatientList(questionaryId);
109109
document.getElementById('button-download').onclick = async () => {
110110
await exportXLSFile(headers, patientsData, 'patients');
111111
}
@@ -144,73 +144,38 @@ async function refreshInviteList(collector) {
144144
/**
145145
* Update the patient list
146146
*/
147-
async function setPatientList(collector) {
147+
async function refreshPatientList(collector) {
148+
const { headers, patientsData } = await drLib.getPatientsData(collector);
149+
148150
const table = document.getElementById('patients-table');
149151

150152
const requestContent = collector.statusData.requestContent;
151153
console.log('## collector requestContent', requestContent);
152154

153155
// clear table
154156
table.innerHTML = '';
155-
const itemDefs = drLib.getFirstFormFields(requestContent.app.data.forms);
156-
const staticHeaders = {
157-
status: 'Status',
158-
inviteName: 'Invite',
159-
username: 'Username',
160-
createdAt: 'Date'
161-
}
162-
const headers = structuredClone(staticHeaders);
163-
164157
// --- headers
165158
const headerRow = table.insertRow(-1);
166159
for (const [key, value] of Object.entries(headers)) {
167-
const headerStatusCell = document.createElement("TH");
168-
headerStatusCell.innerHTML = value;
169-
headerRow.appendChild(headerStatusCell);
170-
}
171-
172-
for (const itemDef of itemDefs) {
173160
const headerCell = document.createElement("TH");
174-
headerCell.innerHTML = HDSLib.l(itemDef.data.label);
161+
headerCell.innerHTML = value;
175162
headerRow.appendChild(headerCell);
176-
headers[itemDef.key] = HDSLib.l(itemDef.data.label);
177163
}
178164

179165
// --- patients
180166

181-
// get all invites
182-
const invites = await collector.getInvites();
183-
const activeInvites = invites.filter(i => i.status === 'active');
184-
activeInvites.sort((a, b) => b.dateCreation - a.dateCreation); // sort by creation date reverse
185-
186-
// fetch patient data
187-
const patientPromises = activeInvites.map((invite) =>
188-
drLib.getPatientDetails(invite, itemDefs)
189-
);
190-
const patientsResults = await Promise.all(patientPromises);
191-
console.log('## patientsResults', patientsResults);
192-
193-
const patientsData = [];
194-
for (const patient of patientsResults) {
167+
for (const patient of patientsData) {
195168
const row = table.insertRow(-1);
196-
const patientData = {};
197-
198-
for (const key of Object.keys(staticHeaders)) {
169+
170+
for (const key of Object.keys(headers)) {
199171
let text = patient[key];
200-
patientData[key] = patient[key];
201-
if (key === 'inviteName') {
202-
const page = `dr-patient-view.html?collectorKey=${collector.key}`;
172+
if (key === 'inviteName') { // for inviteName add a link
173+
const page = `dr-patient-view.html?collectorId=${collector.id}&inviteKey=${patient.invite.key}`;
203174
text = `<A HREF="${page}">${patient.inviteName}</A>`;
204175
}
205176
row.insertCell(-1).innerHTML = text;
206177
}
207178

208-
for (const itemDef of itemDefs) {
209-
const value = patient.formData[itemDef.key]?.value;
210-
row.insertCell(-1).innerHTML = (value != null) ? value : '';
211-
patientData[itemDef.key] = value;
212-
}
213-
patientsData.push(patientData);
214179
}
215180
// return this to be used by Excel Download
216181
return { headers, patientsData };

0 commit comments

Comments
 (0)