Skip to content

Commit d7aa199

Browse files
committed
Changing navigation logic
1 parent 5145c91 commit d7aa199

6 files changed

Lines changed: 92 additions & 141 deletions

CookieUtils.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export const CookieUtils = {
2+
get,
3+
set,
4+
del
5+
};
6+
7+
/**
8+
* Set a local cookie
9+
* @memberof CookieUtils
10+
* @param {string} cookieKey - The key for the cookie
11+
* @param {mixed} value - The Value
12+
* @param {number} expireInDays - Expiration date in days from now
13+
* @param {string} path
14+
*/
15+
function set (cookieKey, value, expireInDays = 365, path) {
16+
expireInDays = expireInDays;
17+
const myDate = new Date();
18+
const hostName = window.location.hostname;
19+
const cookiePath = window.location.pathname;
20+
myDate.setDate(myDate.getDate() + expireInDays);
21+
let cookieStr = encodeURIComponent(cookieKey) + '=' +
22+
encodeURIComponent(JSON.stringify(value)) +
23+
';expires=' + myDate.toGMTString() +
24+
';domain=.' + hostName + ';path=' + cookiePath;
25+
// do not add SameSite when removing a cookie
26+
if (expireInDays >= 0) cookieStr += ';SameSite=Strict';
27+
document.cookie = cookieStr;
28+
}
29+
30+
/**
31+
* Return the value of a local cookie
32+
* @memberof CookieUtils
33+
* @param cookieKey - The key
34+
*/
35+
function get (cookieKey) {
36+
const name = encodeURIComponent(cookieKey);
37+
const value = '; ' + document.cookie;
38+
const parts = value.split( name + '=');
39+
if (parts.length < 2) return null;
40+
const first = parts.pop().split(';').shift();
41+
return JSON.parse(decodeURIComponent(first));
42+
}
43+
44+
/**
45+
* Delete a local cookie
46+
* @memberof CookieUtils
47+
* @param cookieKey - The key
48+
*/
49+
function del (cookieKey) {
50+
set(cookieKey, { deleted: true }, -1);
51+
}

patient-history-controler.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import { patientLib } from './patient-lib.js';
77
*/
88

