Skip to content

Commit ee3ba4e

Browse files
committed
Now relying on lib for model singleton
1 parent 38349ac commit ee3ba4e

6 files changed

Lines changed: 18 additions & 49 deletions

File tree

common-lib.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,6 @@ let model;
44
// following the APP GUIDELINES: https://api.pryv.com/guides/app-guidelines/
55
export const serviceInfoUrl = HDSLib.pryv.Browser.serviceInfoFromUrl() || "https://demo.datasafe.dev/reg/service/info";
66

7-
export function hdsModel () {
8-
if (!model) { throw new Error('Initialize model with `initHDSModel()` first') };
9-
return model;
10-
}
11-
12-
export async function initHDSModel () {
13-
if (!model) {
14-
console.log(HDSLib);
15-
const service = new HDSLib.pryv.Service(serviceInfoUrl);
16-
const serviceInfo = await service.info();
17-
model = new HDSLib.HDSModel(serviceInfo.assets['hds-model']);
18-
console.log('## model:', model);
19-
await model.load();
20-
}
21-
return model;
22-
}
23-
24-
/**
25-
* Return HDSLib.pryv.Connection with property HDSModel Loaded
26-
* @param {string} apiEndpoint
27-
* @returns
28-
*/
29-
export async function connectAPIEndpoint (apiEndpoint) {
30-
const connection = new HDSLib.pryv.Connection(apiEndpoint);
31-
connection.hdsModel = await initHDSModel();
32-
return connection;
33-
}
347

358
// ---- state management to be replaced by framework logic
369

dr-controler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hdsModel, initHDSModel, stateGetApp } from './common-lib.js';
1+
import { stateGetApp } from './common-lib.js';
22
import { drLib } from './dr-lib.js';
33
import { exportXLSFile } from './exporToXLS.js';
44

@@ -15,7 +15,7 @@ window.onload = (event) => {
1515
};
1616

