11import React , { useState , useEffect , useRef } from 'react' ;
22import styles from './index.module.css' ;
33import beautify from "xml-beautifier" ;
4- import { saveFormSubmission } from '../../api' ;
4+ import { getPrefillXML , saveFormSubmission } from '../../api' ;
55
66const GITPOD_URL = process . env . REACT_APP_GITPOD_WORKSPACE_URL
77
@@ -17,18 +17,13 @@ const GenericForm = (props) => {
1717 return encodeURIComponent ( JSON . stringify ( func ) ) ;
1818 }
1919
20- const getFormURI = ( form , ofsd , prefillSpec ) => {
21- // console.log(form, ofsd, prefillSpec);
22- // return encodeURIComponent(`https://3006-samagradevelop-workflow-gkbrz650idv.ws-us89b.gitpod.io/prefill?form=${form}&onFormSuccessData=${encodeFunction(ofsd)}&prefillSpec=${encodeFunction(prefillSpec)}`);
23- return encodeURIComponent ( `${ GITPOD_URL . slice ( 0 , GITPOD_URL . indexOf ( '/' ) + 2 ) + "3006-" + GITPOD_URL . slice ( GITPOD_URL . indexOf ( '/' ) + 2 ) } /prefill?form=${ form } &onFormSuccessData=${ encodeFunction ( ofsd ) } &prefillSpec=${ encodeFunction ( prefillSpec ) } ` ) ;
24- }
25-
2620 const startingForm = formSpec . startingForm ;
21+ const [ fileUrls , setFileUrls ] = useState ( { } ) ;
2722 const [ formId , setFormId ] = useState ( startingForm ) ;
2823 const [ encodedFormSpec , setEncodedFormSpec ] = useState ( encodeURI ( JSON . stringify ( formSpec . forms [ formId ] ) ) ) ;
2924 const [ onFormSuccessData , setOnFormSuccessData ] = useState ( undefined ) ;
3025 const [ onFormFailureData , setOnFormFailureData ] = useState ( undefined ) ;
31- const [ encodedFormURI , setEncodedFormURI ] = useState ( getFormURI ( formId , formSpec . forms [ formId ] . onFormSuccess , formSpec . forms [ formId ] . prefill ) ) ;
26+ const [ encodedFormURI , setEncodedFormURI ] = useState ( '' ) ;
3227 const formSubmitted = useRef ( false ) ;
3328
3429 useEffect ( ( ) => {
@@ -37,7 +32,8 @@ const GenericForm = (props) => {
3732 const data = typeof e . data === "string" ? JSON . parse ( e . data ) : e . data ;
3833 try {
3934 const { nextForm, formData, onSuccessData, onFailureData } = data ;
40- // console.log("data--->", data)
35+ console . log ( "data--->" , data )
36+ if ( data . fileURLs ) setFileUrls ( data . fileURLs )
4137
4238 if ( data ?. state != "ON_FORM_SUCCESS_COMPLETED" && formData ) {
4339 setFormData ( beautify ( formData ) )
@@ -53,13 +49,15 @@ const GenericForm = (props) => {
5349 } ) ;
5450 formSubmitted . current = false ;
5551 }
56- if ( nextForm . type === 'form' ) {
52+ if ( nextForm ? .type === 'form' ) {
5753 setFormId ( nextForm . id ) ;
5854 setOnFormSuccessData ( onSuccessData ) ;
5955 setOnFormFailureData ( onFailureData ) ;
6056 setEncodedFormSpec ( encodeURI ( JSON . stringify ( formSpec . forms [ formId ] ) ) ) ;
61- setEncodedFormURI ( getFormURI ( nextForm . id , onSuccessData , formSpec . forms [ nextForm . id ] . prefill ) ) ;
62- } else {
57+ let newForm = await getPrefillXML ( nextForm . id , formSpec . forms [ nextForm . id ] . onFormSuccess ) ;
58+ console . log ( "new FORM" , newForm )
59+ setEncodedFormURI ( newForm ) ;
60+ } else if ( nextForm ?. type == 'url' ) {
6361 window . location . href = nextForm . url ;
6462 }
6563
@@ -92,12 +90,32 @@ const GenericForm = (props) => {
9290 return jsonRes ?. data ;
9391 }
9492
93+ const getForm = async ( ) => {
94+ let prefilledForm = await getPrefillXML ( startingForm , formSpec . forms [ formId ] . onFormSuccess ) ;
95+ setEncodedFormURI ( prefilledForm )
96+ }
97+
98+ useEffect ( ( ) => {
99+ getForm ( ) ;
100+ } , [ ] )
101+
95102 return (
96103 < div className = { styles . container } >
97- < div className = { styles . header } >
104+ < div className = { styles . header + ' animate__animated animate__slideInLeft animate__faster' } >
98105 < div onClick = { ( ) => setSelectedFlow ( { } ) } > Go Back</ div >
99106 < div > Workflow /{ selectedFlow . name } </ div >
100107 </ div >
108+ { Object . keys ( fileUrls ) ?. length > 0 && < div className = { styles . imageLinks } >
109+ < p > Uploaded Images</ p >
110+ { Object . keys ( fileUrls ) ?. map ( el => < a href = { fileUrls [ el ] . url } target = "_blank" > { fileUrls [ el ] . url } </ a > ) }
111+ </ div >
112+ }
113+ {
114+ selectedFlow . offline && < p className = 'animate__animated animate__fadeIn' style = { { color : '#fff' , fontSize : '1.5rem' } } > Disable internet and try submitting the form</ p >
115+ }
116+ {
117+ selectedFlow . submitToHasura && < p className = 'animate__animated animate__fadeIn' style = { { color : '#fff' , fontSize : '1.5rem' } } > Submit the form and check < a style = { { color : '#ffc119' } } target = "_blank" href = { `${ GITPOD_URL . slice ( 0 , GITPOD_URL . indexOf ( '/' ) + 2 ) + "8080-" + GITPOD_URL . slice ( GITPOD_URL . indexOf ( '/' ) + 2 ) } ` } > Hasura</ a > </ p >
118+ }
101119 < div className = { styles . formContainer } >
102120 < iframe title = 'current-form'
103121 className = { styles . odkForm }
0 commit comments