99
window.onload = async (event) => {
10-
const { patientApiEndpoint, questionaryId } = getPatientApiEndpointAndFormId();
10+
const navData = patientLib.navGetData();
11+
console.log('## navData', navData);
12+
const { patientApiEndpoint, questionaryId } = navData;
1113
console.log('## patientApiEndpoint:', patientApiEndpoint);
12-
await connect(patientApiEndpoint, questionaryId);
14+
await patientLib.connect(patientApiEndpoint, questionaryId);
15+
// - form title
16+
const formTitle = document.getElementById('card-questionnary-details-title');
17+
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
1318
// -- navigation
1419
document.getElementById('nav-profile').onclick = () => {
15-
document.location.href = 'patient-profile.html' + patientLib.getNavigationQueryParams();
20+
document.location.href = 'patient-profile.html';
1621
};
1722

1823
// -- connection
@@ -23,23 +28,6 @@ window.onload = async (event) => {
2328
}
2429

2530

26-
// ------- Get Questionnary and endpoint info -------- //
27-
function getPatientApiEndpointAndFormId() {
28-
const params = new URLSearchParams(document.location.search);
29-
const patientApiEndpoint = params.get('patientApiEndpoint');
30-
const questionaryId = params.get('questionaryId');
31-
if (!patientApiEndpoint) {
32-
alert('No patientApiEndpoint found in the URL');
33-
return;
34-
}
35-
if (!questionaryId) {
36-
alert('No questionaryId found in the URL');
37-
return;
38-
}
39-
return { patientApiEndpoint, questionaryId };
40-
}
41-
42-
4331
// ------- Form -------- //
4432

4533
/**

patient-home-controler.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ async function showFormDetails(formInfo) {
7474
const show = !!formInfo;
7575
document.getElementById('card-questionnary-details-nothing').style.display = show ? 'none' : 'block';
7676
document.getElementById('card-questionnary-details-something').style.display= show ? 'block' : 'none';
77+
// clear navData
78+
patientLib.navSetData(null);
7779
if (!show) return;
7880
const formDetails = await patientHomeLib.getQuestionnaryDetails(formInfo);
7981
console.log('## showFormDetails', formDetails);
@@ -102,13 +104,20 @@ async function showFormDetails(formInfo) {
102104
const buttonRevoke = document.getElementById('revoke-access-button');
103105

104106
// -- pass the apiEndpoint to the next page !! Insecure just for demo
105-
const openHREF = `patient-profile.html?patientApiEndpoint=${patientHomeLib.getPatientApiEndpoint()}&questionaryId=${formInfo.questionaryId}`;
107+
const navData = {
108+
patientApiEndpoint: patientHomeLib.getPatientApiEndpoint(),
109+
questionaryId: formInfo.questionaryId
110+
}
111+
patientLib.navSetData(navData);
112+
console.log("## nav set data", patientLib.navGetData())
113+
106114
if (formDetails.status === 'accepted') {
107115
buttonOpen.innerHTML = 'Open';
108116
buttonOpen.onclick = async function () {
109117
// -- hack publish access anyway (this should be done just once)
110118
await patientHomeLib.publishAccess(formInfo, formDetails.sharedApiEndpoint);
111-
document.location.href = openHREF;
119+
patientLib.navSetData()
120+
document.location.href = 'patient-profile.html';
112121
};
113122
buttonRevoke.innerHTML = 'Revoke';
114123
buttonRevoke.onclick = async function () {
@@ -121,7 +130,7 @@ async function showFormDetails(formInfo) {
121130
buttonOpen.innerHTML = 'Grant access and Open';
122131
buttonOpen.onclick = async function () {
123132
await patientHomeLib.grantAccess(formInfo, formDetails);
124-
document.location.href = openHREF;
133+
document.location.href = 'patient-profile.html';
125134
};
126135
buttonRevoke.innerHTML = 'Refuse';
127136
buttonRevoke.onclick = async function () {

patient-lib.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { dataDefs } from './common-data-defs.js';
2+
import { CookieUtils } from './CookieUtils.js';
23

34
export const patientLib = {
45
handleFormSubmit,
56
getFormTitle,
67
getFormContent,
78
connect,
8-
getNavigationQueryParams
9+
getNavigationQueryParams,
10+
navSetData,
11+
navGetData
912
}
1013

1114

@@ -23,6 +26,19 @@ function getNavigationQueryParams() {
2326
return `?patientApiEndpoint=${connection.apiEndpoint}&questionaryId=${_questionaryId}`
2427
}
2528

29+
// --------------- navigation - to be replaced if built-in framework ------- //
30+
31+
const COOKIE_KEY = 'hds-demo-dr-form';
32+
function navSetData(data) {
33+
if (data == null) return Pryv.Browser.CookieUtils.del(COOKIE_KEY);
34+
CookieUtils.set(COOKIE_KEY, data, 365, '/');
35+
}
36+
37+
function navGetData() {
38+
return CookieUtils.get(COOKIE_KEY);
39+
}
40+
41+
2642
// ---------------- form content ---------------- //
2743

2844
function getFormTitle (questionaryId) {

patient-profile-controler copy.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

patient-profile-controler.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import { patientLib } from './patient-lib.js';
77
*/
88

99
window.onload = async (event) => {
10-
const { patientApiEndpoint, questionaryId } = getPatientApiEndpointAndFormId();
10+
const navData = patientLib.navGetData();
11+
console.log('## navData', navData);
12+
const { patientApiEndpoint, questionaryId } = navData;
1113
console.log('## patientApiEndpoint:', patientApiEndpoint);
1214
await patientLib.connect(patientApiEndpoint, questionaryId);
1315
// - form title
1416
const formTitle = document.getElementById('card-questionnary-details-title');
1517
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
1618
// -- navigation
1719
document.getElementById('nav-history').onclick = () => {
18-
document.location.href = 'patient-history.html' + patientLib.getNavigationQueryParams();
20+
document.location.href = 'patient-history.html';
1921
};
2022
// -- content
2123
updateFormContent(questionaryId, 'profile');
@@ -26,23 +28,6 @@ window.onload = async (event) => {
2628
}
2729

2830

29-
// ------- Get Questionnary and endpoint info -------- //
30-
function getPatientApiEndpointAndFormId() {
31-
const params = new URLSearchParams(document.location.search);
32-
const patientApiEndpoint = params.get('patientApiEndpoint');
33-
const questionaryId = params.get('questionaryId');
34-
if (!patientApiEndpoint) {
35-
alert('No patientApiEndpoint found in the URL');
36-
return;
37-
}
38-
if (!questionaryId) {
39-
alert('No questionaryId found in the URL');
40-
return;
41-
}
42-
return { patientApiEndpoint, questionaryId };
43-
}
44-
45-
4631
// ------- Form -------- //
4732

4833
/**

0 commit comments

Comments
 (0)