1717
async function stateChange(state) {
18-
await initHDSModel();
18+
await HDSLib.initHDSModel();
1919
if (state === 'loggedIN') {
2020
document.getElementById('please-login').style.display = 'none';
2121
document.getElementById('data-view').style.display = 'block';
@@ -109,7 +109,7 @@ async function showQuestionnary(questionaryId) {
109109
let content = form[key];
110110
if (key === 'itemKeys') {
111111
content = content.map((itemKey) => {
112-
const itemDef = hdsModel().itemsDefs.forKey(itemKey);
112+
const itemDef = HDSLib.model.itemsDefs.forKey(itemKey);
113113
return '- ' + HDSLib.l(itemDef.data.label);
114114
}).join('\n<br>');
115115
}

dr-lib.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dataDefs } from "./common-data-defs.js";
2-
import { hdsModel, serviceInfoUrl, initHDSModel, stateSaveApp } from "./common-lib.js"
2+
import { serviceInfoUrl, stateSaveApp } from "./common-lib.js"
33

44

55
// NEW
@@ -49,7 +49,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
4949
// called each time the authentication state changes
5050
console.log("##pryvAuthStateChange", state);
5151
if (state.id === HDSLib.pryv.Browser.AuthStates.AUTHORIZED) {
52-
await initHDSModel(); // hds model needs to be initialized
52+
await HDSLib.initHDSModel(); // hds model needs to be initialized
5353
const appManaging = await HDSLib.appTemplates.AppManagingAccount.newFromApiEndpoint(APP_MANAGING_STREAMID, state.apiEndpoint, APP_MANAGING_NAME);
5454
stateSaveApp('managing', appManaging);
5555
await initDemoAccount(appManaging);
@@ -92,7 +92,7 @@ async function initDemoAccount (appManaging) {
9292
}
9393
// 2 - get the permissions with eventual preRequest
9494
const preRequest = questionary.permissionsPreRequest || [];
95-
const permissions = hdsModel().authorizations.forItemKeys(itemKeys, { preRequest });
95+
const permissions = HDSLib.model.authorizations.forItemKeys(itemKeys, { preRequest });
9696

9797
const requestContent = {
9898
version: '0',
@@ -141,7 +141,7 @@ async function getPatientsData (collector) {
141141
const firstForm = Object.values(requestContent.app.data.forms)[0];
142142
const itemDefs = [];
143143
for (const itemKey of firstForm.itemKeys) {
144-
const itemDef = hdsModel().itemsDefs.forKey(itemKey);
144+
const itemDef = HDSLib.model.itemsDefs.forKey(itemKey);
145145
itemDefs.push(itemDef);
146146
headers[itemDef.key] = HDSLib.l(itemDef.data.label);
147147
}
@@ -211,7 +211,7 @@ async function getPatientDetails(invite, itemDefs) {
211211
* @param {*} event
212212
*/
213213
function dataFieldFromEvent(event) {
214-
const itemDef = hdsModel().itemsDefs.forEvent(event, false);
214+
const itemDef = HDSLib.model.itemsDefs.forEvent(event, false);
215215
if (!itemDef) {
216216
console.error("## itemDef not found for event", event);
217217
return null;

dr-patient-view-controler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { initHDSModel, stateGetApp } from "./common-lib.js";
1+
import { stateGetApp } from "./common-lib.js";
22
import { drPatientLib } from "./dr-patient-view-lib.js";
33
import { exportXLSFile } from './exporToXLS.js';
44

@@ -14,7 +14,7 @@ import { exportXLSFile } from './exporToXLS.js';
1414

1515
let invite;
1616
window.onload = async (event) => {
17-
await initHDSModel();
17+
await HDSLib.initHDSModel();
1818
// get collectorId & inviteKey from URL
1919
const params = new URLSearchParams(document.location.search);
2020
const collectorId = params.get('collectorId');

dr-patient-view-lib.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11

22

3-
import { connectAPIEndpoint, hdsModel } from './common-lib.js';
4-
53
export const drPatientLib = {
64
setRefresh
75
}
@@ -37,7 +35,7 @@ async function getPatientData (invite) {
3735

3836

3937
function getLineForEvent (event) {
40-
const model = hdsModel();
38+
const model = HDSLib.model;
4139
const line = {
4240
time: (new Date(event.time * 1000)).toISOString(),
4341
formLabel: 'Unkown',

patient-lib.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { hdsModel, initHDSModel } from "./common-lib.js"
2-
31

42
export const patientLib = {
53
handleFormSubmit,
@@ -43,11 +41,11 @@ async function navGetPages(collectorClient) {
4341

4442

4543
async function getFormHistorical (collectorClient, formKey) {
46-
await initHDSModel();
44+
await HDSLib.initHDSModel();
4745
const requestData = collectorClient.requestData;
4846
const form = requestData.app.data.forms[formKey];
4947
const formFields = form.itemKeys.map((itemKey) => {
50-
const itemDef = (hdsModel().itemsDefs.forKey(itemKey));
48+
const itemDef = HDSLib.model.itemsDefs.forKey(itemKey);
5149

5250
return {
5351
id: itemDef.key,
@@ -67,12 +65,12 @@ async function getFormHistorical (collectorClient, formKey) {
6765
* @returns
6866
*/
6967
async function getFormPermanentContent (collectorClient, formKey) {
70-
await initHDSModel();
68+
await HDSLib.initHDSModel();
7169
const requestData = collectorClient.requestData;
7270
const form = requestData.app.data.forms[formKey];
7371
console.log('## getFormPermanentContent ', {form, formKey, collectorClient})
7472
// get formItems
75-
const formItemDefs = form.itemKeys.map((itemKey) => (hdsModel().itemsDefs.forKey(itemKey)));
73+
const formItemDefs = form.itemKeys.map((itemKey) => (HDSLib.model.itemsDefs.forKey(itemKey)));
7674
// get the values from the API
7775
const apiCalls = formItemDefs.map(itemDef => ({
7876
method: 'events.get',
@@ -114,10 +112,10 @@ async function getFormPermanentContent (collectorClient, formKey) {
114112

115113

116114
async function getHistoricalContent(collectorClient, formKey) {
117-
await initHDSModel();
115+
await HDSLib.initHDSModel();
118116
const requestData = collectorClient.requestData;
119117
const form = requestData.app.data.forms[formKey];
120-
const itemDefs = form.itemKeys.map((itemKey) => (hdsModel().itemsDefs.forKey(itemKey)));
118+
const itemDefs = form.itemKeys.map((itemKey) => (HDSLib.model.itemsDefs.forKey(itemKey)));
121119
const tableHeaders = itemDefs.map(itemDef => ({
122120
fieldId: itemDef.key,
123121
label: itemDef.data.label.en,
@@ -126,7 +124,7 @@ async function getHistoricalContent(collectorClient, formKey) {
126124

127125
const valuesByDateStr = {};
128126
function addEntry (event) {
129-
const itemDef = hdsModel().itemsDefs.forEvent(event, false);
127+
const itemDef = HDSLib.model.itemsDefs.forEvent(event, false);
130128
if (itemDef == null) {
131129
console.log('Historical content -- unkown event', event);
132130
return;

0 commit comments

Comments
 (0)