@@ -6,22 +6,29 @@ import { navControler } from './patient-nav-controler.js'
66 *
77 * @param {* } event
88 */
9-
9+ let navData ;
1010window . onload = async ( event ) => {
11- const navData = await navControler . setNavComponents ( ) ;
11+ navData = await navControler . setNavComponents ( ) ;
1212 console . log ( '## navData' , navData ) ;
13- const { patientApiEndpoint, questionaryId } = navData ;
13+ const { patientApiEndpoint, questionaryId, formKey } = navData ;
1414 console . log ( '## patientApiEndpoint:' , patientApiEndpoint ) ;
1515 await patientLib . connect ( patientApiEndpoint , questionaryId ) ;
16- // - form title
17- const formTitle = document . getElementById ( 'card-questionnary-details-title' ) ;
18- formTitle . innerHTML = patientLib . getFormTitle ( questionaryId ) ;
19-
20- // -- connection
21- updateFormContent ( questionaryId , 'history' ) ;
22- document . getElementById ( 'submit-button-history' ) . addEventListener ( "click" , function ( ) {
23- submitForm ( questionaryId , 'history' ) ;
24- } ) ;
16+ // - form title
17+ const formTitle = document . getElementById ( 'card-questionnary-details-title' ) ;
18+ formTitle . innerHTML = patientLib . getFormTitle ( questionaryId ) ;
19+ refreshForm ( ) ;
20+ }
21+
22+
23+ async function refreshForm ( ) {
24+ const { questionaryId, formKey } = navData ;
25+ // -- content
26+ ; console . log ( )
27+ const formData = await patientLib . getFormContent ( questionaryId , formKey )
28+ updateFormContent ( formData ) ;
29+ document . getElementById ( 'submit-button-list' ) . onclick = function ( ) {
30+ submitForm ( formData ) ;
31+ } ;
2532}
2633
2734
@@ -30,15 +37,54 @@ window.onload = async (event) => {
3037/**
3138 * Take the from content from the definition and actual values and create the HTML
3239 */
33- async function updateFormContent ( questionaryId , formKey ) {
34- const formData = await patientLib . getFormContent ( questionaryId , formKey ) ;
40+ async function updateFormContent ( formData ) {
3541 console . log ( 'Form content:' , formData ) ;
3642
43+ // Append the HTML to the form
44+ document . getElementById ( 'inputs-list' ) . innerHTML = '' ; // Clear previous content
45+ for ( let i = 0 ; i < formData . length ; i ++ ) {
46+ const formField = formData [ i ] ;
47+ const fieldId = formField . id ;
48+ const fieldValue = ( formField . value != null ) ? formField . value : '' ;
49+ const fieldType = formField . type ;
50+ const fieldLabel = formField . label ;
51+
52+ // Create the HTML for the form field
53+ let fieldHTML = `\n<BR><label for="${ fieldId } ">${ fieldLabel } </label>` ;
54+ if ( fieldType === 'text' || fieldType === 'number' ) {
55+ fieldHTML += `<input type="${ fieldType } " id="${ fieldId } " value="${ fieldValue } " class="form-control"/>` ;
56+ } else if ( fieldType === 'select' ) {
57+ fieldHTML += `<select id="${ fieldId } " class="form-control">` ;
58+ fieldHTML += `<option value="">--</option>` ;
59+ for ( const option of formField . options ) {
60+ const selected = ( option . value === fieldValue ) ? 'selected' : '' ;
61+ fieldHTML += `<option value="${ option . value } " ${ selected } >${ option . label } </option>` ;
62+ }
63+ fieldHTML += `</select>` ;
64+ } else if ( fieldType === 'date' ) {
65+ fieldHTML += `<input type="date" id="${ fieldId } " value="${ fieldValue } " class="form-control"/>` ;
66+ }
67+
68+ document . getElementById ( 'inputs-list' ) . innerHTML += fieldHTML ;
69+ }
3770}
3871
3972/**
4073 * Submit the form and send the data to the API
4174 */
42- async function submitForm ( questionaryId , formKey ) {
43-
75+ async function submitForm ( formData ) {
76+ const values = { } ;
77+ for ( let i = 0 ; i < formData . length ; i ++ ) {
78+ const field = formData [ i ] ;
79+ const fieldId = field . id ;
80+ const formField = document . getElementById ( fieldId ) ;
81+ // Store the value in the values object
82+ if ( field . type === 'date' ) {
83+ values [ field . id ] = formField . valueAsDate ;
84+ } else {
85+ values [ field . id ] = formField . value . trim ( ) ;
86+ }
87+ }
88+ await patientLib . handleFormSubmit ( formData , values ) ;
89+ alert ( 'Form submitted successfully' ) ;
4490} ;
0 commit comments