Skip to content

Commit ad8e534

Browse files
committed
Now fecthing user list
1 parent de16ce3 commit ad8e534

7 files changed

Lines changed: 117 additions & 43 deletions

File tree

common-data-defs.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,32 @@ const patientBasePermissions = [
2323
{id: 'body-weight', name: 'Body weight'},
2424
]
2525

26+
const formContent = [
27+
{
28+
streamId: 'profile-name',
29+
eventType: 'contact/name',
30+
type: 'text',
31+
label: 'Name',
32+
dataFieldKey: 'name',
33+
},
34+
{
35+
streamId: 'profile-name',
36+
eventType: 'contact/surname',
37+
type: 'text',
38+
label: 'Surname',
39+
dataFieldKey: 'surname',
40+
},
41+
{
42+
streamId: 'profile-nationality',
43+
eventType: 'contact/nationality',
44+
type: 'text',
45+
label: 'Nationality',
46+
dataFieldKey: 'nationality',
47+
},
48+
];
2649

2750
const dataDefs = {
2851
patientBaseStreams,
29-
patientBasePermissions
52+
patientBasePermissions,
53+
formContent
3054
};

dr-controler.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
* @param {*} event
66
*/
77

8-
9-
108
window.onload = (event) => {
119
stateChange('loggedOut');
1210
drLib.showLoginButton('login-button', stateChange);
1311
};
1412

