1+ /**
2+ * UI management code.
3+ * Relies on patientLib for API calls and data management
4+ *
5+ * @param {* } event
6+ */
7+
8+ window . onload = ( event ) => {
9+ stateChange ( 'loggedOut' ) ;
10+ patientHomeLib . showLoginButton ( 'login-button' , stateChange ) ;
11+ } ;
12+
13+ async function stateChange ( state ) {
14+ if ( state === 'loggedIN' ) {
15+ document . getElementById ( 'please-login' ) . style . visibility = 'hidden' ;
16+ document . getElementById ( 'card-content' ) . style . visibility = 'visible' ;
17+ const formApiEndpoint = getRequestFrormApiEndPoint ( ) ;
18+ console . log ( '## formApiEndpoint:' , formApiEndpoint ) ;
19+ const formsInfo = await patientHomeLib . getForms ( formApiEndpoint ) ;
20+ showFormList ( formsInfo )
21+ } else {
22+ document . getElementById ( 'please-login' ) . style . visibility = 'visible' ;
23+ document . getElementById ( 'card-content' ) . style . visibility = 'hidden' ;
24+ }
25+ }
26+
27+ // ------- Get Dr's info -------- //
28+ function getRequestFrormApiEndPoint ( ) {
29+ const params = new URLSearchParams ( document . location . search ) ;
30+ const formApiEndpoint = params . get ( 'formApiEndpoint' ) ;
31+ return formApiEndpoint || null ;
32+ }
33+
34+ // --------- Update form list --------- //
35+ async function showFormList ( formsInfo ) {
36+ console . log ( '## showFormList' , formsInfo ) ;
37+ const table = document . getElementById ( 'questionnary-table' ) ;
38+ for ( const formInfo of formsInfo ) {
39+
40+ // fill the table row
41+ const row = table . insertRow ( - 1 ) ;
42+ const cellQuestionnary = row . insertCell ( - 1 ) ;
43+ cellQuestionnary . innerHTML = formInfo . questionaryId ;
44+ cellQuestionnary . onclick = function ( ) {
45+ showFormDetails ( formInfo ) ;
46+ } ;
47+
48+ const cellDr = row . insertCell ( - 1 ) ;
49+ cellDr . innerHTML = formInfo . drUserId ;
50+
51+ const cellStatus = row . insertCell ( - 1 ) ;
52+ cellStatus . innerHTML = formInfo . formEvent . streamIds [ 0 ] ;
53+ }
54+ }
55+
56+ // ----------- Show form details ----------- //
57+ async function showFormDetails ( formInfo ) {
58+ const show = ! ! formInfo ;
59+ document . getElementById ( 'card-questionnary-details-nothing' ) . style . display = show ? 'none' : 'block' ;
60+ document . getElementById ( 'card-questionnary-details-something' ) . style . display = show ? 'block' : 'none' ;
61+ if ( ! show ) return ;
62+ const formDetails = await patientHomeLib . getQuestionnaryDetails ( formInfo ) ;
63+ console . log ( '## showFormDetails' , formDetails ) ;
64+ // - permissions
65+ const table = document . getElementById ( 'access-request' ) ;
66+ for ( const permission of formDetails . permissions ) {
67+ const row = table . insertRow ( - 1 ) ;
68+ const cellStream = row . insertCell ( - 1 ) ;
69+ cellStream . innerHTML = permission . name ;
70+ const cellLevel = row . insertCell ( - 1 ) ;
71+ cellLevel . innerHTML = permission . level ;
72+ }
73+ // - grant access / open
74+ const button = document . getElementById ( 'grant-access-button' ) ;
75+
76+ // -- pass the apiEndpoint to the next page !! Insecure just for demo
77+ const openHREF = `patient-profile.html?patientApiEndpoint=${ patientHomeLib . getPatientApiEndpoint ( ) } &questionaryId=${ formInfo . questionaryId } ` ;
78+ if ( formDetails . status === 'accepted' ) {
79+ button . innerHTML = 'Open' ;
80+ button . onclick = async function ( ) {
81+ // await patientHomeLib.publishAccess(formInfo, formDetails.sharedApiEndpoint);
82+ document . location . href = openHREF ;
83+ } ;
84+ }
85+ else {
86+ button . innerHTML = 'Grant access and Open' ;
87+ button . onclick = async function ( ) {
88+ await patientHomeLib . grantAccess ( formInfo , formDetails ) ;
89+ document . location . href = openHREF ;
90+ } ;
91+ }
92+
93+ // - json
94+ const jsonContent = document . getElementById ( 'card-questionnary-details-content' ) ;
95+ jsonContent . innerHTML = '<pre>' + JSON . stringify ( formInfo . formEvent , null , 2 ) + '<pre>' ;
96+ }
0 commit comments