15-
function stateChange(state) {
13+
async function stateChange(state) {
1614
if (state === 'loggedIN') {
1715
document.getElementById('please-login').style.visibility = 'hidden';
1816
document.getElementById('data-view').style.visibility = 'visible';
1917
setSharingLink();
18+
await drLib.getPatientsList();
2019
} else {
2120
document.getElementById('please-login').style.visibility = 'visible';
2221
document.getElementById('data-view').style.visibility = 'hidden';

dr-lib.js

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ let connection = null;
22

33
const drLib = {
44
showLoginButton,
5-
getSharingToken
5+
getSharingToken,
6+
getPatientsList,
67
}
78

89
function showLoginButton (loginSpanId, stateChangeCallBack) {
@@ -35,7 +36,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
3536
console.log('##pryvAuthStateChange', state);
3637
if (state.id === Pryv.Browser.AuthStates.AUTHORIZED) {
3738
connection = new Pryv.Connection(state.apiEndpoint);
38-
await initPatientAccount(connection);
39+
await initDrAccount(connection);
3940
stateChangeCallBack('loggedIN');
4041
}
4142
if (state.id === Pryv.Browser.AuthStates.INITIALIZED) {
@@ -45,12 +46,79 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
4546
}
4647
}
4748

49+
// -------- Fetch patient list --------
50+
51+
const patients = {};
52+
53+
async function getPatientsList () {
54+
const res = await connection.api([{ method: 'events.get', params: { types: ['credentials/pryv-api-endpoint'] } }]);
55+
const patientApiEndpointEvents = res[0].events;
56+
for (const event of patientApiEndpointEvents) {
57+
if (event.type === 'credentials/pryv-api-endpoint') {
58+
const patient = {
59+
apiEndpoint: event.content,
60+
formData: {}
61+
};
62+
const patientConnection = new Pryv.Connection(patient.apiEndpoint);
63+
// -- get patient info
64+
const patientInfo = await patientConnection.accessInfo();
65+
patient.username = patientInfo.user.username;
66+
67+
// -- get data
68+
const profileEvents = await patientConnection.api([{ method: 'events.get', params: { streams: ['profile'] } }]);
69+
for (const profileEvent of profileEvents[0].events) {
70+
const field = dataFieldFromEvent(profileEvent);
71+
if (field) {
72+
patient.formData[field.key] = field;
73+
}
74+
}
75+
patients[patient.username] = patient;
76+
}
77+
}
78+
79+
console.log('## Patients list', patients);
80+
return patients;
81+
}
82+
83+
const dataFieldsCache = {};
84+
function initFieldsCache (event) {
85+
if (Object.keys(dataFieldsCache).length !== 0) return;
86+
for (const formField of dataDefs.formContent) {
87+
const dataFieldId = formField.streamId + ':' + formField.eventType;
88+
dataFieldsCache[dataFieldId] = formField;
89+
}
90+
}
91+
92+
/**
93+
* Link an event to a data field from form
94+
* @param {*} event
95+
*/
96+
function dataFieldFromEvent (event) {
97+
initFieldsCache(event);
98+
const formFieldId = event.streamId + ':' + event.type;
99+
const dataField = dataFieldsCache[formFieldId];
100+
if (!dataField) {
101+
console.error('## Data field not found for event', event);
102+
return null;
103+
}
104+
const field = {
105+
formFieldId,
106+
key: dataField.dataFieldKey,
107+
label: dataField.label,
108+
type: dataField.type,
109+
value: event.content,
110+
event: event
111+
};
112+
return field;
113+
}
114+
115+
// -------- initualization functions --------
48116

49117
/**
50118
* Initialize the doctor account
51119
* @param {*} connection
52120
*/
53-
async function initPatientAccount (connection) {
121+
async function initDrAccount (connection) {
54122
await initStreams(connection);
55123
console.log('## Dr account initialized')
56124
}

dr.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<body>
2020
<div class="container">
21+
<img src="https://healthdatasafe.github.io/style/images/Horizontal/hds-logo-hz-1024x400-hz-color.png" alt="Logo" width="300px">
2122
<h1>Demo Form - Dr's page</h1>
2223
<div class="card">
2324
<div class="card-body">

patient-controler.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ async function getDoctorInfo() {
4141
* Take the from content from the definition and actual values and create the HTML
4242
*/
4343
async function updateFormContent() {
44-
const formContent = await patientLib.getFormContent();
45-
console.log('Form content:', formContent);
44+
const formData = await patientLib.getFormContent();
45+
console.log('Form content:', formData);
4646

4747
document.getElementById('inputs-list').innerHTML = ''; // Clear previous content
48-
for (let i = 0; i < formContent.length; i++) {
49-
const formField = formContent[i];
48+
for (let i = 0; i < formData.length; i++) {
49+
const formField = formData[i];
5050
const fieldId = formField.id;
5151
const fieldValue = formField.value || '';
5252
const fieldType = formField.type;
@@ -73,9 +73,9 @@ async function updateFormContent() {
7373
*/
7474
async function submitForm() {
7575
const values = {};
76-
const formContent = await patientLib.getFormContent();
77-
for (let i = 0; i < formContent.length; i++) {
78-
const field = formContent[i];
76+
const formData = await patientLib.getFormContent();
77+
for (let i = 0; i < formData.length; i++) {
78+
const field = formData[i];
7979
const fieldId = field.id;
8080
const fieldValue = document.getElementById(fieldId).value.trim();
8181
values[field.id] = fieldValue; // Store the value in the values object

patient-lib.js

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ async function initSharingWithDr (formApiEndpoint) {
8585
const sharedAccess = accessesCheckRes[0].accesses.find(access => access.name === sharingAccessId);
8686
if (sharedAccess) {
8787
console.log('## Already created access for this Dr');
88+
return;
8889
}
8990
//-- create a set of permission based on the datadefs streams --//
9091
const permissions = dataDefs.patientBasePermissions.map(perm => ({
@@ -123,36 +124,16 @@ async function initSharingWithDr (formApiEndpoint) {
123124

124125
// ---------------- form content ---------------- //
125126

126-
const formContent = [
127-
{
128-
streamId: 'profile-name',
129-
eventType: 'contact/name',
130-
contentField: 'name',
131-
type: 'text',
132-
label: 'Name',
133-
},
134-
{
135-
streamId: 'profile-name',
136-
eventType: 'contact/surname',
137-
contentField: 'surname',
138-
type: 'text',
139-
label: 'Surname',
140-
},
141-
{
142-
streamId: 'profile-nationality',
143-
eventType: 'contact/nationality',
144-
type: 'text',
145-
label: 'Nationality',
146-
},
147-
];
148-
149-
let formInitialized = null;
127+
128+
// local copy of formContent + actual values
129+
let formData = null;
150130
async function getFormContent () {
151-
if (formInitialized) { return formContent; }
131+
if (formData) { return formData; }
132+
formData = structuredClone(dataDefs.formContent);
152133
formInitialized = true; // prevent multiple calls to this function
153134

154135
// get the values from the API
155-
const apiCalls = formContent.map(field => ({
136+
const apiCalls = formData.map(field => ({
156137
method: 'events.get',
157138
params: {
158139
streams: [field.streamId],
@@ -164,7 +145,7 @@ async function getFormContent () {
164145
const res = await connection.api(apiCalls);
165146
for (let i = 0; i < res.length; i++) {
166147
const e = res[i];
167-
const field = formContent[i];
148+
const field = formData[i];
168149
field.id = 'field-' + i; // generate a unique id for the field
169150
console.log('## getFormContent ' + i, e);
170151
if (e.events && e.events.length > 0) {
@@ -173,13 +154,13 @@ async function getFormContent () {
173154
field.eventId = event.id; // will allow t track if the event is to be updated
174155
}
175156
}
176-
return formContent;
157+
return formData;
177158
};
178159

179160
// ---------------- create / update data ---------------- //
180161
async function handleFormSubmit (values) {
181162
const apiCalls = [];
182-
for (const field of formContent) {
163+
for (const field of formData) {
183164
const streamId = field.streamId;
184165
const eventType = field.eventType;
185166
const eventId = field.eventId;

patient.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<body>
2020
<div class="container">
21+
<img src="https://healthdatasafe.github.io/style/images/Horizontal/hds-logo-hz-1024x400-hz-color.png" alt="Logo" width="300px">
2122
<h1>Demo Form - Patient's page</h1>
2223
<div class="card">
2324
<div class="card-body">

0 commit comments

Comments
 (0)